Sorting and latest records in ElasticSearch

て烟熏妆下的殇ゞ 提交于 2019-12-23 18:10:39

问题


I've two questions related to ElasticSearch.

1) Is there any way to specify that I want results with specific field sorted in descending order? An equivalt SQL query will be:

select * from table1 where a="b" order by myprimarykey desc;

2) How to get first and last(latest) record?


回答1:


1) Elasticsearch has quite sophisticated Sorting API that allows you to control sort order. So, in elasticsearch, an equivalent to your MySql query would look like this:

{
    "query" : {
        "term" : { "a" : "b" }
    },
    "sort" : [
        { "myprimarykey" : "desc"} }
    ]
}

Sorting can also be specified on _search URI.

2) To retrieve the first and the last records you would need to perform two searches with desc and asc sort orders and retrieve one record for each. It's possible to combine both queries using Multi Search API.



来源:https://stackoverflow.com/questions/11607808/sorting-and-latest-records-in-elasticsearch

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