如何在Spring Boot NativeSearchQueryBuilder
内的弹性搜索查询中添加“ search_after”(https://www.elastic.co/guide/en/elasticsearch/reference/6.4/search-request-search-after.html)?
答案 0 :(得分:1)
是的,您可以将 NativeSearchQueryBuilder
与自定义查询结合使用以获得所需的结果。
NativeSearchQueryBuilder
不提供在后面添加搜索的方法。
但是您可以使用使用本机构建器构建的查询来发出带有搜索后参数的字符串查询请求。
这是一个例子。
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.EntityMapper;
...
var queryBuilder = new NativeSearchQueryBuilder().withQuery(BoolQueryBuilder.boolQuery());
var jsonQuery = new JSONObject();
jsonQuery.put("size", SEARCH_AFTER_PAGE_SIZE);
jsonQuery.put("query", new JSONObject(queryBuilder.build().getQuery().toString()));
var sortList = new JSONObject();
for (Sort.Order order : sort) {
sortList.put(order.getProperty(), order.getDirection());
}
jsonQuery.put("sort", sortList);
String businessSortMarker = "previous order value 1";
String tiebreakerSortMarker = "previous order value 2";
if (searchAfterParameters != null) {
jsonQuery.put("search_after", Arrays.asList(businessSortMarker, tiebreakerSortMarker));
}
var query = new Search.Builder(searchAfterQuery.toString()).build();
SearchResult result = client.execute(query);
var resultPage = new SearchResultsPage<ResultObject>();
EsEntityMapper documentsMapper = new EsEntityMapper();
searchResult.getHits(JsonObject.class).stream()
.map(hitJson -> hitJson.source)
.map(source -> documentsMapper.mapToObject(searchResultJson.toString(), SearchResult.class))
.collect(Collectors.toList());
//map the documents itself (content)
var resultObject = mapHitsSourceToResultObjects(searchResult);
resultPage.setContent(resultObject);
您可以在此处查看更多详细信息: https://www.shardik.com/blog/2021/03/09/es-deep-search/