Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Service
- public class SearchProductsMapper {
- @Autowired
- private ElasticsearchClient esClient;
- @SneakyThrows
- public List<Product> search(SearchProductsDto searchProductsDto) {
- final SearchResponse<Product> response;
- if (searchProductsDto.isRelevance()) {
- if (searchProductsDto.getPriceAsc().equals("price_asc")) {
- response = esClient.search(s -> 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)
- )
- )
- )
- )
- .sort(sort -> sort
- .field(f -> f
- .order(SortOrder.Asc)
- .field("price.p1")
- )
- )
- .highlight(h -> 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("{}")))),
- Product.class);
- return getProducts(response);
- }
- if (searchProductsDto.getPriceDesc().equals("price_desc")) {
- response = esClient.search(s -> 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)
- )
- )
- )
- )
- .sort(sort -> sort
- .field(f -> f
- .order(SortOrder.Desc)
- .field("price.p1")
- )
- )
- .highlight(h -> 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("{}")))),
- Product.class);
- return getProducts(response);
- }
- if (searchProductsDto.getQuantityAsc().equals("quantity_asc")) {
- response = esClient.search(s -> 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)
- )
- )
- )
- )
- .sort(sort -> sort
- .field(f -> f
- .order(SortOrder.Asc)
- .field("quantity.tv")
- )
- )
- .highlight(h -> 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("{}")))),
- Product.class);
- return getProducts(response);
- }
- if (searchProductsDto.getQuantityDesc().equals("quantity_desc")) {
- response = esClient.search(s -> 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)
- )
- )
- )
- )
- .sort(sort -> sort
- .field(f -> f
- .order(SortOrder.Desc)
- .field("quantity.tv")
- )
- )
- .highlight(h -> 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("{}")))),
- Product.class);
- return getProducts(response);
- }
- }
- response = esClient.search(s -> 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)
- )
- )
- )
- )
- .highlight(h -> 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("{}")))),
- Product.class);
- return getProducts(response);
- }
- 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()
- .replaceAll("(\\[|\\])", ""));
- }
- if (hit.highlight().get("vendorArticle") != null) {
- product.setVendorArticle(hit.highlight().get("vendorArticle").toString()
- .replaceAll("(\\[|\\])", ""));
- }
- if (hit.highlight().get("itemCode") != null) {
- product.setItemCode(hit.highlight().get("itemCode").toString()
- .replaceAll("(\\[|\\])", ""));
- }
- return product;
- }).collect(Collectors.toList());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement