質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Elasticsearch

Elasticsearchは、クラウド向けに構築された、RESTful な API を提供する分散型のサーチエンジンアプリケーションです。

Q&A

解決済

1回答

1459閲覧

【Elasticsearch7】data_range buckets:failed to parse date field [Now-1y] with format [yyyy-MM-dd]エラー

a0002

総合スコア7

Elasticsearch

Elasticsearchは、クラウド向けに構築された、RESTful な API を提供する分散型のサーチエンジンアプリケーションです。

0グッド

0クリップ

投稿2020/12/28 02:07

困っていること

Elasticsearch7で下記のようdata_range bucketsの定義をしたのですが、
以下のようなエラーが出てしまいます。

解決方法をご教授願えないでしょうか?

data_range bucketsのクエリ記法

curl -XGET'http://localhost:9200/niziu/_search?pretty' -H 'Content-Type:application/json' -d' > { "aggs": {     "genre": {      "date_range": {        "field": "date" ,        "format":"yyyy-MM-dd" ,        "ranges":[{ "from":"Now-1y" ,"to":"Now" }]   }}}}'

エラー内容

{ "error" : { "root_cause" : [ { "type" : "parse_exception", "reason" : "failed to parse date field [Now-1y] with format [yyyy-MM-dd]: [failed to parse date field [Now-1y] with format [yyyy-MM-dd]]" } ], "type" : "search_phase_execution_exception", "reason" : "all shards failed", "phase" : "query", "grouped" : true, "failed_shards" : [ { "shard" : 0, "index" : "niziu", "node" : "blesSRiaRTW6xRf_O8_jNg", "reason" : { "type" : "parse_exception", "reason" : "failed to parse date field [Now-1y] with format [yyyy-MM-dd]: [failed to parse date field [Now-1y] with format [yyyy-MM-dd]]", "caused_by" : { "type" : "illegal_argument_exception", "reason" : "failed to parse date field [Now-1y] with format [yyyy-MM-dd]", "caused_by" : { "type" : "date_time_parse_exception", "reason" : "Text 'Now-1y' could not be parsed at index 0" } } } } ] }, "status" : 400 }

試したこと1

format関連でエラーが発生しているようだったので
'Now-1y'を'Now-1M'に範囲を変えてみたが、エラー内容に変化はなかった

試したこと2

同じくformat関連でエラーが発生しているようだったので
クエリ記法で「"format":"yyyy-MM-dd" , 」を抜いてみたが
以下のようなエラーが発生した

{ "error" : { "root_cause" : [ { "type" : "parse_exception", "reason" : "failed to parse date field [Now-1y] with format [strict_date_optional_time||epoch_millis]: [failed to parse date field [Now-1y] with format [strict_date_optional_time||epoch_millis]]" } ], "type" : "search_phase_execution_exception", "reason" : "all shards failed", "phase" : "query", "grouped" : true, "failed_shards" : [ { "shard" : 0, "index" : "niziu", "node" : "blesSRiaRTW6xRf_O8_jNg", "reason" : { "type" : "parse_exception", "reason" : "failed to parse date field [Now-1y] with format [strict_date_optional_time||epoch_millis]: [failed to parse date field [Now-1y] with format [strict_date_optional_time||epoch_millis]]", "caused_by" : { "type" : "illegal_argument_exception", "reason" : "failed to parse date field [Now-1y] with format [strict_date_optional_time||epoch_millis]", "caused_by" : { "type" : "date_time_parse_exception", "reason" : "Failed to parse with all enclosed parsers" } } } } ] }, "status" : 400 }

データの中身

{"index":{"_index":"niziu","_type":"_doc"}} { "name": "山口 真子" , "date": "2020-12-24", "genre": "st1" , "price": "10" } {"index":{"_index":"niziu","_type":"_doc"}} { "name": "花橋 梨緒" , "date": "2020-12-23", "genre": "st1" , "price": "100" } {"index":{"_index":"niziu","_type":"_doc"}} { "name": "勝村 摩耶" , "date": "2020-12-23", "genre": "st2" , "price": "50" }

補足情報(FW/ツールのバージョンなど)

CentOS8
Elasticsearch7.10

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

原因と解決方法

クエリで指定Nowを指定されていますが、小文字でnowを指定されるとエラーが解消されるかと思います。
定義 : https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#date-math

検証

マッピングが指定されていなかったので、以下の内容で仮定して検証を行いました。

json

