Ch05 Aggregation

1/11/2023 ElasticSearchES

#

# Advanced

# 数据聚合

image-20230113122519851

  • 聚合的字段不能是text,也就是说不能分词

# Bucket聚合语法

  • 和groupby

image-20230113123833154

image-20230113123606919

image-20230113123706889

# Metrics 聚合语法

image-20230113225225623

  • 根据每个桶中的平均得分排序image-20230113225602726

# RestClient聚合

image-20230113225813906

  • 发送请求

image-20230113225925636

  • 解析结果image-20230113230032810

# 多条件聚合

image-20230113232843404

image-20230113232913519

# 拼音分词器(https://github.com/medcl/elasticsearch-analysis-pinyin)

  • 可以实现搜索的时候自动提示的功能
  • 把分词器的目录挂载到es的plugin之下安装
POST /_analyze
{
  "text": ["如家酒店还不错"]
  , "analyzer": "pinyin"
}
1
2
3
4
5

image-20230114230305586

# 自定义分词器

  • 因为拼音分词器不会分词(一个字一个字转成拼音,不知道哪个是词)

image-20230114230231354

image-20230114230816116

  • 但是查询的时候不能用拼音分词器:image-20230114231323946

  • image-20230114231307053

    • 所以在创建索引时和搜索时要使用不同的分词器image-20230114231502223

    image-20230114231600073

# 自动补全

# Completion suggester

image-20230114231817633

  • title分成两个字段的原因是,如果是一个字符串的话只能对S进行自动补全,用户搜W是不会出现的

  • 自动补全的查询

image-20230114232454531

image-20230114232604487

# 例子

  • 需要分词的用text_anlyzer, 不需要分词(固定的词条,keyword )的用completion_analyzer
  • 把关键字,比如说品牌,商圈放到suggestion里面去image-20230114234510358
// 酒店数据索引库
PUT /hotel
{
  "settings": {
    "analysis": {
      "analyzer": {
        "text_anlyzer": {
          "tokenizer": "ik_max_word",
          "filter": "py"
        },
        "completion_analyzer": {
          "tokenizer": "keyword",
          "filter": "py"
        }
      },
      "filter": {
        "py": {
          "type": "pinyin",
          "keep_full_pinyin": false,
          "keep_joined_full_pinyin": true,
          "keep_original": true,
          "limit_first_letter_length": 16,
          "remove_duplicated_term": true,
          "none_chinese_pinyin_tokenize": false
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "id":{
        "type": "keyword"
      },
      "name":{
        "type": "text",
        "analyzer": "text_anlyzer",
        "search_analyzer": "ik_smart",
        "copy_to": "all"
      },
      "address":{
        "type": "keyword",
        "index": false
      },
      "price":{
        "type": "integer"
      },
      "score":{
        "type": "integer"
      },
      "brand":{
        "type": "keyword",
        "copy_to": "all"
      },
      "city":{
        "type": "keyword"
      },
      "starName":{
        "type": "keyword"
      },
      "business":{
        "type": "keyword",
        "copy_to": "all"
      },
      "location":{
        "type": "geo_point"
      },
      "pic":{
        "type": "keyword",
        "index": false
      },
      "all":{
        "type": "text",
        "analyzer": "text_anlyzer",
        "search_analyzer": "ik_smart"
      },
      "suggestion":{
          "type": "completion",
          "analyzer": "completion_analyzer"
      }
    }
  }
}

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
  • 查询image-20230114234648928

# Restful API

image-20230114235504762

  • 处理结果image-20230114235707624
Last Updated: 11/19/2024, 1:54:38 PM