elasticsearch多字段聚合实现方式

1、背景

我们知道在sql中是可以实现 group by 字段a,字段b,那么这种效果在elasticsearch中该如何实现呢?此处我们记录在elasticsearch中的3种方式来实现这个效果。

2、实现多字段聚合的思路

实现多字段聚合的思路
图片来源:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html
从上图中,我们可以知道,可以通过3种方式来实现 多字段的聚合操作。

3、需求

根据省(province)和性别(sex)来进行聚合,然后根据聚合后的每个桶的数据,在根据每个桶中的最大年龄(age)来进行倒序排序。

4、数据准备

4.1 创建索引

PUT /index_person
{
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "properties": {
      "id": {
        "type": "long"
      },
      "name": {
        "type": "keyword"
      },
      "province": {
        "type": "keyword"
      },
      "sex": {
        "type": "keyword"
      },
      "age": {
        "type": "integer"
      },
      "address": {
        "type": "text",
        "analyzer": "ik_max_word",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}

4.2 准备数据

PUT /_bulk
{"create":{"_index":"index_person","_id":1}}
{"id":1,"name":"张三","sex":"男","age":20,"province":"湖北","address":"湖北省黄冈市罗田县匡河镇"}
{"create":{"_index":"index_person","_id":2}}
{"id":2,"name":"李四","sex":"男","age":19,"province":"江苏","address":"江苏省南京市"}
{"create":{"_index":"index_person","_id":3}}
{"id":3,"name":"王武","sex":"女","age":25,"province":"湖北","address":"湖北省武汉市江汉区"}
{"create":{"_index":"index_person","_id":4}}
{"id":4,"name":"赵六","sex":"女","age":30,"province":"北京","address":"北京市东城区"}
{"create":{"_index":"index_person","_id":5}}
{"id":5,"name":"钱七","sex":"女","age":16,"province":"北京","address":"北京市西城区"}
{"create":{"_index":"index_person","_id":6}}
{"id":6,"name":"王八","sex":"女","age":45,"province":"北京","address":"北京市朝阳区"}

5、实现方式

5.1 multi_terms实现

5.1.1 dsl

GET /index_person/_search
{
  "size": 0,
  "aggs": {
    "agg_province_sex": {
      "multi_terms": {
        "size": 10,
        "shard_size": 25,
        "order":{
          "max_age": "desc"    
        },
        "terms": [
          {
            "field": "province",
            "missing": "defaultProvince"
          },
          {
            "field": "sex"
          }
        ]
      },
      "aggs": {
        "max_age": {
          "max": {
            "field": "age"
          }
        }
      }
    }
  }
}

5.1.2 java 代码

    @Test
    @DisplayName("多term聚合-根据省和性别聚合,然后根据最大年龄倒序")
    public void agg01() throws IOException {

        SearchRequest searchRequest = new SearchRequest.Builder()
                .size(0)
                .index("index_person")
                .aggregations("agg_province_sex", agg ->
                        agg.multiTerms(multiTerms ->
                                        multiTerms.terms(term -> term.field("province"))
                                                .terms(term -> term.field("sex"))
                                                .order(new NamedValue<>("max_age", SortOrder.Desc))
                                )
                                .aggregations("max_age", ageAgg ->
                                        ageAgg.max(max -> max.field("age")))

                )
                .build();
        System.out.println(searchRequest);
        SearchResponse<Object> response = client.search(searchRequest, Object.class);
        System.out.println(response);
    }

5.1.3 运行结果

运行结果

5.2 script实现

5.2.1 dsl

GET /index_person/_search
{
  "size": 0,
  "runtime_mappings": {
    "runtime_province_sex": {
      "type": "keyword",
      "script": """
          String province = doc['province'].value;
          String sex = doc['sex'].value;
          emit(province + '|' + sex);
      """
    }
  },
  "aggs": {
    "agg_province_sex": {
      "terms": {
        "field": "runtime_province_sex",
        "size": 10,
        "shard_size": 25,
        "order": {
          "max_age": "desc"
        }
      },
      "aggs": {
        "max_age": {
          "max": {
            "field": "age"
          }
        }
      }
    }
  }
}

5.2.2 java代码

@Test
    @DisplayName("多term聚合-根据省和性别聚合,然后根据最大年龄倒序")
    public void agg02() throws IOException {

        SearchRequest searchRequest = new SearchRequest.Builder()
                .size(0)
                .index("index_person")
                .runtimeMappings("runtime_province_sex", field -> {
                    field.type(RuntimeFieldType.Keyword);
                    field.script(script -> script.inline(new InlineScript.Builder()
                            .lang(ScriptLanguage.Painless)
                            .source("String province = doc['province'].value;\n" +
                                    "          String sex = doc['sex'].value;\n" +
                                    "          emit(province + '|' + sex);")
                            .build()));
                    return field;
                })
                .aggregations("agg_province_sex", agg ->
                        agg.terms(terms ->
                                        terms.field("runtime_province_sex")
                                                .size(10)
                                                .shardSize(25)
                                                .order(new NamedValue<>("max_age", SortOrder.Desc))
                                )
                                .aggregations("max_age", minAgg ->
                                        minAgg.max(max -> max.field("age")))
                )
                .build();
        System.out.println(searchRequest);
        SearchResponse<Object> response = client.search(searchRequest, Object.class);
        System.out.println(response);
    }

5.2.3 运行结果

运行结果

5.3 通过copyto实现

我本地测试过,通过copyto没实现,此处故先不考虑

5.5 通过pipeline来实现

实现思路:
创建mapping时,多创建一个字段pipeline_province_sex,该字段的值由创建数据时指定pipeline来生产。

5.4.1 创建mapping

PUT /index_person
{
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "properties": {
      "id": {
        "type": "long"
      },
      "name": {
        "type": "keyword"
      },
      "province": {
        "type": "keyword"
      },
      "sex": {
        "type": "keyword"
      },
      "age": {
        "type": "integer"
      },
      "pipeline_province_sex":{
        "type": "keyword"
      },
      "address": {
        "type": "text",
        "analyzer": "ik_max_word",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}

此处指定了一个字段pipeline_province_sex,该字段的值会由pipeline来处理。

5.4.2 创建pipeline

PUT _ingest/pipeline/pipeline_index_person_provice_sex
{
  "description": "将provice和sex的值拼接起来",
  "processors": [
    {
      "set": {
        "field": "pipeline_province_sex",
        "value": ["{{province}}", "{{sex}}"]
      }, 
      "join": {
        "field": "pipeline_province_sex",
        "separator": "|"
      }
    }
  ]
}

5.4.3 插入数据

PUT /_bulk?pipeline=pipeline_index_person_provice_sex
{"create":{"_index":"index_person","_id":1}}
{"id":1,"name":"张三","sex":"男","age":20,"province":"湖北","address":"湖北省黄冈市罗田县匡河镇"}
{"create":{"_index":"index_person","_id":2}}
{"id":2,"name":"李四","sex":"男","age":19,"province":"江苏","address":"江苏省南京市"}
{"create":{"_index":"index_person","_id":3}}
{"id":3,"name":"王武","sex":"女","age":25,"province":"湖北","address":"湖北省武汉市江汉区"}
{"create":{"_index":"index_person","_id":4}}
{"id":4,"name":"赵六","sex":"女","age":30,"province":"北京","address":"北京市东城区"}
{"create":{"_index":"index_person","_id":5}}
{"id":5,"name":"钱七","sex":"女","age":16,"province":"北京","address":"北京市西城区"}
{"create":{"_index":"index_person","_id":6}}
{"id":6,"name":"王八","sex":"女","age":45,"province":"北京","address":"北京市朝阳区"}

注意: 此处的插入需要指定上一步的pipeline
PUT /_bulk?pipeline=pipeline_index_person_provice_sex

5.4.4 聚合dsl

GET /index_person/_search
{
  "size": 0,
  "aggs": {
    "agg_province_sex": {
      "terms": {
        "field": "pipeline_province_sex",
        "size": 10,
        "shard_size": 25,
        "order": {
          "max_age": "desc"   
        }
      }, 
      "aggs": {
        "max_age": {
          "max": {
            "field": "age"
          }
        }
      }
    }
  }
}

5.4.5 运行结果

运行结果

6、实现代码

https://gitee.com/huan1993/spring-cloud-parent/blob/master/es/es8-api/src/main/java/com/huan/es8/aggregations/bucket/MultiTermsAggs.java

7、参考文档

  1. https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html
  • 4
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
### 回答1: Elasticsearch字段聚合是指在一个索引中,对多个字段进行聚合操作,以便更好地分析数据。这种聚合方式可以帮助用户更好地理解数据,发现数据中的规律和趋势,从而更好地做出决策。在Elasticsearch中,可以使用聚合框架来实现字段聚合,具体操作可以参考官方文档。 ### 回答2: Elasticsearch是一个流行的开源搜索引擎,它支持许多聚合操作。在Elasticsearch中,聚合是一种方法,用于对数据进行分组和计算。常用的聚合操作包括sum、avg、max、min等,除此之外还有复杂的聚合操作,比如多字段聚合。 多字段聚合是一种聚合操作,它可以将多个字段的数据进行聚合,并生成一个聚合结果。这个结果可以是一个数值、一个文本字符串、一个日期、一个地理位置等等,具体根据数据的类型而定。为了使用多字段聚合操作,我们需要指定要聚合字段。同时,我们也需要指定如何对这些字段进行聚合(如何计算、如何分组等) 在多字段聚合中,我们可以使用Elasticsearch提供的各种Aggregation类型,包括Metrics Aggregation、Bucket Aggregation、Pipeline Aggregation和Matrix Aggregation等。每种类型都具有不同的功能和特点,可以根据实际需求自由组合使用,实现复杂的聚合操作。 以Bucket Aggregation为例,Bucket Aggregation是一种将文档分组的聚合方法。我们可以使用Bucket Aggregation来按照多个字段进行分组,生成聚合结果。具体实现时,我们可以使用Elasticsearch中的Terms 和Histogram Aggregation来分别按照字符串和数值进行分组。我们可以通过指定不同的字段聚合计算方式,来实现各种不同的多字段聚合操作。 总之,多字段聚合Elasticsearch中非常重要的聚合操作之一,它可以对多个字段的数据进行复杂的聚合计算,为搜索引擎的用户提供更加优质的搜索结果。通过学习和掌握多字段聚合的方法和技巧,我们可以更好地利用Elasticsearch的强大功能,实现高效、快速、准确的搜索。 ### 回答3: Elasticsearch是一个流行的分布式全文搜索和分析引擎,可以轻松地处理各种类型的数据,并支持多字段聚合。多字段聚合是指在多个字段上执行聚合操作以获取分析结果的过程。在Elasticsearch中,可以使用聚合桶来实现这些操作。 首先,要执行多字段聚合,必须使用"aggs"关键字,它可以包含多个聚合。例如,以下查询将同时执行两个聚合,一个是根据year和genre字段计算电影数量的聚合,另一个是根据rating和genre字段计算平均评级的聚合: ``` { "aggs": { "by_year_genre": { "terms": { "script": "doc['year'].value + '|' + doc['genre'].value" } }, "by_rating_genre": { "avg": { "field": "rating" }, "terms": { "field": "genre" } } } } ``` 在这个查询中,第一个聚合使用"terms"桶来计算每个year/genre组合的电影数量。 "script"参数连接两个字段,以便在聚合过程中进行分组。 第二个聚合使用"avg"聚合来计算每个genre组合的平均评级,并使用"terms"桶进行分组。 另一个非常有用的聚合是按日期范围进行聚合。 以下查询显示了如何按月份进行聚合: ``` { "aggs": { "by_month": { "date_histogram": { "field": "date", "interval": "month", "format": "yyyy-MM" } } } } ``` 在上面的聚合中,"date_histogram"桶将按月份对结果进行分组,并使用"yyyy-MM"格式来指定日期格式。Elasticsearch还支持许多其他类型的聚合,包括范围聚合、嵌套聚合和指标聚合。 总之,Elasticsearch是强大的搜索和分析工具,可以轻松地执行多字段聚合操作。通过使用聚合桶,可以对结果进行分组、计数、平均值等分析,并在结果中显示有关数据的有用信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值