弹出数据弹性搜索,排序不起作用

时间:2017-12-10 04:27:39

标签: elasticsearch spring-data-elasticsearch

我正在使用弹性搜索数据库和弹簧数据。 以下是我正在搜索的文档,搜索结果应返回已排序可分页列表。

@Data
@EqualsAndHashCode(exclude = { "id" })
@Document(indexName = "job", type = "job")
public class JobDocument implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    private String id;

    @Field(type = FieldType.String, index = FieldIndex.analyzed, store = true)
    private String name;

    @Field(type = FieldType.String, index = FieldIndex.analyzed, store = true)
    private String desc;

    @Field(type = FieldType.Date, store = true)
    private LocalDateTime dateTime; // java.Time
}

我正在desc字段中搜索文字,并希望按dateTime字段对其进行排序。我的搜索服务确实如下,

BoolQueryBuilder queryBuilder = boolQuery();
queryBuilder.must(QueryBuilders.queryStringQuery("*" + desc + "*").lenient(true).field("desc"));
NativeSearchQueryBuilder searchQuery = new NativeSearchQueryBuilder();
searchQuery.withPageable(pageable);
searchQuery.withSort(SortBuilders.fieldSort("dateTime")
.order(SortOrder.DESC))
searchQuery.withQuery(queryBuilder);
Page<JobDocument> jobs = jobRepo.search(searchQuery.build());

以下是我的回购,

public interface JobDAO extends ElasticsearchRepository<JobDocument, String>
{}

搜索和分页正在运行,但排序无效。 我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

它们是可分页对象中可用的排序选项。您可以将它们添加到单个对象PageRequest中,而不必分别使用排序和分页。

BoolQueryBuilder queryBuilder = boolQuery();
queryBuilder.must(QueryBuilders.queryStringQuery("*" + desc + "*").lenient(true).field("desc"));
NativeSearchQueryBuilder searchQuery = new NativeSearchQueryBuilder();
searchQuery.withPageable(new PageRequest(0, 10, Sort.Direction.DESC, "dateTime"));
searchQuery.withQuery(queryBuilder);