简体   繁体   English

Elasticsearch 具有逐字段文档计数的多字段聚合

[英]Elasticsearch Multiple field aggregation with field wise document count

In a scenario, I have to search for phone numbers and license numbers that start with ' 861 '.在一个场景中,我必须搜索以“ 861 ”开头的电话号码和许可证号码。 I need to get field-wise matching data and field-wise total document count.我需要获取逐字段匹配数据和逐字段总文档数。

For that, I used multiple field aggregation.为此,我使用了多字段聚合。

In the output, I can see the field-wise data.在 output 中,我可以看到逐场数据。 But I also want to see the field-wise total count.但我也想看看按字段计算的总数。 Below is the search and aggregation query.下面是搜索和聚合查询。

My Query:我的查询:

GET emp_details_new/_search 
{
   "_source": [],
   "size": 0,
   "min_score": 1,
   "query": {
      "multi_match": {
        "query": "861",
         "fields": ["licence_num","phone"],
         "type": "phrase_prefix"
      }
   },
   "aggs": {
      "licence_num": {
         "terms": {
            "field": "licence_num.keyword",
            "include": "86.*"
         }
      },
      "phone": {
         "terms": {
            "field": "phone.keyword",
            "include": "86.*"
         }
      }
   }
}

Output: In the output, I can get only field wise data, also looking for field-wise count. Output:在 output 中,我只能获得逐场数据,同时也在寻找逐场计数。

{
  "took" : 31,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "phone" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "8613789726",
          "doc_count" : 1
        },
        {
          "key" : "8617323318",
          "doc_count" : 1
        }
      ]
    },
    "licence_num" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "8616203799",
          "doc_count" : 1
        },
        {
          "key" : "8616829169",
          "doc_count" : 1
        }
      ]
    }
  }
}

You can use Value Count aggregation for field-wise total count.您可以将值计数聚合用于按字段计算的总计数。

{
    "_source": [],
    "size": 0,
    "min_score": 1,
    "query": {
        "multi_match": {
            "query": "861",
            "fields": [
                "licence_num",
                "phone"
            ],
            "type": "phrase_prefix"
        }
    },
    "aggs": {
        "licence_num": {
            "terms": {
                "field": "licence_num.keyword",
                "include": "86.*"
            }
        },
        "phone": {
            "terms": {
                "field": "phone.keyword",
                "include": "86.*"
            }
        },
        "licence_num_count": {
            "value_count": {
                "field": "licence_num.keyword"
            }
        },
        "phone_count": {
            "value_count": {
                "field": "phone.keyword"
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM