需求
根据关键字查询、根据品牌查询、商品类别、商品属性信息、价格区间、是否有库存、排序(销量、价格、 上架时间等)

根据关键字查询
bool多条件查询,
mast相当于and关系,各个条件都满足才行,mast会计算评分
用match是因match分词搜索
POST /product_db/_search{"query": {"bool": {"must": [{"match": {"name": {"query": "手机"}}}]}}}
根据品牌聚合

POST /product_db/_search{"aggs": {"brand_agg": {"terms": {"field": "brandId","size": 10}}}}
buckets就是聚合后的结果,
key就是brandId, doc_count是这个brandId的数量
显示品牌名字和品牌图片
上面的聚合结果没有品牌名字和品牌图片,我需要品牌名字和品牌图片,目前有一种解决方案是用brandId再去数据库查询一遍,还有一种解决方案是子聚合
POST /product_db/_search{"aggs": {"brand_agg": {"terms": {"field": "brandId","size": 10},"aggs": {"brand_name_agg": {"terms": {"field": "brandName"}},"brand_img_agg": {"terms": {"field": "brandImg"}}}}}}
商品类别

先根据品牌id聚合,再根据类目id子聚合
POST /product_db/_search{"aggs": {"brand_agg": {"terms": {"field": "brandId","size": 100}},"category_agg": {"terms": {"field": "categoryId","size": "100"},"aggs": {"category_name_agg": {"terms": {"field": "categoryName"}}}}},"size": 0}

商品属性信息

商品属性是最麻烦的,因为商品属性是动态变化的,
cpu是属性名, 2核 4核 这个是属性值
颜色是属性名, 白色 蓝色 是属性值
POST /product_db/_search{"aggs": {"attr_agg": {"terms": {"field": "attrs.attrId"}}}}
用这种方式聚合查询是聚合查询不出来数据的
用nested的方式去查询
POST /product_db/_search{"aggs": {"attr_agg": {"nested": {"path": "attrs"},"aggs": {"attr_id_agg": {"terms": {"field": "attrs.attrId"},"aggs": {"attr_name_agg": {"terms": {"field": "attrs.attrName"}},"attr_value_agg": {"terms": {"field": "attrs.attrValue"}}}}}}},"size":0}


价格区间
价格区间
POST /product_db/_search{"query": {"bool": {"filter": [{"range":{"price":{"from": 2500,"to": 5000}}}]}}}
是否有库存
不需要评分,就用filter,filter效率高
POST /product_db/_search{"query": {"bool": {"filter": [{"term": {"hasStock": true}}]}}}
排序(销量、价格、 上架时间等)
销量升序用asc, 降序用desc , 价格和上架时间和销量是一样的
POST /product_db/_search{"sort": {"salecount": {"order": "asc"}}}
分页
POST /product_db/_search{"from": 0,"size": 10}
高亮
POST /product_db/_search{"query": {"bool": {"must": [{"match": {"name": {"query": "手机"}}}]}},"highlight": {"pre_tags": ["<b style='color:red'>"],"post_tags": ["</b>"],"fields": {"name": {}}}}
结果:

