11
14

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 5 years have passed since last update.

Elasticsearchで、yyyy/MM/dd HH:mm:ssの文字を日付型にパースしたい

Last updated at Posted at 2019-01-21

困ったこと

「yyyy/MM/dd HH: mm:ss」形式の文字をインデックステンプレートから、Dateにパースしたらエラーが発生した

※HH: mmの間に半角はいって申し訳ない・絵文字になるもので

【インデックステンプレート】

PUT _template/template_1
{
  "index_patterns": ["te*", "bar*"],
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "_doc": {
      "properties": {
        "sysdate": {
          "type": "date"
        }
      }
    }
  }
}

【インデックスの登録】

PUT test_index/_doc/1
{
  "sysdate":"2019/01/21 23:00"
}

【エラー】

"reason": "Invalid format: \"2019/01/21 23:00\" is malformed at \"/01/21 23:00\""

うーん:cold_sweat: インデックステンプレートをformatを追加すればよいのか
Elasticsearch Reference [6.5] format

        "sysdate": {
          "type": "date",
          "format": "yyyy/MM/dd HH:mm:ss"
        }

エラーの状況は変わりませんでした。下記にformatを変更しても変わりませんでした。

"format": "yyyy/MM/dd' 'HH:mm:ss"
"format": "yyyy/MM/ddTHH:mm:ss"

下記の資料やElasticsearchのformatを読んだところ「YYYY/MM/DD」の形しか対応していない・・・

Elasticsearch 先輩で日付を触りたい

対応方法

下記に同様な悩みを抱えている記事をみつけた

Failed to parse date field yyyy/MM/dd HH: mm:ss SOLVED

手順としては下記の通り
①ingest・pipeline・gsubを使い「/」 ⇨「-」、「半角スペース」⇨「T」へ変換
 「yyyy/MM/ddTHH: mm:ss」→「yyyy-MM-ddTHH: mm:ss」
Gsub Processor
②formatに「date_time_no_millis」を使いパースする

【ingest・pipeline・gsub】
gsub を複数記載するには下記を参考にした
Elastic Ingest with multiple grok processors

PUT _ingest/pipeline/test_pipeline
{
  "description" : "test pipeline",
  "processors" : [
    {
      "gsub" : {
        "field": "sysdate",
        "pattern": "/",
        "replacement": "-",
        "ignore_missing":true
      }
    },
    {
      "gsub" : {
        "field": "sysdate",
        "pattern": " ",
        "replacement": "T",
        "ignore_missing":true
      }
    }
  ]
}

【インデックステンプレート】

PUT _template/template_1
{
  "index_patterns": ["te*", "bar*"],
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "_doc": {
      "properties": {
        "sysdate": {
          "type": "date",
          "format": "date_time_no_millis"
        }
      }
    }
  }
}

【インデックスの登録】
「?pipeline=」でpipelineを設定する

PUT test_index/_doc/1?pipeline=test_pipeline
{
  "sysdate":"2019/01/21 23:00"
}

【結果】
上手くいった!!

{
  "_index" : "test_index",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

終わりに

gsub はifを使えるだね
Conditional Execution in Pipelines

gsubってpattern.matcherしてreplaceAllしてるのか
GsubProcessor.java

11
14
3

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
11
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?