Search for exact field in an array of strings in elasticsearch with sorting via latest publication date

折月煮酒 提交于 2020-06-28 05:01:37

问题


This is a follow-up question of exact string search in an array of strings.

There is another thing if the document has a publishing date.

{
   "datePublished":"2020-05-22T15:06:00.000Z",
   "locations":[
      "Landkreis Cloppenburg",
      "Berlin"
   ]
}

and sort them with the latest publication date. Then the result is coming back with the Landkreis Cloppenburg if I have this in the query:

"sort": [
   {
      "doc.datePublished":{
         "order":"desc"
      }
   }
]

回答1:


the correct query should be like below

{
    "sort": [
        {
            "datePublished": {
                "order": "desc"
            }
        }
    ],
    "query": {
        "term": {
            "location": {
                "value": "Cloppenburg"
            }
        }
    }
}

As the previous answer was written by me, I just enhanced and included your date field, so my mapping looks like

{
    "mappings": {
        "properties": {
            "location": {
                "type": "keyword"
            },
            "datePublished" : {
                "type" : "date"
            }
        }
    }
}


来源:https://stackoverflow.com/questions/62263201/search-for-exact-field-in-an-array-of-strings-in-elasticsearch-with-sorting-via

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