ElasticSearch 검색 용례 요약 참조

9219 단어 ElasitcSearch
ElasticSearch 검색 용례 요약 참조
kibana 코드
GET goods/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": {
              "query": "  ",
              "operator": "and"
            }
          }
        }
      ],
      "filter": [
        {
          "terms": {
            "brandId": [
              "1",
              "4"
            ]
          }
        },
        {
          "terms": {
            "categoryId": [
              "225"
            ]
          }
        },
        {
          "nested": {
            "path": "searchAttrs",
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "searchAttrs.attrId": {
                        "value": "4"
                      }
                    }
                  },
                  {
                    "terms": {
                      "searchAttrs.attrValue": [
                        "8G",
                        "7"
                      ]
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "range":{
            "price":{
              "gte":0,
              "lte":5000
            }
          }
        },
        {
          "term":{
            "store":false
          }
        }
      ]
    }
  },
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ],
  "from": 0,
  "size": 10,
  "highlight": {
    "fields": {"title": {}},
    "pre_tags": "",
    "post_tags": ""
  },
    "aggs": {
    "attrAgg": {
      "nested": {
        "path": "searchAttrs"
      },
      "aggs": {
        "attrIdAgg": {
          "terms": {
            "field": "searchAttrs.attrId"
          },
          "aggs": {
            "attrNameAgg": {
              "terms": {
                "field": "searchAttrs.attrName"
              }
            },
            "attrValueAgg": {
              "terms": {
                "field": "searchAttrs.attrValue"
              }
            }
          }
        }
      }
    },
    "brandIdAgg": {
      "terms": {
        "field": "brandId"
      },
      "aggs": {
        "brandNameAgg": {
          "terms": {
            "field": "brandName"
          }
        },
          "brandLogAgg": {
          "terms": {
            "field": "logo"
          }
        }
      }
    },
    "categoryIdAgg": {
      "terms": {
        "field": "categoryId"
      },
      "aggs": {
        "categoryNameAgg": {
          "terms": {
            "field": "categoryName"
          }
        }
      }
    }
  }
}


kibana 페이지 테스트
http://search.gmall.com/search?keyword=%22%E8%8B%B9%E6%9E%9C%22&brandId=1,4&cid=225&priceFrom=0&priceTo=5000&store=false&props=4:8G-12G

대응하는 자바 코드 구현(판단이 완벽하지 않음)
public SearchSourceBuilder buildDsl(SearchParam searchParam) {
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        String keyword = searchParam.getKeyword();
        if (StringUtils.isEmpty(keyword)) {
            return null;
        }
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.matchQuery("title", keyword).operator(Operator.AND));
        List brandId = searchParam.getBrandId();
        if (!CollectionUtils.isEmpty(brandId)) {
            boolQueryBuilder.filter(QueryBuilders.termsQuery("brandId", brandId));
        }
        Long cid = searchParam.getCid();
        if (cid != null) {
            boolQueryBuilder.filter(QueryBuilders.termQuery("categoryId", cid));
        }
        Double priceFrom = searchParam.getPriceFrom();
        Double priceTo = searchParam.getPriceTo();
        if (priceFrom != null || priceTo != null) {
            RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("price");
            if (priceFrom != null) {
                rangeQueryBuilder.gte(priceFrom);
            }
            if (priceTo != null) {
                rangeQueryBuilder.lte(priceTo);
            }
            boolQueryBuilder.filter(rangeQueryBuilder);
        }
        Boolean store = searchParam.getStore();
        if (store != null) {
            boolQueryBuilder.filter(QueryBuilders.termQuery("store", store));
        }


        // 1.2.5.         props=5:  -  &props=6:  865-  1000
        List props = searchParam.getProps();
        if (!CollectionUtils.isEmpty(props)) {
            props.forEach(prop -> {
                String[] attrs = StringUtils.split(prop, ":");
                if (attrs != null && attrs.length == 2) {
                    String attrId = attrs[0];
                    String attrValue = attrs[1];
                    String[] atrValues = StringUtils.split(attrValue, "-");
                    if (atrValues==null){
                      //  System.out.println("   ");
                     //   System.out.println("33333"+attrValue);
                        atrValues=new String[1];
                        atrValues[0]=attrValue;
                       // System.out.println("44444"+atrValues[0]);
                    }
                    BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
                   // System.out.println("1111111111111"+attrId);
                  //  System.out.println("2222222222222"+ Arrays.toString(atrValues));
                    boolQuery.must(QueryBuilders.termQuery("searchAttrs.attrId", attrId));
                    boolQuery.must(QueryBuilders.termsQuery("searchAttrs.attrValue", atrValues));
                    boolQueryBuilder.filter(QueryBuilders.nestedQuery("searchAttrs", boolQuery, ScoreMode.None));
                }
            });
        }
        sourceBuilder.query(boolQueryBuilder);

        Integer sort = searchParam.getSort();
        String field = "";
        SortOrder sortOrder = null;
        switch (sort) {
            case 1:
                field = "price";
                sortOrder = SortOrder.ASC;
                break;
            case 2:
                field = "price";
                sortOrder = SortOrder.DESC;
                break;
            case 3:
                field = "createTime";
                sortOrder = SortOrder.DESC;
                break;
            case 4:
                field = "price";
                sortOrder = SortOrder.DESC;
                break;
            default:
                field = "_score";
                sortOrder = SortOrder.DESC;
                break;
        }
        sourceBuilder.sort(field, sortOrder);

        Integer pageNum = searchParam.getPageNum();
        Integer pageSize = searchParam.getPageSize();
        sourceBuilder.from((pageNum - 1) * pageSize);
        sourceBuilder.size(pageSize);

        sourceBuilder.highlighter(new HighlightBuilder().field("title").preTags("").postTags(""));

        sourceBuilder.aggregation(AggregationBuilders.terms("brandIdAgg").field("brandId").
                subAggregation(AggregationBuilders.terms("brandNameAgg").field("brandName")).
                subAggregation(AggregationBuilders.terms("brandLogAgg").field("logo")));

        sourceBuilder.aggregation(AggregationBuilders.terms("categoryIdAgg").field("categoryId").
                subAggregation(AggregationBuilders.terms("categoryNameAgg").field("categoryName")));

        sourceBuilder.aggregation(AggregationBuilders.nested("attrAgg", "searchAttrs").
                subAggregation(AggregationBuilders.terms("attrIdAgg").field("searchAttrs.attrId")
                .subAggregation(AggregationBuilders.terms("attrNameAgg").field("searchAttrs.attrName"))
                .subAggregation(AggregationBuilders.terms("attrValueAgg").field("searchAttrs.attrValue"))));

        sourceBuilder.fetchSource(new String[]{"skuId","title","price","subTitle","defaultImage"},null );
        System.out.println(sourceBuilder.toString());
        return sourceBuilder;
    }

좋은 웹페이지 즐겨찾기