嘿我需要设置默认搜索弹性搜索排序结果的最高精度如何使用弹簧数据弹性搜索,弹簧数据启动实现它?
以下是我想添加此内容的示例代码:
boolean issetPriceFrom = Optional.ofNullable(searchParams.getPriceFrom()).isPresent();
boolean issetPriceTo = Optional.ofNullable(searchParams.getPriceTo()).isPresent();
final List<FilterBuilder> filters = Lists.newArrayList();
final NativeSearchQueryBuilder searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery());
Optional.ofNullable(searchParams.getBuyer()).ifPresent(v -> filters.add(boolFilter().must(termFilter("buyer", v))));
Optional.ofNullable(searchParams.getCat()).ifPresent(v -> filters.add(boolFilter().must(termFilter("cat", v))));
Optional.ofNullable(searchParams.getComment_type()).ifPresent(v -> filters.add(boolFilter().must(termFilter("comment_type", v))));
Optional.ofNullable(searchParams.getItem()).ifPresent(v -> filters.add(boolFilter().must(termFilter("item", v))));
Optional.ofNullable(searchParams.getSeller()).ifPresent(v -> filters.add(boolFilter().must(termFilter("seller", v))));
Optional.ofNullable(searchParams.getTree_cat()).ifPresent(v -> filters.add(boolFilter().must(termFilter("tree_cat", v))));
final BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
//access for many sellers
if (searchParams.getSellers() != null) {
// Optional.ofNullable(searchParams.getSellers().split(",")).ifPresent(v -> {
// filters.add(boolFilter().must(termsFilter("seller", v)));
// });
for (String user : searchParams.getSellers().split(",")) {
boolQueryBuilder.must(queryStringQuery(user).field("seller"));
}
}
//access for many categories
if (searchParams.getCats() != null) {
// Optional.ofNullable(searchParams.getCats().split(",")).ifPresent(v -> {
// filters.add(boolFilter().must(termsFilter("cat", v)));
// });
for (String category : searchParams.getCats().split(",")) {
boolQueryBuilder.must(queryStringQuery(category).field("cat"));
}
}
if (Optional.ofNullable(searchParams.getTitle()).isPresent()) {
boolQueryBuilder.must(queryStringQuery(searchParams.getTitle()).analyzeWildcard(true).field("title"));
}
if (Optional.ofNullable(searchParams.getComment_text()).isPresent()) {
boolQueryBuilder.must(queryStringQuery(searchParams.getComment_text()).analyzeWildcard(true).field("comment_text"));
}
if (issetPriceFrom || issetPriceTo) {
filters.add(rangeFilter("price").from(searchParams.getPriceFrom()).to(searchParams.getPriceTo()));
}
if (Optional.ofNullable(searchParams.getTsFrom()).isPresent()
|| Optional.ofNullable(searchParams.getTsTo()).isPresent()) {
filters.add(rangeFilter("ts").from(searchParams.getTsFrom()).to(searchParams.getTsTo()));
}
if (hasComments != null && hasComments.equals(true)) {
filters.add(FilterBuilders.boolFilter().must(FilterBuilders.existsFilter("comment_text")));
}
if (hasComments != null && hasComments.equals(false)) {
filters.add(FilterBuilders.boolFilter().must(FilterBuilders.missingFilter("comment_text")));
}
searchQuery.withQuery(boolQueryBuilder);
FilterBuilder[] filterArr = new FilterBuilder[filters.size()];
filterArr = filters.toArray(filterArr);
searchQuery.withFilter(andFilter(filterArr));
if (pageable != null) {
searchQuery.withPageable(pageable);
}
return searchQuery;
如果我得到一段可以实现的代码,我将不胜感激。
谢谢!
答案 0 :(得分:1)
你只需要使用withSort
方法。使用它:
SortBuilder sortBuilder = SortBuilders.fieldSort(fieldName).order(SortOrder.ASC)
NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
.withSort(sortBuilder)
希望这会有所帮助..