复杂查询
查询api
Search search = new Search.Builder("dsl的json语句").addIndex("索引名").addType("表名").build();
dsl查询工具类
1 @Service 2 public class SearchServiceImpl implements SearchService{ 3 4 @Autowired 5 JestClient jestClient; 6 7 @Override 8 public Listlist(PmsSearchParam pmsSearchParam) { 9 String dslStr = getSearchDsl(pmsSearchParam); 10 System.err.println(dslStr); 11 // 用api执行复杂查询 12 List pmsSearchSkuInfos = new ArrayList<>(); 13 Search search = new Search.Builder(dslStr).addIndex("gmall0105").addType("PmsSkuInfo").build(); 14 SearchResult execute = null; 15 try { 16 execute = jestClient.execute(search); 17 } catch (IOException e) { 18 e.printStackTrace(); 19 } 20 List > hits = execute.getHits(PmsSearchSkuInfo.class); 21 for (SearchResult.Hit hit : hits) { 22 PmsSearchSkuInfo source = hit.source; 23 Map > highlight = hit.highlight; 24 if(highlight!=null){ 25 String skuName = highlight.get("skuName").get(0); 26 source.setSkuName(skuName); 27 } 28 29 pmsSearchSkuInfos.add(source); 30 } 31 32 System.out.println(pmsSearchSkuInfos.size()); 33 return pmsSearchSkuInfos; 34 } 35 36 private String getSearchDsl(PmsSearchParam pmsSearchParam) { 37 38 String[] skuAttrValueList = pmsSearchParam.getValueId(); 39 String keyword = pmsSearchParam.getKeyword(); 40 String catalog3Id = pmsSearchParam.getCatalog3Id(); 41 42 // jest的dsl工具 43 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); 44 // bool 45 BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder(); 46 47 // filter 48 if(StringUtils.isNotBlank(catalog3Id)){ 49 TermQueryBuilder termQueryBuilder = new TermQueryBuilder("catalog3Id",catalog3Id); 50 boolQueryBuilder.filter(termQueryBuilder); 51 } 52 if(skuAttrValueList!=null){ 53 for (String pmsSkuAttrValue : skuAttrValueList) { 54 TermQueryBuilder termQueryBuilder = new TermQueryBuilder("skuAttrValueList.valueId",pmsSkuAttrValue); 55 boolQueryBuilder.filter(termQueryBuilder); 56 } 57 } 58 59 // must 60 if(StringUtils.isNotBlank(keyword)){ 61 MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("skuName",keyword); 62 boolQueryBuilder.must(matchQueryBuilder); 63 } 64 65 // query 66 searchSourceBuilder.query(boolQueryBuilder); 67 68 // highlight 69 HighlightBuilder highlightBuilder = new HighlightBuilder(); 70 highlightBuilder.preTags(""); 71 highlightBuilder.field("skuName"); 72 highlightBuilder.postTags(""); 73 searchSourceBuilder.highlight(highlightBuilder); 74 // sort 75 searchSourceBuilder.sort("id",SortOrder.DESC); 76 // from 77 searchSourceBuilder.from(0); 78 // size 79 searchSourceBuilder.size(20); 80 81 82 //aggs
//es中使用aggregations聚合函数抽取平台属性
83 TermsBuilder groupby_attr = AggregationBuilders.terms("groupby_attr").field("skuAttrValueList.valueId"); 84 searchSourceBuilder.aggregation(groupby_attr); 85 86 return searchSourceBuilder.toString(); 87 88 } 89 }
mysql数据导入es
1 //1.查询mysql数据 2 ListpmsSkuInfoList = new ArrayList<>(); 3 4 5 pmsSkuInfoList = skuService.getAllSku("107"); 6 7 List catalog3Id = skuService.getAllSku("catalog3Id"); 8 9 //2.转换成es的数据结构 10 List pmsSearchSkuInfos = new ArrayList<>(); 11 12 for (PmsSkuInfo pmsSkuInfo : pmsSkuInfoList) { 13 PmsSearchSkuInfo pmsSearchSkuInfo = new PmsSearchSkuInfo(); 14 15 BeanUtils.copyProperties(pmsSkuInfo, pmsSearchSkuInfo); 16 17 pmsSearchSkuInfos.add(pmsSearchSkuInfo); 18 } 19 20 // 导入es 21 for (PmsSearchSkuInfo pmsSearchSkuInfo : pmsSearchSkuInfos) { 22 Index put = new Index.Builder(pmsSearchSkuInfo).index("gmall0105").type("PmsSkuInfo").id(pmsSearchSkuInfo.getId() + "").build(); 23 jestClient.execute(put); 24 }