Theme NexT works best with JavaScript enabled
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 @Override public SearchResult search (SearchParam searchParam) { try { SearchRequest.Builder s = new SearchRequest .Builder(); 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))); } 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>" ))); } 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @Bean public ElasticsearchClient getElasticsearchClient () { RestClient restClient = RestClient.builder(new HttpHost ("localhost" , 9200 , "http" )).build(); ElasticsearchTransport transport = new RestClientTransport (restClient, new JacksonJsonpMapper ()); return new ElasticsearchClient (transport); }