本文介绍了ElasticSearch中的排序和最新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个与ElasticSearch有关的问题.

1)有什么方法可以指定我想要特定字段以降序排列的结果吗?等效SQL查询为:

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

2)如何获取第一个和最后一个(最新)记录?

解决方案

1)Elasticsearch具有相当完善的排序API ,可让您控制排序顺序.因此,在elasticsearch中,等效于您的MySql查询如下所示:

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

也可以在_search URI 上指定排序.

2)要检索第一条和最后一条记录,您需要使用descasc排序顺序执行两次搜索,并为每个检索一个记录.可以使用 Multi Search API 组合两个查询. >

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) 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.

这篇关于ElasticSearch中的排序和最新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 10:30