当我尝试将来自elasticsearch的响应放入具有新对象名称的新列表时,结果不正确。当我添加点击列表结果是正确的是怎么回事?我应该怎么解决?任何想法我应该做什么?
不正确意味着总结果不同且不正确。
那是不正确的回应:
public Map findAllByIndexParams(TransIndexSearchParams searchParams) {
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)));
});
}
//access for many categories
if (searchParams.getCats() != null) {
Optional.ofNullable(searchParams.getCats().split(",")).ifPresent(v -> {
filters.add(boolFilter().must(termsFilter("cat", v)));
});
}
if (Optional.ofNullable(searchParams.getTitle()).isPresent()) {
boolQueryBuilder.should(queryStringQuery(searchParams.getTitle()).analyzeWildcard(true).field("title"));
}
if (Optional.ofNullable(searchParams.getComment_text()).isPresent()) {
boolQueryBuilder.should(queryStringQuery(searchParams.getComment_text()).analyzeWildcard(true).field("comment_text"));
}
if (Optional.ofNullable(searchParams.getPriceFrom()).isPresent()
|| Optional.ofNullable(searchParams.getPriceTo()).isPresent()) {
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()));
}
searchQuery.withQuery(boolQueryBuilder);
FilterBuilder[] filterArr = new FilterBuilder[filters.size()];
filterArr = filters.toArray(filterArr);
searchQuery.withFilter(andFilter(filterArr));
List<TransIndex> rowsTrans = new ArrayList();
List rows = new ArrayList();
Map response = new HashMap();
String scrollId = searchTemplate.scan(searchQuery.build(), 1000, false);
Page<TransIndex> page = searchTemplate.scroll(scrollId, 5000L, TransIndex.class);
if (page.hasContent()) {
while (true) {
for (TransIndex hit : page.getContent()) {
Map row = new HashMap();
row.put("endts", auctionRepository.findTransactionAuctionById(hit.getItem(), hit.getTransactionIndexId()).get("endts"));
row.put("startts", auctionRepository.findTransactionAuctionById(hit.getItem(), hit.getTransactionIndexId()).get("startts"));
row.put("qty", auctionRepository.findTransactionAuctionById(hit.getItem(), hit.getTransactionIndexId()).get("qty"));
row.put("price", auctionRepository.findTransactionAuctionById(hit.getItem(), hit.getTransactionIndexId()).get("price"));
row.put("buyer", hit.getBuyer());
row.put("cat", hit.getCat());
row.put("comment_text", hit.getComment_text());
row.put("comment_type", hit.getComment_type());
row.put("item", hit.getItem());
row.put("seller", hit.getSeller());
row.put("title", hit.getTitle());
row.put("transactionIndexId", hit.getTransactionIndexId());
row.put("tree_cat", hit.getTree_cat());
row.put("ts", hit.getTs());
rows.add(row);
}
page = searchTemplate.scroll(scrollId, 5000L, TransIndex.class);
if (page.hasContent() == false) {
break;
}
}
}
response.put("content", rows);
return response;
}
这是正确的:
public Map findAllByIndexParams(TransIndexSearchParams searchParams) {
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)));
});
}
//access for many categories
if (searchParams.getCats() != null) {
Optional.ofNullable(searchParams.getCats().split(",")).ifPresent(v -> {
filters.add(boolFilter().must(termsFilter("cat", v)));
});
}
if (Optional.ofNullable(searchParams.getTitle()).isPresent()) {
boolQueryBuilder.should(queryStringQuery(searchParams.getTitle()).analyzeWildcard(true).field("title"));
}
if (Optional.ofNullable(searchParams.getComment_text()).isPresent()) {
boolQueryBuilder.should(queryStringQuery(searchParams.getComment_text()).analyzeWildcard(true).field("comment_text"));
}
if (Optional.ofNullable(searchParams.getPriceFrom()).isPresent()
|| Optional.ofNullable(searchParams.getPriceTo()).isPresent()) {
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()));
}
searchQuery.withQuery(boolQueryBuilder);
FilterBuilder[] filterArr = new FilterBuilder[filters.size()];
filterArr = filters.toArray(filterArr);
searchQuery.withFilter(andFilter(filterArr));
List<TransIndex> rowsTrans = new ArrayList();
List rows = new ArrayList();
Map response = new HashMap();
String scrollId = searchTemplate.scan(searchQuery.build(), 1000, false);
Page<TransIndex> page = searchTemplate.scroll(scrollId, 5000L, TransIndex.class);
if (page.hasContent()) {
while (true) {
for (TransIndex hit : page.getContent()) {
rows.add(hit);
}
page = searchTemplate.scroll(scrollId, 5000L, TransIndex.class);
if (page.hasContent() == false) {
break;
}
}
}
response.put("content", rows);
return response;
}
接下来的问题是如何加快搜索速度?当我获得许多记录时,它已经搜索了很长时间,例如500条记录发现&gt; 30秒。