0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ElasticsearchでDateと判別される文字列の種類を調べてみた

Last updated at Posted at 2022-04-19

以前こんな記事を書きました

このときは何も考えずにdateを入れたらtextになってしまって、それを後からなんとかした話でした。

新しくindexを作るに当たって、勝手にdateになるformatを調べました。詳細はここのDate detectionという項目に説明があります。

自分でformatを指定することもできますが、mappingも面倒なので、document側でcontrolすることにします。

test documents

これを流しました。

delete test
put test

# text
PUT test/_doc/a
{
  "a": "2022/1/1"
}

# date
PUT test/_doc/b
{
  "b": "2022/12/31"
}

# date
PUT test/_doc/c
{
  "c": "2022-12-31"
}

# text
PUT test/_doc/d
{
  "d": "2022-12-31 12:34:56"
}

# date
PUT test/_doc/e
{
  "e": "2022-12-31T12:34:56"
}

# date
PUT test/_doc/f
{
  "f": "2022-12-31T12:34:56.000"
}

# text
PUT test/_doc/g
{
  "g": "2022-12-31 12:34:56.000"
}

# date
PUT test/_doc/h
{
  "h": "2022-12-31T12:34:56+0900"
}

# text
PUT test/_doc/i
{
  "i": "2022-12-31 12:34:56+0900"
}


# date
PUT test/_doc/j
{
  "j": "2022-04-22T15:23:32.769446"
}


# date
PUT test/_doc/k
{
  "k": "2022-08-19T02:57:23.084Z"
}


# text
PUT test/_doc/l
{
  "l": "Thu Aug 25 2022 00:00:00 GMT+0900 (Japan Standard Time)"
}



get test/_mapping

Result

結果。

{
  "test" : {
    "mappings" : {
      "properties" : {
        "a" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "b" : {
          "type" : "date",
          "format" : "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis"
        },
        "c" : {
          "type" : "date"
        },
        "d" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "e" : {
          "type" : "date"
        },
        "f" : {
          "type" : "date"
        },
        "g" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "h" : {
          "type" : "date"
        },
        "i" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "j" : {
          "type" : "date"
        },
        "k" : {
          "type" : "date"
        },
        "l" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

inputから来る形式を、それぞれいい感じに変換してあげればよさそうです。

Pythonの場合は datetime.now().isoformat() でok

from datetime import datetime
datetime.now().isoformat()       # '2022-04-22T15:23:32.769446'
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?