/**
* 根据页面传入数据去es中检索结果
*
* @param searchParam 搜索参数封装
* @return SearchResult 里面包含页面需要的所有信息
*/
@Override
public SearchResult search(SearchParam searchParam) {
//动态构建检索语句
try {
SearchRequest.Builder s = new SearchRequest.Builder();
// Function<SearchRequest.Builder, ObjectBuilder<SearchRequest>> fn = s -> {
//查询的索引
s.index(ElasticSearchConstant.PRODUCT_INDEX);
//查询条件
s.query(q -> q
.bool(bool -> {
if (StringUtils.hasText(searchParam.getKeyword())) {
bool.must(must -> must
.match(match -> match.field(ElasticSearchConstant.PRODUCT_INDEX).query(searchParam.getKeyword()))
);
}
bool.filter(filter -> {
if (searchParam.getCatalog3Id() != null) {
filter.term(term -> term
.field("catalogId")
.value(searchParam.getCatalog3Id()));
} else if (searchParam.getBrandId() != null && searchParam.getBrandId().size() > 0) {
List<FieldValue> collect = searchParam.getBrandId().stream().map(FieldValue::of).collect(Collectors.toList());
filter.terms(terms -> terms
.field("brandId")
.terms(termsValue -> termsValue
.value(collect)));
} else if (StringUtils.hasText(searchParam.getSkuPrice())) {
filter.range(range -> {
String skuPrice = searchParam.getSkuPrice();
String[] skuRange = skuPrice.split("_");
if (skuRange.length == 2) {
range.field("skuPrice").gte(JsonData.of(skuRange[0])).lte(JsonData.of(skuRange[1]));
} else if (skuRange.length == 1) {
if (skuPrice.startsWith("_")) {
range.field("skuPrice").lte(JsonData.of(skuRange[0]));
} else {
range.field("skuPrice").gte(JsonData.of(skuRange[0]));
}
}
return range;
});
} else if (searchParam.getAttrs() != null && searchParam.getAttrs().size() > 0) {
List<String> attrs = searchParam.getAttrs();
for (String attr : attrs) {
String[] attrString = attr.split("_");
filter.nested(nested -> nested
.scoreMode(ChildScoreMode.None)
.path("attrs")
.query(query -> query.bool(attrBool -> attrBool
.must(mustAttrs -> {
mustAttrs.term(termAttrId -> termAttrId
.field("attrs.attrId")
.value(attrString[0]));
mustAttrs.term(termAttrValue -> termAttrValue
.field("attrs.attrValue")
.value(attrString[1]));
return mustAttrs;
}
))
));
}
}
filter.term(term -> term
.field("hasStock")
.value(searchParam.getHasStock() == 1));
return filter;
});
return bool;
})
);

//排序
if (StringUtils.hasText(searchParam.getSort())) {
String[] sortString = searchParam.getSort().split("_");
s.sort(sort -> sort
.field(field -> field.field(sortString[0]).order(sortString[1].equalsIgnoreCase("asc") ? SortOrder.Asc : SortOrder.Desc)));
}
//分页 (起始-1) * 页面大小
s.from((searchParam.getPageNum() - 1) * ElasticSearchConstant.PRODUCT_PAGE_SIZE);
s.size(ElasticSearchConstant.PRODUCT_PAGE_SIZE);
//高亮
if (StringUtils.hasText(searchParam.getKeyword())) {
s.highlight(h -> h
.fields(searchParam.getKeyword(), highlight -> highlight
.preTags("<b style=-'color:red'>")
.postTags("</b>")));
}
// return s;
// };
SearchRequest searchRequest = s.build();
Object o = JSON.toJSON(searchRequest);
SearchResponse<SearchResult> search = elasticsearchClient.search(searchRequest,SearchResult.class);
String jsonString = JSON.toJSONString(searchParam);
System.out.println(jsonString);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

elasticsearch java api


/**
* 注入一个elasticsearch clent客户端
*
* @return ElasticsearchClient 返回elasticsearch客户端
*/
@Bean
public ElasticsearchClient getElasticsearchClient() {
// Create the low-level client
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200, "http")).build();
// Create the transport with a Jackson mapper
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
// And create the API client
return new ElasticsearchClient(transport);
}