elasticsearch嵌套聚合并排重获取数据

正经学徒,佛系记录,不搞事情

首先创建索引mapping,添加测试数据(由于目的是为了测试聚合,因此设置成keyword不分词)

PUT transcripts
{ 
    "mappings":{
        "transcripts":{
            "properties": {
                "name":{
                    "type":"keyword"
                },
                "score":{
                    "type":"keyword"
                },
                "sex":{
                    "type":"keyword"
                }
            }
        }
    }
}

测试数据如下:

name sex score
张三 100
张三 90
李四 80
李四 70

如果是单纯的想获取去重后对应数量:

获取所有名字所对应的数量:

POST transcripts/transcripts/_search
{
   "query": {
      "match_all": {}
   },
   "size": 0,//统计是不需要原数据的
   "aggs": {
      "nameAlias": {//别名
        "terms": {
           "field": "name", //distinct的字段   
           "size":10
        }
      }
   }
}

结果:名为张三和李四的各两人

获取所有名字下所有性别所对应的数量:

POST transcripts/transcripts/_search
{
   "query": {
      "match_all": {}
   },
   "size": 0,
   "aggs": {
      "nameAlias": {
        "terms": {
           "field": "name",    
           "size":10
        },
        "aggs": {
          "sexAlias": {
            "terms": {
               "field": "sex",    
               "size":10
            }
          }
        }
      }
   }
}

 结果:张三下有1个女性,1一个男性,李四下有2个男性

如果想获取每个记录的具体值(即想要获取具体的分数):

主要语法如下:

"aggs": {
    "alias": {//别名
        "top_hits": {
            "size": 1//返回第n条匹配的值
        }
    }
}

获取所有名字匹配的一条的数据:

POST transcripts/transcripts/_search
{
   "query": {
      "match_all": {}
   },
   "size": 0,
   "aggs": {
      "nameAlias": {
        "terms": {
           "field": "name",    
           "size":10
        },
        "aggs": {
           "alias1": {
              "top_hits": {
                  "size": 1
              }
           }
        }
      }
   }
}

结果:_source 下展示了所有的文档数据

获取所有名字下所有性别匹配的一条数据:

POST transcripts/transcripts/_search
{
   "query": {
      "match_all": {}
   },
   "size": 0,
   "aggs": {
      "nameAlias": {
        "terms": {
           "field": "name",    
           "size":10
        },
        "aggs": {
          "sexAlias": {
            "terms": {
               "field": "sex",    
               "size":10
            },
            "aggs": {
               "alias1": {
                  "top_hits": {
                      "size": 1
                  }
               }
            }
          }
        }
      }
   }
}

 结果:_source 下展示了所有的文档数据

"aggregations": {
      "nameAlias": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
            {
               "key": "张三",
               "doc_count": 2,
               "sexAlias": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                     {
                        "key": "女",
                        "doc_count": 1,
                        "alias1": {
                           "hits": {
                              "total": 1,
                              "max_score": 1,
                              "hits": [
                                 {
                                    "_index": "transcripts",
                                    "_type": "transcripts",
                                    "_id": "FU7koWgB2ZoT18WIeKTI",
                                    "_score": 1,
                                    "_source": {
                                       "name": "张三",
                                       "sex": "女",
                                       "score": "90"
                                    }
                                 }
                              ]
                           }
                        }
                     },
                     {
                        "key": "男",
                        "doc_count": 1,
                        "alias1": {
                           "hits": {
                              "total": 1,
                              "max_score": 1,
                              "hits": [
                                 {
                                    "_index": "transcripts",
                                    "_type": "transcripts",
                                    "_id": "FE7koWgB2ZoT18WISaTx",
                                    "_score": 1,
                                    "_source": {
                                       "name": "张三",
                                       "sex": "男",
                                       "score": "100"
                                    }
                                 }
                              ]
                           }
                        }
                     }
                  ]
               }
            },
            {
               "key": "李四",
               "doc_count": 2,
               "sexAlias": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                     {
                        "key": "男",
                        "doc_count": 2,
                        "alias1": {
                           "hits": {
                              "total": 2,
                              "max_score": 1,
                              "hits": [
                                 {
                                    "_index": "transcripts",
                                    "_type": "transcripts",
                                    "_id": "F07koWgB2ZoT18WItaQZ",
                                    "_score": 1,
                                    "_source": {
                                       "name": "李四",
                                       "sex": "男",
                                       "score": "70"
                                    }
                                 }
                              ]
                           }
                        }
                     }
                  ]
               }
            }
         ]
      }
   }

猜你喜欢

转载自blog.csdn.net/qq_31748587/article/details/86712254
今日推荐