탄력적 검색이 타임 스탬프 범위 쿼리를 수행하지 못함

Nov 25 2020

특정 시간 범위 내에서 쿼리를해야합니다.

우선, 다음과 같은 쿼리를하고 싶습니다.

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

답변

2 ESCoder Nov 25 2020 at 10:00

에 대한 인덱스 매핑을 설정하지 않았을 수 있습니다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"
          }
        ]
      }
    }
  }
}

위의 쿼리는 두 개의 범위 버킷을 생성합니다. 첫 번째는 1 개월 이전의 모든 문서를 "버킷"하고 두 번째 쿼리는 1 개월 전 이후의 모든 문서를 "버킷"합니다. 인덱스 데이터에는 1 개월 이전의 문서가 없기 때문에 doc_count첫 번째 버킷은 0이고 두 번째 버킷은 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
        }
      ]
    }
  }