1{ 2 "mappings": { 3 "properties": { 4 "name": { 5 "type": "text" 6 }, 7 "date": { 8 "type": "date", 9 "format": "date_optional_time" 10 }, 11 "genre": { 12 "type": "text" 13 }, 14 "price": { 15 "type": "integer" 16 } 17 } 18 } 19}

この状態で、添付のクエリに似た物を投げてみます。

json

1{ 2 "aggs": { 3 "genre": { 4 "date_range": { 5 "field": "date", 6 "ranges": [ 7 { 8 "from": "Now-1y", 9 "to": "Now" 10 } 11 ] 12 } 13 } 14 } 15}

質問と同様のエラーが得られることを確認しました。

json

1{ 2 "error" : { 3 "root_cause" : [ 4 { 5 "type" : "parse_exception", 6 "reason" : "failed to parse date field [Now-1y] with format [date_optional_time]: [failed to parse date field [Now-1y] with format [date_optional_time]]" 7 } 8 ], 9 "type" : "search_phase_execution_exception", 10 "reason" : "all shards failed", 11 "phase" : "query", 12 "grouped" : true, 13 "failed_shards" : [ 14 { 15 "shard" : 0, 16 "index" : "niziu", 17 "node" : "1iRyCg0_RXWtawkBw9GZLw", 18 "reason" : { 19 "type" : "parse_exception", 20 "reason" : "failed to parse date field [Now-1y] with format [date_optional_time]: [failed to parse date field [Now-1y] with format [date_optional_time]]", 21 "caused_by" : { 22 "type" : "illegal_argument_exception", 23 "reason" : "failed to parse date field [Now-1y] with format [date_optional_time]", 24 "caused_by" : { 25 "type" : "date_time_parse_exception", 26 "reason" : "Text 'Now-1y' could not be parsed at index 0" 27 } 28 } 29 } 30 } 31 ] 32 }, 33 "status" : 400 34}

クエリ内のNownowに変換してリクエストしてみます。

json

1{ 2 "aggs": { 3 "genre": { 4 "date_range": { 5 "field": "date", 6 "ranges": [ 7 { 8 "from": "now-1y", 9 "to": "now" 10 } 11 ] 12 } 13 } 14 } 15}

結果、想定した検索結果を得ることができました。

json

1{ 2 "took" : 2, 3 "timed_out" : false, 4 "_shards" : { 5 "total" : 1, 6 "successful" : 1, 7 "skipped" : 0, 8 "failed" : 0 9 }, 10 "hits" : { 11 "total" : { 12 "value" : 3, 13 "relation" : "eq" 14 }, 15 "max_score" : 1.0, 16 "hits" : [ 17 { 18 "_index" : "niziu", 19 "_type" : "_doc", 20 "_id" : "tDCx13YBaiqaF_IqRiiW", 21 "_score" : 1.0, 22 "_source" : { 23 "name" : "山口 真子", 24 "date" : "2020-12-24", 25 "genre" : "st1", 26 "price" : "10" 27 } 28 }, 29 { 30 "_index" : "niziu", 31 "_type" : "_doc", 32 "_id" : "tTCx13YBaiqaF_IqRiiW", 33 "_score" : 1.0, 34 "_source" : { 35 "name" : "花橋 梨緒", 36 "date" : "2020-12-23", 37 "genre" : "st1", 38 "price" : "100" 39 } 40 }, 41 { 42 "_index" : "niziu", 43 "_type" : "_doc", 44 "_id" : "tjCx13YBaiqaF_IqRiiW", 45 "_score" : 1.0, 46 "_source" : { 47 "name" : "勝村 摩耶", 48 "date" : "2020-12-23", 49 "genre" : "st2", 50 "price" : "50" 51 } 52 } 53 ] 54 }, 55 "aggregations" : { 56 "genre" : { 57 "buckets" : [ 58 { 59 "key" : "2020-01-06T12:44:01.294Z-2021-01-06T12:44:01.294Z", 60 "from" : 1.578314641294E12, 61 "from_as_string" : "2020-01-06T12:44:01.294Z", 62 "to" : 1.609937041294E12, 63 "to_as_string" : "2021-01-06T12:44:01.294Z", 64 "doc_count" : 3 65 } 66 ] 67 } 68 } 69}

以上、ご参考までに。

投稿2021/01/06 12:46

Pakio

総合スコア22

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

a0002

2021/01/18 06:46

ご連絡が大変遅れてしまい申し訳ありません。 先ほど、この件のエラーが解決したことを確認いたしました。 アドバイスをありがとうございました
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問