Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Service
- public class SearchProductsMapper {
- private static final String REGEX = "(\\[|\\])";
- @Autowired
- private ElasticsearchClient esClient;
- @SneakyThrows
- public List<Product> search(SearchProductsDto searchProductsDto) {
- final SearchResponse<Product> response;
- if (searchProductsDto.isRelevance()) {
- if (!ObjectUtils.isEmpty(searchProductsDto.getPriceAsc()) && searchProductsDto.getPriceAsc().equals("price_asc")) {
- response = esClient.search(s -> getBuilder(searchProductsDto, s)
- .sort(sort -> getSortObjects(sort, SortOrder.Asc, "price.p1"))
- .highlight(SearchProductsMapper::getFields),
- Product.class);
- return getProducts(response);
- }
- else if (!ObjectUtils.isEmpty(searchProductsDto.getPriceDesc()) && searchProductsDto.getPriceDesc().equals("price_desc")) {
- response = esClient.search(s -> getBuilder(searchProductsDto, s)
- .sort(sort -> getSortObjects(sort, SortOrder.Desc, "price.p1"))
- .highlight(SearchProductsMapper::getFields),
- Product.class);
- return getProducts(response);
- }
- else if (!ObjectUtils.isEmpty(searchProductsDto.getQuantityAsc()) && searchProductsDto.getQuantityAsc().equals("quantity_asc")) {
- response = esClient.search(s -> getBuilder(searchProductsDto, s)
- .sort(sort -> getSortObjects(sort, SortOrder.Asc, "quantity.tv"))
- .highlight(SearchProductsMapper::getFields),
- Product.class);
- return getProducts(response);
- }
- else if (!ObjectUtils.isEmpty(searchProductsDto.getQuantityDesc()) && searchProductsDto.getQuantityDesc().equals("quantity_desc")) {
- response = esClient.search(s -> getBuilder(searchProductsDto, s)
- .sort(sort -> getSortObjects(sort, SortOrder.Desc, "quantity.tv"))
- .highlight(SearchProductsMapper::getFields),
- Product.class);
- return getProducts(response);
- }
- }
- response = esClient.search(s -> getBuilder(searchProductsDto, s)
- .highlight(SearchProductsMapper::getFields),
- Product.class);
- return getProducts(response);
- }
- private static ObjectBuilder<SortOptions> getSortObjects(SortOptions.Builder sort, SortOrder desc, String value) {
- return sort
- .field(f -> f
- .order(desc)
- .field(value)
- );
- }
- private static Highlight.Builder getFields(Highlight.Builder h) {
- return h
- .preTags("[<em><b>]")
- .postTags("[</b></em> ]")
- .fields("productDescription", f -> f.withJson(new StringReader("{}")))
- .fields("vendorArticle", f -> f.withJson(new StringReader("{}")))
- .fields("itemCode", f -> f.withJson(new StringReader("{}")));
- }
- private static SearchRequest.Builder getBuilder(SearchProductsDto searchProductsDto, SearchRequest.Builder s) {
- return s
- .index("product")
- .from(searchProductsDto.getFrom())
- .size(searchProductsDto.getPageSize())
- .query(q -> q
- .bool(bool -> bool
- .must(must -> must
- .multiMatch(t -> t
- .fields("productDescription", "vendorArticle", "itemCode")
- .query(searchProductsDto.getQuery())
- )
- )
- .should(should -> should
- .match(match -> match
- .field("vendorArticle")
- .query(searchProductsDto.getQuery())
- .boost(1.0F)
- )
- )
- .should(should -> should
- .match(match -> match
- .field("itemCode")
- .query(searchProductsDto.getQuery())
- .boost(0.7F)
- )
- )
- .should(should -> should
- .match(match -> match
- .field("inStock")
- .query(true)
- .boost(1.0F)
- )
- )
- )
- );
- }
- private List<Product> getProducts(SearchResponse<Product> response) {
- return response.hits().hits().stream().map(hit -> {
- Product product = hit.source();
- assert product != null;
- if (hit.highlight().get("productDescription") != null) {
- product.setProductDescription(hit.highlight().get("productDescription").toString()
- .replace(REGEX, EMPTY));
- }
- if (hit.highlight().get("vendorArticle") != null) {
- product.setVendorArticle(hit.highlight().get("vendorArticle").toString()
- .replace(REGEX, EMPTY));
- }
- if (hit.highlight().get("itemCode") != null) {
- product.setItemCode(hit.highlight().get("itemCode").toString()
- .replace(REGEX, EMPTY));
- }
- return product;
- }).collect(Collectors.toList());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement