如何从Elasticsearch获取单个字段数 [英] How to get the individual count of field from Elasticsearch

查看:113
本文介绍了如何从Elasticsearch获取单个字段数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在字典中的内容在下面

My content inside a dictionary is below

test=
[ { 'masterid': '1', 'name': 'Group1', 'BusinessArea': [ { 'id': '14', 'name': 'Accounting', 'parentname': 'Finance'}, { 'id': '3', 'name': 'Research', 'parentname': 'R & D' } ], 'Designation': [ { 'id': '16', 'name': 'L1' }, { 'id': '20', 'name': 'L2' }, { 'id': '25', 'name': 'L2' }] }, 

{ 'masterid': '2', 'name': 'Group1', 'BusinessArea': [ { 'id': '14', 'name': 'Research', 'parentname': '' }, { 'id': '3', 'name': 'Accounting', 'parentname': '' } ], 'Role': [ { 'id': '5032', 'name': 'Tester' }, { 'id': '5033', 'name': 'Developer' } ], 'Designation': [ { 'id': '16', 'name': 'L1' }, { 'id': '20', 'name': 'L2' }, { 'id': '25', 'name': 'L2' }]},

 { 'masterid': '3', 'name': 'Group1', 'BusinessArea': [ { 'id': '14', 'name': 'Engineering' }, { 'id': '3', 'name': 'Engineering', 'parentname': '' } ], 'Role': [ { 'id': '5032', 'name': 'Developer' }, { 'id': '5033', 'name': 'Developer', 'parentname': '' } ], 'Designation': [ { 'id': '16', 'name': 'L1' }, { 'id': '20', 'name': 'L2' }, { 'id': '25', 'name': 'L2' }]}]

下面是放入弹性搜索索引的代码

Code is below to put into elastic search index

from elasticsearch import Elasticsearch
es = Elasticsearch()
es.indices.create(index='new')
for e in test:
        es.index(index="new", body=e, id=e['id'])

我想获取所有名称的 BusinessArea 的masterid计数

I want to get the count of masterid of BusinessArea which is all the names

这里是会计研究 工程

 [ {
      "name": "BusinessArea",
      "values": [
        {
          "name": "Accounting",
          "count": "2"
        },
        {
          "name": "Research",
          "count": "2"
        },
    {
          "name": "Engineering",
          "count": "1"
        }]
}]

或者我可以像下面这样回答

or can i have answer like below

{
    "A": {
        "Designation": [{
                "key": "L1",
                "doc_count": 3
            },
            {
                "key": "L2",
                "doc_count": 3
            }
        ]
    },
    {
        "B": {
            "BusinessArea": [{
                    "key": "Accounting",
                    "doc_count": 2
                },
                {
                    "key": "Research",
                    "doc_count": 2
                },
                {
                    "key": "Engineering",
                    "doc_count": 1
                }
            ]
        }
    }

推荐答案

如果要获取字段的单个计数,可以使用

If you want to get the individual count of the field you can use the terms aggregation that is a multi-bucket value source-based aggregation where buckets are dynamically built - one per unique value.

搜索查询:

{
  "size":0,
  "aggs": {
    "countNames": {
      "terms": {
        "field": "BusinessArea.name.keyword"
      }
    }
  }
}

搜索结果:

"aggregations": {
    "countNames": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "Accounting",
          "doc_count": 2
        },
        {
          "key": "Research",
          "doc_count": 2
        },
        {
          "key": "Engineering",
          "doc_count": 1
        }
      ]
    }

更新1:

如果您要对 Designation BusinessArea

搜索查询:

{
  "size": 0,
  "aggs": {
    "countNames": {
      "terms": {
        "field": "BusinessArea.name.keyword"
      }
    },
    "designationNames": {
      "terms": {
        "field": "Designation.name.keyword"
      }
    }
  }
}

搜索结果:

"aggregations": {
    "designationNames": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "L1",
          "doc_count": 3
        },
        {
          "key": "L2",
          "doc_count": 3
        }
      ]
    },
    "countNames": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "Accounting",
          "doc_count": 2
        },
        {
          "key": "Research",
          "doc_count": 2
        },
        {
          "key": "Engineering",
          "doc_count": 1
        }
      ]
    }

这篇关于如何从Elasticsearch获取单个字段数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