1. 新建es映射关系
PUT product
{
"mappings" : {
"properties" : {
"attrs" : {
"type" : "nested",
"properties" : {
"attrId" : {
"type" : "long"
},
"attrName" : {
"type" : "keyword"
},
"attrValue" : {
"type" : "keyword"
}
}
},
"brandId" : {
"type" : "long"
},
"brandImg" : {
"type" : "keyword"
},
"brandName" : {
"type" : "keyword"
},
"catalogId" : {
"type" : "long"
},
"catalogName" : {
"type" : "keyword"
},
"hasStock" : {
"type" : "boolean"
},
"hotScore" : {
"type" : "long"
},
"saleCount" : {
"type" : "long"
},
"skuId" : {
"type" : "long"
},
"skuImg" : {
"type" : "keyword"
},
"skuPrice" : {
"type" : "keyword"
},
"skuTitle" : {
"type" : "text",
"analyzer" : "ik_smart"
},
"spuId" : {
"type" : "keyword"
}
}
}
}
2. 查询
复杂的查询有以下几部分组成 检索关键字query、排序关键字sort,分页条件,高亮显示,聚合分析等
2.1 query
query查询的是一些检索匹配信息,对于关键字也就是全文检索的查询,我们一般放在must中,使用match进行全文检索匹配,全文检索可以计算相关性得分,当然,一些复杂的检索,肯定是会用到复合查询,因此,需要使用到bool,对于那些仅仅用于检索但是不需要计算得分的字段的检索,可以放在filter中,大致的检索格式如下:
在filter中可以使用term单值查询或者terms多值查询,还有嵌套查询,使用term进行精确查询如图:
2.2 sort排序
sort与query平级,直接指定排序字段即可,如图:
2.3 分页
在es中分页条件通过from与size来指定。from表示从第几页开始,size表示每页显示都少数据
例如:
2.4 高亮highlight
highlight高亮显示语query平级,其有三个重要的属性field,前置标签pre_tags,后置标签post_tags,例如:
查询结果如图:
3. 聚合分析aggs
聚合分析与query同级。
3.1 简单聚合
按照品牌id进行聚合
GET gulimall_product/_search
{
"query": {
"match_all": {
}
},
"aggs": {
"brand_agg": {
"terms": {
"field": "brandId",
"size": 10
}
}
}
}
3.2 带子聚合
3.2.1 带一个子聚合
按照品牌进行聚合,在按照品牌id显示品牌名
GET gulimall_product/_search
{
"query": {
"match_all": {
}
},
"aggs": {
"brand_agg": {
"terms": {
"field": "brandId",
"size": 10
},
"aggs": {
"brand_name_agg": {
"terms": {
"field": "brandName",
"size": 10
}
}
}
}
}
}
3.2.2 带多个子聚合
按照品牌进行聚合,在按照品牌id显示品牌名与品牌图片
GET gulimall_product/_search
{
"query": {
"match_all": {
}
},
"aggs": {
"brand_agg": {
"terms": {
"field": "brandId",
"size": 10
},
"aggs": {
"brand_name_agg": {
"terms": {
"field": "brandName",
"size": 10
}
},
"brand_img_agg":{
"terms": {
"field": "brandImg",
"size": 10
}
}
}
}
}
}
3.3 多字段聚合
品牌和分类进行聚合
GET gulimall_product/_search
{
"query": {
"match_all": {
}
},
"aggs": {
"brand_agg": {
"terms": {
"field": "brandId",
"size": 10
},
"aggs": {
"brand_name_agg": {
"terms": {
"field": "brandName",
"size": 10
}
},
"brand_img_agg":{
"terms": {
"field": "brandImg",
"size": 10
}
}
}
},
"catalog_agg":{
"terms": {
"field": "catalogId",
"size": 10
},
"aggs": {
"catalog_name_agg": {
"terms": {
"field": "catalogName",
"size": 10
}
}
}
}
}
}
3.4 嵌入式聚合
嵌入式聚合也要使用nested,详细使用见文档:https://www.elastic.co/guide/en/elasticsearch/reference/7.11/search-aggregations-bucket-reverse-nested-aggregation.html
3.4.1 简单嵌套聚合
GET gulimall_product/_search
{
"query": {
"match_all": {
}
},
"aggs": {
"attr_agg":{
"nested": {
"path": "attrs"
},
"aggs": {
"attr_id": {
"terms": {
"field": "attrs.attrId",
"size": 10
}
}
}
}
}
}
3.4.2 带子聚合的嵌套聚合
GET gulimall_product/_search
{
"query": {
"match_all": {
}
},
"aggs": {
"attr_agg":{
"nested": {
"path": "attrs"
},
"aggs": {
"attr_id": {
"terms": {
"field": "attrs.attrId",
"size": 10
},
"aggs": {
"attr_name_id": {
"terms": {
"field": "attrs.attrName",
"size": 10
}
}
}
}
}
}
}
}
4. 完整的DSL查询语句
GET gulimall_product/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"skuTitle": "华为"
}
}
],
"filter": [
{
"term": {
"catalogId": "1111133"
}
},
{
"terms": {
"brandId": [
"3",
"5"
]
}
},
{
"term": {
"hasStock": true
}
},
{
"nested": {
"path": "attrs",
"query": {
"bool": {
"must": [
{
"term": {
"attrs.attrId": {
"value": "15"
}
}
},
{
"terms": {
"attrs.attrValue": [
"aaa",
"白色",
"OCE-AN10"
]
}
}
]
}
}
}
}
]
}
},
"sort": [
{
"skuPrice": {
"order": "desc"
}
}
],
"aggs": {
"brand_agg": {
"terms": {
"field": "brandId",
"size": 10
},
"aggs": {
"brand_name_agg": {
"terms": {
"field": "brandName",
"size": 10
}
},
"brand_img_agg":{
"terms": {
"field": "brandImg",
"size": 10
}
}
}
},
"catalog_agg":{
"terms": {
"field": "catalogId",
"size": 10
},
"aggs": {
"catalog_name_agg": {
"terms": {
"field": "catalogName",
"size": 10
}
}
}
},
"attr_agg":{
"nested": {
"path": "attrs"
},
"aggs": {
"attr_id": {
"terms": {
"field": "attrs.attrId",
"size": 10
},
"aggs": {
"attr_name_id": {
"terms": {
"field": "attrs.attrName",
"size": 10
}
}
}
}
}
}
},
"from": 0,
"size": 2,
"highlight": {
"fields": {"skuTitle": {}},
"pre_tags": "<b style='color:red'>",
"post_tags": "</b>"
}
}