ElasticSearch query string search date by range in nested object

☆樱花仙子☆ 提交于 2021-02-11 14:16:07

问题


Suppose I have documents that have this kind of structure

{
  "_index": "unittest_repositorydb_iscatalogdata_v2",
  "_type": "product",
  "_id": "Product_100092_In_81810",
  "_score": 2.0794415,
  "_source": {
    "p": {
      "effective_dt": null,
      "code_s_lower": "B19_9394_Exp",
      "expiration_dt": "2020-05-16T00:00:00.0000000Z"
    },
    "catId_s": "fNXXb5CRkpM"
  }
}

What I want to do is search by expiration date using query string. This is the query I do in Kibana:

GET _search
{
  "query": {
    "query_string": {
      "query": "catId_s:fNXXb5CRkpM AND p.expiration_dt:[2020\\-05\\-10T00\\:00\\:00Z TO 2020\\-05\\-17T00\\:00\\:00Z]",
      "fields": [
        "p.code_s_lower^31",
        "p.expiration_dt",
        "p.effective_dt"
      ],
      "lenient": true
    }
  },
  "from": 0,
  "size": 50,
  "_source": [
    "catId_s",
    "p.code_s_lower",
    "p.expiration_dt",
    "p.effective_dt"
  ]
}

But this does not return any results. One thing I found was if I move the dates outside the nested object p and putting them at the same level as catId_s, the search was working. Is there any thing I am doing wrong? Do I need to do something special to search in nested objects?

The version of ES is Version: 5.3.0, Build: 3adb13b/2017-03-23T03:31:50.652Z, JVM: 1.8.0_231


回答1:


If your exp. dt. is of type date, it's recommended to apply a proper range query:

{
  "query":{
    "bool":{
      "must":[
        {
          "nested":{
            "path":"p",
            "query":{
              "bool":{
                "must":[
                  {
                    "range":{
                      "p.expiration_dt":{
                        "gte":"2020-05-10T00:00:00Z",
                        "lte":"2020-05-17T00:00:00Z"
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

As @Val pointed out, the rest of your query string should be split and put into the nested must.



来源:https://stackoverflow.com/questions/62715377/elasticsearch-query-string-search-date-by-range-in-nested-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!