简体   繁体   中英

How to get the individual count of field from 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'])

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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