How to get the individual count of field from Elasticsearch

独自空忆成欢 提交于 2020-12-26 11:06:40

问题


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'])

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

Here it is Accounting, Research Engineering

 [ {
      "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
                }
            ]
        }
    }

回答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.

Search Query:

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

Search Result:

"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
        }
      ]
    }

Update 1:

If you want to have an individual count of the field for Designation as well as BusinessArea

Search Query:

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

Search Result:

"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
        }
      ]
    }



回答2:


You can simply use the count API of elasticsearch to get the count of All the documents in the elasticsearch index or based on a condition as shown in the same doc.

For your case, it should be like

GET /<your-index-name>/_count?q=name:BusinessArea

Or, if masterid is the Unique-id in your document, you can simply use

 GET /<your-index-name>/_count


来源:https://stackoverflow.com/questions/64675696/how-to-get-the-individual-count-of-field-from-elasticsearch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!