• ElasticSearch中实际操作细节点


    ElasticSearch中的细节点

    1、提示:

    1.1 ElasticSearch相关文档:

    下面是ElasticSearch的官方文档链接:

    ElasticSearch官方文档

    如果基础学习ElasticSearch还可以参考我的另外两篇文章(仅供参考,以官网为准)

    ElasticSearch基础概念和安装使用

    ElasticSearch详细指令操作

    1.2 Kibana的常用快捷键

    这里的快捷键在kibana的help里面可以查看

    • Ctrl + / 直接从kibana跳转到官方文档
    • Ctrl+Enter 直接执行当前DSL语句
    • Ctrl+ I 格式化DSL
    • Alt + L 光标在括号处折叠当前的DSL代码块

    1.3 kibana的注释方式

    # 用于注释

    #! 用于警示

    2、term与terms的用法以及区别

    在 Elasticsearch 中,term 和 terms 都是用于查询文档的查询语句。它们的区别在于:

    • term 查询:精确匹配某个字段的值。例如,查询 title: "Elasticsearch" 将匹配所有包含 Elasticsearch 字符串的文档。
    • terms 查询:匹配某个字段的多个值。例如,查询 title: ["Elasticsearch", "Apache Solr"] 将匹配所有包含 ElasticsearchApache Solr 字符串的文档。

    具体来说,term 查询的语法如下:

    {
      "query": {
        "term": {
          "字段名": "值"
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    例如,以下查询将匹配所有包含 Elasticsearch 字符串的文档:

    {
      "query": {
        "term": {
          "title": "Elasticsearch"
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    terms 查询的语法如下:

    {
      "query": {
        "terms": {
          "字段名": ["值1", "值2", ...]
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    例如,以下查询将匹配所有包含 ElasticsearchApache Solr 字符串的文档:

    {
      "query": {
        "terms": {
          "title": ["Elasticsearch", "Apache Solr"]
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在实际使用中,term 查询通常用于精确匹配某个字段的值。例如,查询文档的标题是否包含某个特定的关键字。terms 查询通常用于匹配某个字段的多个值。例如,查询文档的标题是否包含某些特定的关键字。

    那么为什么在聚合里面使用terms呢?

    在 Elasticsearch 中,terms 聚合用于将文档分组,根据某个字段的值将文档分为多个桶。每个桶都包含一组具有相同值的文档。

    使用 terms 聚合可以实现以下功能:

    • 统计某个字段的值的分布。例如,可以使用 terms 聚合统计文档的标题中包含的所有关键字。
    • 查找文档中具有某个特定值的记录。例如,可以使用 terms 聚合查找文档的标题中包含 Elasticsearch 关键字的记录。
    • 聚合其他聚合。例如,可以使用 terms 聚合将文档分组,然后使用其他聚合来分析每个桶中的文档。

    具体来说,terms 聚合的语法如下:

    {
      "aggs": {
        "聚合名称": {
          "terms": {
            "字段名": {
              "参数1": 值1,
              "参数2": 值2,
              ...
            }
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    例如,以下查询将统计文档的标题中包含的所有关键字:

    {
      "query": {
        "match_all": {}
      },
      "aggs": {
        "keywords": {
          "terms": {
            "field": "title"
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    这个查询将返回以下结果:

    {
      "took": 1,
      "timed_out": false,
      "hits": {
        "total": 1000,
        "max_score": 1.0,
        "hits": [
          {
            "_index": "my_index",
            "_type": "my_type",
            "_id": "1",
            "_score": 1.0,
            "_source": {
              "title": "Elasticsearch 入门"
            }
          },
          {
            "_index": "my_index",
            "_type": "my_type",
            "_id": "2",
            "_score": 1.0,
            "_source": {
              "title": "Elasticsearch 搜索"
            }
          },
          ...
        ]
      },
      "aggregations": {
        "keywords": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 0,
          "buckets": [
            {
              "key": "Elasticsearch",
              "doc_count": 500
            },
            {
              "key": "搜索",
              "doc_count": 250
            },
            {
              "key": "索引",
              "doc_count": 125
            },
            ...
          ]
        }
      }
    }
    
    • 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

    可以看到,这个查询返回了两个桶,分别是 Elasticsearch搜索。每个桶都包含具有相同值的文档。

    因此,在聚合里面使用 terms 可以实现灵活的聚合分析。

    3、ElasticSearch中"index":“false”,“doc_values”:"false"这两个属性是干嘛的

    在 Elasticsearch 中,“index”:“false” 和 “doc_values”:“false” 这两个属性用于控制字段的索引和 doc_values 属性。

    • “index”:“false” 属性表示该字段不参与索引。这意味着该字段的值不会存储在索引中,因此无法使用该字段进行查询。
    • “doc_values”:“false” 属性表示该字段不参与 doc_values。这意味着该字段的值不会存储在 doc_values 中,因此无法使用该字段进行排序、聚合等操作。

    默认情况下,所有字段都参与索引和 doc_values。如果需要禁用某个字段的索引或 doc_values,可以设置相应的属性。

    以下是这两个属性的具体用途:

    • 禁用字段索引

    如果需要禁用某个字段的索引,可以设置 “index”:“false” 属性。例如,以下查询将创建一个索引,其中 title 字段不参与索引:

    PUT my_index
    {
      "mappings": {
        "my_type": {
          "properties": {
            "title": {
              "type": "text",
              "index": false
            }
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    这个查询将创建一个名为 my_index 的索引,其中 my_type 类型的 title 字段不参与索引。这意味着该字段的值不会存储在索引中,因此无法使用该字段进行查询。

    • 禁用字段 doc_values

    如果需要禁用某个字段的 doc_values,可以设置 “doc_values”:“false” 属性。例如,以下查询将创建一个索引,其中 title 字段不参与 doc_values:

    PUT my_index
    {
      "mappings": {
        "my_type": {
          "properties": {
            "title": {
              "type": "text",
              "doc_values": false
            }
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    这个查询将创建一个名为 my_index 的索引,其中 my_type 类型的 title 字段不参与 doc_values。这意味着该字段的值不会存储在 doc_values 中,因此无法使用该字段进行排序、聚合等操作。

    4、nested嵌入式扁平化处理是什么,怎么对该字段进行查询呢

    4.1 扁平化处理

    Elasticsearch 中的 nested 类型用于存储嵌套的对象。嵌套对象可以是任何类型的对象,包括字段、数组、甚至其他嵌套对象。

    nested 类型的字段可以进行嵌套扁平化处理。嵌套扁平化处理将嵌套对象转换为扁平的 JSON 格式。这使得嵌套对象可以像普通字段一样进行查询。

    例如:在原始数据中,products 字段是一个数组,其中每个元素是一个对象,包含 nameprice 两个字段。例如:

    [
      {
        "name": "iPhone 13",
        "price": 999
      },
      {
        "name": "iPad Pro",
        "price": 1099
      }
    ]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    经过扁平化处理后,products 字段会被转换为两个字段:products.nameproducts.price。例如:

    {
      "products.name": ["iPhone 13", "iPad Pro"],
      "products.price": [999, 1099]
    }
    
    • 1
    • 2
    • 3
    • 4

    因此,如果要查询 iphone13 1099,可以使用以下查询:

    GET my_index/my_type/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "products.name": "iPhone 13"
              }
            },
            {
              "match": {
                "products.price": 1099
              }
            }
          ]
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    这个查询将返回以下结果:

    {
      "hits": {
        "total": 1,
        "hits": [
          {
            "_index": "my_index",
            "_type": "my_type",
            "_id": "1",
            "_score": 1.0,
            "_source": {
              "products.name": ["iPhone 13"],
              "products.price": [1099]
            }
          }
        ]
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    从结果中可以看到,Elasticsearch 成功地查询到了 iphone13 1099 的文档。

    换句话说,通过扁平化处理,可以将嵌套的对象转换为扁平的 JSON 格式,这样就可以像查询普通字段一样查询嵌套对象。

    要对 nested 类型的字段进行嵌套扁平化处理,需要设置 type 属性为 nested。例如,以下查询将创建一个索引,其中 user 字段类型为 nested

    PUT my_index
    {
      "mappings": {
        "my_type": {
          "properties": {
            "user": {
              "type": "nested"
            }
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    这个查询将创建一个名为 my_index 的索引,其中 my_type 类型的 user 字段类型为 nested。这意味着 user 字段可以存储嵌套对象

    4.2 nested 类型的字段进行查询

    要对 nested 类型的字段进行查询,可以使用 nested 查询。nested 查询可以使用 path 参数指定嵌套对象的路径。例如,以下查询将查询所有包含 age25 的用户:

    GET my_index/my_type/_search
    {
      "query": {
        "nested": {
          "path": "user",
          "query": {
            "match": {
              "user.age": 25
            }
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    这个查询将返回所有包含 age25 的用户的文档。

    nested 查询还可以使用 query_filter 参数指定过滤条件。例如,以下查询将查询所有包含 age25nameJohn Doe 的用户:

    GET my_index/my_type/_search
    {
      "query": {
        "nested": {
          "path": "user",
          "query": {
            "match": {
              "user.age": 25
            }
          },
          "query_filter": {
            "match": {
              "user.name": "John Doe"
            }
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    这个查询将返回所有包含 age25nameJohn Doe 的用户的文档。

    5、实际查询案例

    5.1先创建索引映射

    注意可以新增加字段的映射的类型但是不能直接修改已有的映射,解决办法是备份修改,具体可以参考上面相关文档我的其他的文章。

    下面这个是网上的谷粒商城项目的文档,这里直接用来举例了

    ##建立product索引以及映射
    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"
          }
        }
      }
    }
    
    • 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

    5.2 添加数据

    es的数据是由项目中的商品上架功能存入es的。先发布商品,对商品的属性以及库存等进行维护管理,然后再上架商品,想好哪些字段数据需要存入es之后构建es的模型数据使用接口把数据存入es。

    5.3 写DSL 语句查询

    查询的DSL语句,需要结合业务来理解,仅供参考(最好使用kibana格式化观看)。 需要注意的是 如果是嵌入式的属性一定得用嵌入式的查询方式,不然可能查不出结果来。

    GET product/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "skuTitle": "测试"
              }
            }
          ],
          "filter": [
            {
              "term": {
                "catalogId": "225"
              }
            },
            {
              "terms": {
                "brandId": [
                  "1",
                  "2",
                  "9"
                ]
              }
            },
            {
              "nested": {
                "path": "attrs",
                "query": {
                  "bool": {
                    "must": [
                      {
                        "term": {
                          "attrs.attrId": {
                            "value": "15"
                          }
                        }
                      },
                      {
                        "terms": {
                          "attrs.attrValue": [
                            "高通(Qualcomm)",
                            "以官网信息为准"
                          ]
                        }
                      }
                    ]
                  }
                }
              }
            },
            {
              "term": {
                "hasStock": {
                  "value": "false"
                }
              }
            },
            {
              "range": {
                "skuPrice": {
                  "gte": 0,
                  "lte": 6000
                }
              }
            }
          ]
        }
      },
        "aggs": {
        "brand_aggs": {
          "terms": {
            "field": "brandId",
            "size": 10
          },
          "aggs": {
            "brand_name_aggs": {
              "terms": {
                "field": "brandName",
                "size": 10
              }
            },
            "brand_img_aggs": {
              "terms": {
                "field": "brandImg",
                "size": 10
              }
            }
          }
        },
        "catelog_aggs": {
          "terms": {
            "field": "catalogId",
            "size": 10
          },
          "aggs": {
            "catelog_name_aggs": {
              "terms": {
                "field": "catalogName",
                "size": 10
              }
            }
          }
        },
        "attr_aggs": {
          "nested": {
            "path": "attrs"
          },
          "aggs": {
            "attr_id_aggrs": {
              "terms": {
                "field": "attrs.attrId",
                "size": 10
              }
              , "aggs": {
                "attr_name_aggs": {
                  "terms": {
                    "field": "attrs.attrName",
                    "size": 10
                  }
                },
                "attr_value_aggs":{
                  "terms": {
                    "field": "attrs.attrValue",
                    "size": 10
                  }
                }
              }
            }
          }
        }
      },
      "sort": [
        {
          "skuPrice": {
            "order": "desc"
          }
        }
      ],
      "from": 0,
      "size": 5,
      "highlight": {
        "fields": {"skuTitle": {}}, 
        "pre_tags": "",
        "post_tags": ""
      }
    }
    
    
    • 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
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149

    5.4 改写DSL语句为java API

    @Slf4j
    @Service
    public class MallSearchServiceImpl implements MallSearchService {
    
        @Autowired
        private RestHighLevelClient esRestClient;
    
        @Resource
        private ProductFeignService productFeignService;
    
        @Override
        public SearchResult search(SearchParam param) {
    
            //1、动态构建出查询需要的DSL语句
            SearchResult result = null;
    
            //1、准备检索请求
            SearchRequest searchRequest = buildSearchRequest(param);
    
            try {
                //2、执行检索请求
                SearchResponse response = esRestClient.search(searchRequest, GulimallElasticSearchConfig.COMMON_OPTIONS);
    
                //3、分析响应数据,封装成我们需要的格式
                result = buildSearchResult(response,param);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            return result;
        }
    
        /**
         * 构建结果数据
         * 模糊匹配,过滤(按照属性、分类、品牌,价格区间,库存),完成排序、分页、高亮,聚合分析功能
         * @param response
         * @return
         */
        private SearchResult buildSearchResult(SearchResponse response,SearchParam param) {
    
            SearchResult result = new SearchResult();
    
            //1、返回的所有查询到的商品
            SearchHits hits = response.getHits();
    
            List<SkuEsModel> esModels = new ArrayList<>();
            //遍历所有商品信息
            if (hits.getHits() != null && hits.getHits().length > 0) {
                for (SearchHit hit : hits.getHits()) {
                    String sourceAsString = hit.getSourceAsString();
                    SkuEsModel esModel = JSON.parseObject(sourceAsString, SkuEsModel.class);
    
                    //判断是否按关键字检索,若是就显示高亮,否则不显示
                    if (!StringUtils.isEmpty(param.getKeyword())) {
                        //拿到高亮信息显示标题
                        HighlightField skuTitle = hit.getHighlightFields().get("skuTitle");
                        String skuTitleValue = skuTitle.getFragments()[0].string();
                        esModel.setSkuTitle(skuTitleValue);
                    }
                    esModels.add(esModel);
                }
            }
            result.setProduct(esModels);
    
            //2、当前商品涉及到的所有属性信息
            List<SearchResult.AttrVo> attrVos = new ArrayList<>();
            //获取属性信息的聚合
            ParsedNested attrsAgg = response.getAggregations().get("attr_agg");
            ParsedLongTerms attrIdAgg = attrsAgg.getAggregations().get("attr_id_agg");
            for (Terms.Bucket bucket : attrIdAgg.getBuckets()) {
                SearchResult.AttrVo attrVo = new SearchResult.AttrVo();
                //1、得到属性的id
                long attrId = bucket.getKeyAsNumber().longValue();
                attrVo.setAttrId(attrId);
    
                //2、得到属性的名字
                ParsedStringTerms attrNameAgg = bucket.getAggregations().get("attr_name_agg");
                String attrName = attrNameAgg.getBuckets().get(0).getKeyAsString();
                attrVo.setAttrName(attrName);
    
                //3、得到属性的所有值
                ParsedStringTerms attrValueAgg = bucket.getAggregations().get("attr_value_agg");
                List<String> attrValues = attrValueAgg.getBuckets().stream().map(item -> item.getKeyAsString()).collect(Collectors.toList());
                attrVo.setAttrValue(attrValues);
    
                attrVos.add(attrVo);
            }
    
            result.setAttrs(attrVos);
    
            //3、当前商品涉及到的所有品牌信息
            List<SearchResult.BrandVo> brandVos = new ArrayList<>();
            //获取到品牌的聚合
            ParsedLongTerms brandAgg = response.getAggregations().get("brand_agg");
            for (Terms.Bucket bucket : brandAgg.getBuckets()) {
                SearchResult.BrandVo brandVo = new SearchResult.BrandVo();
    
                //1、得到品牌的id
                long brandId = bucket.getKeyAsNumber().longValue();
                brandVo.setBrandId(brandId);
    
                //2、得到品牌的名字
                ParsedStringTerms brandNameAgg = bucket.getAggregations().get("brand_name_agg");
                String brandName = brandNameAgg.getBuckets().get(0).getKeyAsString();
                brandVo.setBrandName(brandName);
    
                //3、得到品牌的图片
                ParsedStringTerms brandImgAgg = bucket.getAggregations().get("brand_img_agg");
                String brandImg = brandImgAgg.getBuckets().get(0).getKeyAsString();
                brandVo.setBrandImg(brandImg);
    
                brandVos.add(brandVo);
            }
            result.setBrands(brandVos);
    
            //4、当前商品涉及到的所有分类信息
            //获取到分类的聚合
            List<SearchResult.CatalogVo> catalogVos = new ArrayList<>();
            ParsedLongTerms catalogAgg = response.getAggregations().get("catalog_agg");
            for (Terms.Bucket bucket : catalogAgg.getBuckets()) {
                SearchResult.CatalogVo catalogVo = new SearchResult.CatalogVo();
                //得到分类id
                String keyAsString = bucket.getKeyAsString();
                catalogVo.setCatalogId(Long.parseLong(keyAsString));
    
                //得到分类名
                ParsedStringTerms catalogNameAgg = bucket.getAggregations().get("catalog_name_agg");
                String catalogName = catalogNameAgg.getBuckets().get(0).getKeyAsString();
                catalogVo.setCatalogName(catalogName);
                catalogVos.add(catalogVo);
            }
    
            result.setCatalogs(catalogVos);
            //===============以上可以从聚合信息中获取====================//
            //5、分页信息-页码
            result.setPageNum(param.getPageNum());
            //5、1分页信息、总记录数
            long total = hits.getTotalHits().value;
            result.setTotal(total);
    
            //5、2分页信息-总页码-计算
            int totalPages = (int)total % EsConstant.PRODUCT_PAGESIZE == 0 ?
                    (int)total / EsConstant.PRODUCT_PAGESIZE : ((int)total / EsConstant.PRODUCT_PAGESIZE + 1);
            result.setTotalPages(totalPages);
    
            List<Integer> pageNavs = new ArrayList<>();
            for (int i = 1; i <= totalPages; i++) {
                pageNavs.add(i);
            }
            result.setPageNavs(pageNavs);
    
    
            //6、构建面包屑导航
            if (param.getAttrs() != null && param.getAttrs().size() > 0) {
                List<SearchResult.NavVo> collect = param.getAttrs().stream().map(attr -> {
                    //1、分析每一个attrs传过来的参数值
                    SearchResult.NavVo navVo = new SearchResult.NavVo();
                    String[] s = attr.split("_");
                    navVo.setNavValue(s[1]);
                    R r = productFeignService.attrInfo(Long.parseLong(s[0]));
                    if (r.getCode() == 0) {
                        AttrResponseVo data = r.getData("attr", new TypeReference<AttrResponseVo>() {
                        });
                        navVo.setNavName(data.getAttrName());
                    } else {
                        navVo.setNavName(s[0]);
                    }
    
                    //2、取消了这个面包屑以后,我们要跳转到哪个地方,将请求的地址url里面的当前置空
                    //拿到所有的查询条件,去掉当前
                    String encode = null;
                    try {
                        encode = URLEncoder.encode(attr,"UTF-8");
                        encode.replace("+","%20");  //浏览器对空格的编码和Java不一样,差异化处理
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    String replace = param.get_queryString().replace("&attrs=" + attr, "");
                    navVo.setLink("http://search.gulimall.com/list.html?" + replace);
    
                    return navVo;
                }).collect(Collectors.toList());
    
                result.setNavs(collect);
            }
    
    
            return result;
        }
    
    
        /**
         * 准备检索请求
         * 模糊匹配,过滤(按照属性,分类,品牌,价格区间,库存),排序,分页,高亮,聚合分析
         * @return
         */
        private SearchRequest buildSearchRequest(SearchParam param) {
    
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    
            /**
             * 模糊匹配,过滤(按照属性,分类,品牌,价格区间,库存)
             */
            //1. 构建bool-query
            BoolQueryBuilder boolQueryBuilder=new BoolQueryBuilder();
    
            //1.1 bool-must
            if(!StringUtils.isEmpty(param.getKeyword())){
                boolQueryBuilder.must(QueryBuilders.matchQuery("skuTitle",param.getKeyword()));
            }
    
            //1.2 bool-fiter
            //1.2.1 catelogId
            if(null != param.getCatalog3Id()){
                boolQueryBuilder.filter(QueryBuilders.termQuery("catalogId",param.getCatalog3Id()));
            }
    
            //1.2.2 brandId
            if(null != param.getBrandId() && param.getBrandId().size() >0){
                boolQueryBuilder.filter(QueryBuilders.termsQuery("brandId",param.getBrandId()));
            }
    
            //1.2.3 attrs
            if(param.getAttrs() != null && param.getAttrs().size() > 0){
    
                param.getAttrs().forEach(item -> {
                    //attrs=1_5寸:8寸&2_16G:8G
                    BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
    
    
                    //attrs=1_5寸:8寸
                    String[] s = item.split("_");
                    String attrId=s[0];
                    String[] attrValues = s[1].split(":");//这个属性检索用的值
                    boolQuery.must(QueryBuilders.termQuery("attrs.attrId",attrId));
                    boolQuery.must(QueryBuilders.termsQuery("attrs.attrValue",attrValues));
    
                    NestedQueryBuilder nestedQueryBuilder = QueryBuilders.nestedQuery("attrs",boolQuery, ScoreMode.None);
                    boolQueryBuilder.filter(nestedQueryBuilder);
                });
    
            }
    
            //1.2.4 hasStock
            if(null != param.getHasStock()){
                boolQueryBuilder.filter(QueryBuilders.termQuery("hasStock",param.getHasStock() == 1));
            }
    
    
            //1.2.5 skuPrice
            if(!StringUtils.isEmpty(param.getSkuPrice())){
                //skuPrice形式为:1_500或_500或500_
                RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("skuPrice");
                String[] price = param.getSkuPrice().split("_");
                if(price.length==2){
                    rangeQueryBuilder.gte(price[0]).lte(price[1]);
                }else if(price.length == 1){
                    if(param.getSkuPrice().startsWith("_")){
                        rangeQueryBuilder.lte(price[1]);
                    }
                    if(param.getSkuPrice().endsWith("_")){
                        rangeQueryBuilder.gte(price[0]);
                    }
                }
                boolQueryBuilder.filter(rangeQueryBuilder);
            }
    
            //封装所有的查询条件
            searchSourceBuilder.query(boolQueryBuilder);
    
    
            /**
             * 排序,分页,高亮
             */
    
            //排序
            //形式为sort=hotScore_asc/desc
            if(!StringUtils.isEmpty(param.getSort())){
                String sort = param.getSort();
                String[] sortFileds = sort.split("_");
    
                SortOrder sortOrder="asc".equalsIgnoreCase(sortFileds[1])?SortOrder.ASC:SortOrder.DESC;
    
                searchSourceBuilder.sort(sortFileds[0],sortOrder);
            }
    
            //分页
            searchSourceBuilder.from((param.getPageNum()-1)*EsConstant.PRODUCT_PAGESIZE);
            searchSourceBuilder.size(EsConstant.PRODUCT_PAGESIZE);
    
            //高亮
            if(!StringUtils.isEmpty(param.getKeyword())){
    
                HighlightBuilder highlightBuilder = new HighlightBuilder();
                highlightBuilder.field("skuTitle");
                highlightBuilder.preTags("");
                highlightBuilder.postTags("");
    
                searchSourceBuilder.highlighter(highlightBuilder);
            }
    
    
    
            /**
             * 聚合分析
             */
            //1. 按照品牌进行聚合
            TermsAggregationBuilder brand_agg = AggregationBuilders.terms("brand_agg");
            brand_agg.field("brandId").size(50);
    
    
            //1.1 品牌的子聚合-品牌名聚合
            brand_agg.subAggregation(AggregationBuilders.terms("brand_name_agg")
                    .field("brandName").size(1));
            //1.2 品牌的子聚合-品牌图片聚合
            brand_agg.subAggregation(AggregationBuilders.terms("brand_img_agg")
                    .field("brandImg").size(1));
    
            searchSourceBuilder.aggregation(brand_agg);
    
            //2. 按照分类信息进行聚合
            TermsAggregationBuilder catalog_agg = AggregationBuilders.terms("catalog_agg");
            catalog_agg.field("catalogId").size(20);
    
            catalog_agg.subAggregation(AggregationBuilders.terms("catalog_name_agg").field("catalogName").size(1));
    
            searchSourceBuilder.aggregation(catalog_agg);
    
            //2. 按照属性信息进行聚合
            NestedAggregationBuilder attr_agg = AggregationBuilders.nested("attr_agg", "attrs");
            //2.1 按照属性ID进行聚合
            TermsAggregationBuilder attr_id_agg = AggregationBuilders.terms("attr_id_agg").field("attrs.attrId");
            attr_agg.subAggregation(attr_id_agg);
            //2.1.1 在每个属性ID下,按照属性名进行聚合
            attr_id_agg.subAggregation(AggregationBuilders.terms("attr_name_agg").field("attrs.attrName").size(1));
            //2.1.1 在每个属性ID下,按照属性值进行聚合
            attr_id_agg.subAggregation(AggregationBuilders.terms("attr_value_agg").field("attrs.attrValue").size(50));
            searchSourceBuilder.aggregation(attr_agg);
    
            log.debug("构建的DSL语句 {}",searchSourceBuilder.toString());
    
            SearchRequest searchRequest = new SearchRequest(new String[]{EsConstant.PRODUCT_INDEX},searchSourceBuilder);
    
            return searchRequest;
        }
    }
    
    
    • 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
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347

    结语:

    这些语法等只知识都是仅供参考,真正学习得结合业务与官方文档进行理解,先学基础语法,然后再学习与Spring框架的整合使用,其实会写DSL了之后java API 就是按部就班。但是对查询出的结果进行解析也挺麻烦的。

  • 相关阅读:
    数据接口(json)和selenium的使用
    sass的常用语法总结
    python爬虫——爬取豆瓣top250电影数据(适合初学者)
    【C++】C++ 引用详解 ① ( 变量的本质 - 引入 “ 引用 “ 概念 | 引用语法简介 | 引用做函数参数 | 复杂类型引用做函数参数 )
    阿里云服务结构--长期更新
    【python海洋专题十】Cartopy画特定区域的地形等深线图
    SAP 重复制造简介
    Airtest的多图查找与两图对比
    MySQL中的运算符
    【文末送书】Python数据分析
  • 原文地址:https://blog.csdn.net/qq_45925197/article/details/134287277