ElasticSearchがタイムスタンプ範囲クエリを実行できない
特定の時間範囲内でクエリを実行する必要があります、
まず第一に、私は次のようなクエリを実行したい
{
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "13000020"
}
},
{
"range": {
"timestampstring": {
"lte": "2020-10-05 15:22:58.537"
}
}
}
]
}
}
}
結果は
{
"took": 15,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 12,
"relation": "eq"
},
"max_score": 2.0,
"hits": [
{
"_index": "test",
"_type": "test12",
"_id": "WvNJl3UBy18_Kc9Pl1tu",
"_score": 2.0,
"_source": {
"hdrId": 13000020,
"timestampstring": "2020-11-05 15:22:58.537",
"DevieId": "624232489",
"type": "data"
}
},
{
"_index": "test",
"_type": "test12",
"_id": "jvOSmHUBy18_Kc9PK3qp",
"_score": 2.0,
"_source": {
"hdrId": 13000020,
"timestamp": 1604582511655,
"timestampstring": "2020-11-05 21:21:51.655",
"type": "data"
}
}
]
}
}
誰かが私が間違っていた部分を特定できますか?
第二に、私はこれで例を行うことができません https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-daterange-aggregation.html
上記の例は私のアプリケーションにどのように適合しますか、ありがとう
ジェフ
現時点で私はPostmanでやろうとしています、これがセットアップです
http:// myip:9200 / test / dev / _searchを取得し、ここでインデックスを作成する必要がありますか?
{
"mappings": {
"properties": {
"timestampstring": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss.SSS"
}
}
}
}
そしてそれが来る
{
"error": {
"root_cause": [
{
"type": "parsing_exception",
"reason": "Unknown key for a START_OBJECT in [mappings].",
"line": 2,
"col": 15
}
],
"type": "parsing_exception",
"reason": "Unknown key for a START_OBJECT in [mappings].",
"line": 2,
"col": 15
},
"status": 400
}
回答
のインデックスマッピングを設定していない可能性がありtimestampstring
ます。日付形式の詳細については、こちらを参照してください
インデックスデータ、マッピング、検索クエリ、検索結果を使用した実用的な例の追加
インデックスマッピング:
{
"mappings": {
"properties": {
"timestampstring": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss.SSS"
}
}
}
}
インデックスデータ:
{
"hdrId": 13000020,
"timestamp": 1604582511655,
"timestampstring": "2020-11-05 21:21:51.655",
"type": "data"
}
{
"hdrId": 13000020,
"timestampstring": "2020-11-05 15:22:58.537",
"DevieId": "624232489",
"type": "data"
}
検索クエリ:
同じ検索クエリを実行すると、目的の結果が得られます
{
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "13000020"
}
},
{
"range": {
"timestampstring": {
"lte": "2020-10-05 15:22:58.537"
}
}
}
]
}
}
}
検索結果:
"hits": []
次の方法で、日付範囲の集計を適用できます。
{
"aggs": {
"range": {
"date_range": {
"field": "timestampstring",
"format": "yyyy-MM-dd HH:mm:ss.SSS",
"ranges": [
{
"to": "now-1M"
},
{
"from": "now-1M"
}
]
}
}
}
}
上記のクエリは2つの範囲バケットを作成します。1つ目は1か月前の日付のすべてのドキュメントを「バケット化」し、2つ目は1か月前の日付のすべてのドキュメントを「バケット化」します。インデックスデータには1か月より前の日付のドキュメントがないためdoc_count
、最初のバケットのは0で、2番目のバケットの日付は2です。
検索結果:
"aggregations": {
"range": {
"buckets": [
{
"key": "*-2020-10-25 10:10:07.665",
"to": 1.603620607665E12,
"to_as_string": "2020-10-25 10:10:07.665",
"doc_count": 0
},
{
"key": "2020-10-25 10:10:07.665-*",
"from": 1.603620607665E12,
"from_as_string": "2020-10-25 10:10:07.665",
"doc_count": 2
}
]
}
}