【问题标题】:How to pass sort mode in spring data elasticsearch?如何在spring data elasticsearch中传递排序模式?
【发布时间】:2023-03-23 08:38:01
【问题描述】:

spring data Elasticsearch中如何发送排序模式,类似下面的Elasticsearch。

Elasticsearch 搜索查询。

POST /_search
{
   "query" : {
      "term" : { "product" : "chocolate" }
   },
   "sort" : [
      {"price" : {"order" : "asc", "mode" : "avg"}}
   ]
}

我期待看到如下的一些选项 searchQuery.withSort(SortBuilders.fieldSort("fieldName").order(SortOrder.DESC)).???

【问题讨论】:

    标签: elasticsearch spring-data-elasticsearch


    【解决方案1】:

    您在 Spring Data Elasticsearch 中有不同的方式来定义您的搜索查询(请参阅https://docs.spring.io/spring-data/elasticsearch/docs/4.2.1/reference/html/#elasticsearch.operations.queries 的文档)。所有这些查询类都实现了Query 接口,因此有一个方法addSort(Sort sort)

    所以你基本上可以添加

    query.addSort(Sort.by("fieldName").descending());
    

    但是看着你的普通 Elasticsearch 查询,你已经指定了 mode = avg。要使用排序模式,您需要使用 Elasticsearch 构建器将查询定义为 NativeSearchQuery

    NativeSearchQuery query =
            new NativeSearchQueryBuilder()
                .withQuery(termQuery("product", "chocolate"))
                .withSort(SortBuilders
                                    .fieldSort("price")
                                    .order(SortOrder.DESC)
                                    .sortMode(SortMode.AVG))
            .build();
    

    【讨论】:

    • 谢谢,我已经在使用 pageable 来获取分页结果,我看到有一个排序选项可以在 pageable 中发送,我们可以在这里发送排序模式吗? Pageable pageable= PageRequest.of(int page,int size, Sort sort) query = new NativeSearchQueryBuilder() .withQuery(termQuery("product", "chocolate")).withPageable(pageable)
    • 没有。 Sort 中的 Pageable 来自 Spring Data Commons 项目,所有不同的 Spring Data Modules 都使用该项目。我们无法在此处添加特定于 Elasticsearch 的内容。
    猜你喜欢
    • 1970-01-01
    • 2021-04-21
    • 2014-08-19
    • 2019-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-30
    • 2021-12-24
    相关资源
    最近更新 更多