如何向 spring 数据 elasticsearch 添加排序
How add sorting to spring data elasticsearch
我正在使用 elasticsearch spring 数据编写 api,我想添加 ordering.I 无法在 google 中找到解决方案。所以我写在这里是想问问你们我该怎么做。如果您需要更多代码,请告诉我您需要什么,我会尝试添加更多代码。
我的代码如下:
AuctionQueryController.java
@RequestMapping(value = "/auctions/search", produces = MediaType.APPLICATION_JSON_VALUE)
private List<Auction> search(
@RequestParam(value = "categoryId", required = false) Long categoryId,
@RequestParam(value = "treeCategoryId", required = false) Long treeCategoryId,
@RequestParam(value = "currency", required = false) String currency,
@RequestParam(value = "priceFrom", required = false) Long priceFrom,
@RequestParam(value = "priceTo", required = false) Long priceTo,
@RequestParam(value = "startDateFrom", required = false) Long startDateFrom,
@RequestParam(value = "startDateTo", required = false) Long startDateTo,
@RequestParam(value = "endDateFrom", required = false) Long endDateFrom,
@RequestParam(value = "endDateTo", required = false) Long endDateTo,
@RequestParam(value = "title", required = false) String title,
@RequestParam(value = "uid", required = false) Long uid,
Pageable pageable) {
final AuctionIndexSearchParams searchParams = AuctionIndexSearchParams.builder()
.categoryId(categoryId)
.treeCategoryId(treeCategoryId)
.currency(currency)
.priceFrom(priceFrom)
.priceTo(priceTo)
.startDateFrom(startDateFrom)
.startDateTo(startDateTo)
.endDateFrom(endDateFrom)
.endDateTo(endDateTo)
.title(title)
.uid(uid)
.build();
return auctionService.searchByIndexParams(searchParams, pageable);
}
AuctionService.java
public List<Auction> searchByIndexParams(AuctionIndexSearchParams searchParams, Pageable pageable) {
final List<FilterBuilder> filters = Lists.newArrayList();
final NativeSearchQueryBuilder searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery());
Optional.ofNullable(searchParams.getCategoryId()).ifPresent(v -> filters.add(boolFilter().must(termFilter("cat", v))));
Optional.ofNullable(searchParams.getCurrency()).ifPresent(v -> filters.add(boolFilter().must(termFilter("curr", v))));
Optional.ofNullable(searchParams.getTreeCategoryId()).ifPresent(v -> filters.add(boolFilter().must(termFilter("tcat", v))));
Optional.ofNullable(searchParams.getUid()).ifPresent(v -> filters.add(boolFilter().must(termFilter("uid", v))));
final BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
if (Optional.ofNullable(searchParams.getTitle()).isPresent()) {
boolQueryBuilder.should(queryStringQuery(searchParams.getTitle()).analyzeWildcard(true).field("title"));
}
if (Optional.ofNullable(searchParams.getStartDateFrom()).isPresent()
&& Optional.ofNullable(searchParams.getStartDateTo()).isPresent()) {
filters.add(rangeFilter("start_date").from(searchParams.getStartDateFrom()).to(searchParams.getStartDateTo()));
}
if (Optional.ofNullable(searchParams.getEndDateFrom()).isPresent()
&& Optional.ofNullable(searchParams.getEndDateTo()).isPresent()) {
filters.add(rangeFilter("end_date").from(searchParams.getEndDateFrom()).to(searchParams.getEndDateTo()));
}
if (Optional.ofNullable(searchParams.getPriceFrom()).isPresent()
&& Optional.ofNullable(searchParams.getPriceTo()).isPresent()) {
filters.add(rangeFilter("price").from(searchParams.getPriceFrom()).to(searchParams.getPriceTo()));
}
searchQuery.withPageable(pageable);
searchQuery.withQuery(boolQueryBuilder);
FilterBuilder[] filterArr = new FilterBuilder[filters.size()];
filterArr = filters.toArray(filterArr);
searchQuery.withFilter(andFilter(filterArr));
final FacetedPage<AuctionIndex> search = auctionIndexRepository.search(searchQuery.build());
return search.map(index ->
auctionRepository.findAuctionById(Long.valueOf(index.getId()))
).getContent();
}
您可以使用 SortBuilders
来实现。
searchQuery.withSort(SortBuilders.fieldSort("fieldName").order(SortOrder.DESC))
这将根据 fieldName
通过对结果进行排序。
希望对您有所帮助
我正在使用 elasticsearch spring 数据编写 api,我想添加 ordering.I 无法在 google 中找到解决方案。所以我写在这里是想问问你们我该怎么做。如果您需要更多代码,请告诉我您需要什么,我会尝试添加更多代码。
我的代码如下:
AuctionQueryController.java
@RequestMapping(value = "/auctions/search", produces = MediaType.APPLICATION_JSON_VALUE)
private List<Auction> search(
@RequestParam(value = "categoryId", required = false) Long categoryId,
@RequestParam(value = "treeCategoryId", required = false) Long treeCategoryId,
@RequestParam(value = "currency", required = false) String currency,
@RequestParam(value = "priceFrom", required = false) Long priceFrom,
@RequestParam(value = "priceTo", required = false) Long priceTo,
@RequestParam(value = "startDateFrom", required = false) Long startDateFrom,
@RequestParam(value = "startDateTo", required = false) Long startDateTo,
@RequestParam(value = "endDateFrom", required = false) Long endDateFrom,
@RequestParam(value = "endDateTo", required = false) Long endDateTo,
@RequestParam(value = "title", required = false) String title,
@RequestParam(value = "uid", required = false) Long uid,
Pageable pageable) {
final AuctionIndexSearchParams searchParams = AuctionIndexSearchParams.builder()
.categoryId(categoryId)
.treeCategoryId(treeCategoryId)
.currency(currency)
.priceFrom(priceFrom)
.priceTo(priceTo)
.startDateFrom(startDateFrom)
.startDateTo(startDateTo)
.endDateFrom(endDateFrom)
.endDateTo(endDateTo)
.title(title)
.uid(uid)
.build();
return auctionService.searchByIndexParams(searchParams, pageable);
}
AuctionService.java
public List<Auction> searchByIndexParams(AuctionIndexSearchParams searchParams, Pageable pageable) {
final List<FilterBuilder> filters = Lists.newArrayList();
final NativeSearchQueryBuilder searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery());
Optional.ofNullable(searchParams.getCategoryId()).ifPresent(v -> filters.add(boolFilter().must(termFilter("cat", v))));
Optional.ofNullable(searchParams.getCurrency()).ifPresent(v -> filters.add(boolFilter().must(termFilter("curr", v))));
Optional.ofNullable(searchParams.getTreeCategoryId()).ifPresent(v -> filters.add(boolFilter().must(termFilter("tcat", v))));
Optional.ofNullable(searchParams.getUid()).ifPresent(v -> filters.add(boolFilter().must(termFilter("uid", v))));
final BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
if (Optional.ofNullable(searchParams.getTitle()).isPresent()) {
boolQueryBuilder.should(queryStringQuery(searchParams.getTitle()).analyzeWildcard(true).field("title"));
}
if (Optional.ofNullable(searchParams.getStartDateFrom()).isPresent()
&& Optional.ofNullable(searchParams.getStartDateTo()).isPresent()) {
filters.add(rangeFilter("start_date").from(searchParams.getStartDateFrom()).to(searchParams.getStartDateTo()));
}
if (Optional.ofNullable(searchParams.getEndDateFrom()).isPresent()
&& Optional.ofNullable(searchParams.getEndDateTo()).isPresent()) {
filters.add(rangeFilter("end_date").from(searchParams.getEndDateFrom()).to(searchParams.getEndDateTo()));
}
if (Optional.ofNullable(searchParams.getPriceFrom()).isPresent()
&& Optional.ofNullable(searchParams.getPriceTo()).isPresent()) {
filters.add(rangeFilter("price").from(searchParams.getPriceFrom()).to(searchParams.getPriceTo()));
}
searchQuery.withPageable(pageable);
searchQuery.withQuery(boolQueryBuilder);
FilterBuilder[] filterArr = new FilterBuilder[filters.size()];
filterArr = filters.toArray(filterArr);
searchQuery.withFilter(andFilter(filterArr));
final FacetedPage<AuctionIndex> search = auctionIndexRepository.search(searchQuery.build());
return search.map(index ->
auctionRepository.findAuctionById(Long.valueOf(index.getId()))
).getContent();
}
您可以使用 SortBuilders
来实现。
searchQuery.withSort(SortBuilders.fieldSort("fieldName").order(SortOrder.DESC))
这将根据 fieldName
通过对结果进行排序。
希望对您有所帮助