Bei der elastischen Suche kann keine Zeitstempelbereichsabfrage durchgeführt werden

Nov 25 2020

Ich muss eine Abfrage innerhalb eines bestimmten Zeitraums durchführen.

Zunächst möchte ich eine Abfrage wie machen

    {
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "13000020"
          }
        },
        {
          "range": {
            "timestampstring": {
              "lte": "2020-10-05 15:22:58.537"
            }
          }
        }

      ]
    }
  }
}

und das Ergebnis war

{
    "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"
                }
            }
        ]
    }
}

Kann jemand genau sagen, welchen Teil ich falsch gemacht habe?

zweitens schaffe ich das Beispiel hier nicht https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-daterange-aggregation.html

Wie kann das obige Beispiel zu meiner Bewerbung passen, danke

Jeff

In diesem Moment versuche ich in Postman zu tun, hier ist das Setup

GET http: // myip: 9200 / test / dev / _search und muss ich hier den Index erstellen?

{
  "mappings": {
    "properties": {
      "timestampstring": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss.SSS"
      }
    }
  }
}

und es kommt

{
    "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
}

Antworten

2 ESCoder Nov 25 2020 at 10:00

Möglicherweise haben Sie die Indexzuordnung für nicht festgelegttimestampstring . Weitere Informationen zu Datumsformaten finden Sie hier

Hinzufügen eines Arbeitsbeispiels mit Indexdaten, Zuordnung, Suchabfrage und Suchergebnis

Indexzuordnung:

{
  "mappings": {
    "properties": {
      "timestampstring": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss.SSS"
      }
    }
  }
}

Indexdaten:

{
  "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"
}

Suchanfrage:

Wenn Sie jetzt dieselbe Suchabfrage ausführen, erhalten Sie das gewünschte Ergebnis

{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "13000020"
          }
        },
        {
          "range": {
            "timestampstring": {
              "lte": "2020-10-05 15:22:58.537"
            }
          }
        }
      ]
    }
  }
}

Suchergebnis:

"hits": []

Sie können die Datumsbereichsaggregation folgendermaßen anwenden :

{
  "aggs": {
    "range": {
      "date_range": {
        "field": "timestampstring",
        "format": "yyyy-MM-dd HH:mm:ss.SSS",
        "ranges": [
          {
            "to": "now-1M"       
          },
          {
            "from": "now-1M"
          }
        ]
      }
    }
  }
}

Die obige Abfrage erstellt zwei Bereichsbereiche, wobei der erste alle Dokumente, die vor einem Monat datiert wurden, "zusammenfasst" und der zweite alle Dokumente, die vor einem Monat datiert wurden, "zusammenfasst". Da in den Indexdaten kein Dokument vorhanden ist, das vor einem Monat datiert ist, ist der doc_countdes ersten Buckets 0 und der des zweiten Buckets 2

Suchergebnis:

"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
        }
      ]
    }
  }