Home > Back-end >  Commonly used on es search search code implementation
Commonly used on es search search code implementation

Time:10-31

Package com. Changgou. Search. Service. Impl;

The import com. Alibaba. Fastjson. JSON;
The import com. Changgou. Search. Pojo. SkuInfo;
The import com. Changgou. Search. Service. SearchService;
The import org.apache.com mons. Lang. StringUtils;
The import org. Elasticsearch. Action. The search. The SearchResponse;
The import org. Elasticsearch. Index. Query. BoolQueryBuilder;
Import org. Elasticsearch. Index. The query. The Operator;
The import org. Elasticsearch. Index. Query. QueryBuilders;
The import org. Elasticsearch. Search. SearchHit;
The import org. Elasticsearch. Search. SearchHits;
The import org. Elasticsearch. Search. Aggregations. Aggregation;
The import org. Elasticsearch. Search. Aggregations. AggregationBuilders;
The import org. Elasticsearch. Search. Aggregations. Bucket. Terms. StringTerms;
The import org. Elasticsearch. Search. The fetch. Subphase. Highlight. HighlightBuilder;
The import org. Elasticsearch. Search. The fetch. Subphase. Highlight. HighlightField;
The import org. Elasticsearch. Search. Sort. SortBuilders;
The import org. Elasticsearch. Search. Sort. SortOrder;
The import org. Springframework. Beans. Factory. The annotation. Autowired;
The import org. Springframework. Data. Domain. Page;
The import org. Springframework. Data. Domain. PageRequest;
The import org. Springframework. Data. Domain. Pageable;
The import org. Springframework. Data. Elasticsearch. Core. ElasticsearchTemplate;
The import org. Springframework. Data. Elasticsearch. Core. SearchResultMapper;
The import org. Springframework. Data. Elasticsearch. Core. Aggregation. AggregatedPage;
The import org. Springframework. Data. Elasticsearch. Core. Aggregation. Impl. AggregatedPageImpl;
The import org. Springframework. Data. Elasticsearch. Core. Query. NativeSearchQueryBuilder;
The import org. Springframework. Stereotype. Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Import the Java. Util. Stream. Collectors;

@ Service
Public class SearchServiceImpl implements SearchService {

The @autowired
Private ElasticsearchTemplate ElasticsearchTemplate;

@ Override
Public Map search (Map SearchMap) {

Map ResultMap=new HashMap<> (a);

//build queries
If (searchMap!=null) {
//build a wrapper object query condition
NativeSearchQueryBuilder NativeSearchQueryBuilder=new NativeSearchQueryBuilder ();
BoolQueryBuilder boolQuery=QueryBuilders. BoolQuery ();

//in accordance with the keyword query
If (StringUtils. IsNotEmpty (searchMap. Get (" keywords "))) {
BoolQuery. Must (QueryBuilders. MatchQuery (" name ", searchMap. Get (" keywords ")). The operator (operator) AND));
}

//in accordance with the brand to filter query
If (StringUtils. IsNotEmpty (searchMap. Get (" brand "))) {
BoolQuery. Filter (QueryBuilders. TermQuery (" brandName ", searchMap. Get (" brand ")));
}

//in accordance with the specifications to filter query
For (String key: searchMap keySet ()) {
If (key. StartsWith (" spec_ ")) {
The String value=https://bbs.csdn.net/topics/searchMap.get (key). The replace (" % 2 b ", "+");
//spec_ network system
BoolQuery. Filter (QueryBuilders. TermQuery ((" specMap. "+ key. The substring (5) +" keyword "), value));
}
}

//carried out in accordance with the price interval filter query
If (StringUtils. IsNotEmpty (searchMap. Get (" price "))) {
String [] prices=searchMap. Get (" price "). The split (" - ");
//0-500 500-1000
If (prices. Length==2) {
BoolQuery. Filter (QueryBuilders. RangeQuery (" price "). The lte (prices [1]));
}
BoolQuery. Filter (QueryBuilders. RangeQuery (" price "). The gte (prices [0]));
}
NativeSearchQueryBuilder. WithQuery (boolQuery);

//in accordance with the brand to group (aggregation) query
String skuBrand="skuBrand";
NativeSearchQueryBuilder. AddAggregation (AggregationBuilders. Terms (skuBrand) field (" brandName "));

//in accordance with the specifications and aggregated query
String skuSpec="skuSpec";
NativeSearchQueryBuilder. AddAggregation (AggregationBuilders. Terms (skuSpec) field (" spec. The keyword "));

//open the paging query
String pageNum=searchMap. Get (" pageNum ");//the current page
String pageSize=searchMap. Get (" pageSize ");//each page shows article how much
{if (StringUtils. IsEmpty (pageNum))
PageNum="1";
}
{if (StringUtils. IsEmpty (pageSize))
PageSize="30";
}
//Settings page
//the first parameter: the current page begins with 0
//the second parameter: each page shows article how much
NativeSearchQueryBuilder. WithPageable (PageRequest) of (Integer parseInt (pageNum) - 1, Integer. The parseInt (pageSize)));

//in accordance with the related fields to sort query
//1. 2. The current domain of the current sort operations (ascending ASC, descending DESC)
If (StringUtils. IsNotEmpty (searchMap. Get (" sortField ")) & amp; & SortRule StringUtils. IsNotEmpty (searchMap. Get (" "))) {
If (" ASC ". The equals (searchMap. Get (" sortRule "))) {
//ascending
NativeSearchQueryBuilder. WithSort (SortBuilders. FieldSort ((searchMap. Get (" sortField "))). The order (SortOrder. ASC));
} else {
//descending
NativeSearchQueryBuilder. WithSort (SortBuilders. FieldSort ((searchMap. Get (" sortField "))). The order (SortOrder. DESC));
}
}

//set Gao Liangyu and highlight the style of the
HighlightBuilder. Field Field=new HighlightBuilder. Field (" name ")//Gao Liangyu
PreTags (" & lt; Span style="color: red '& gt;" )//highlight style prefix
PostTags (" & lt;/span>" ); nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull