【问题标题】:How to get the individual count of field from Elasticsearch如何从 Elasticsearch 中获取单个字段计数
【发布时间】:2020-11-05 09:54:02
【问题描述】:

我在字典中的内容在下面

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

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

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的计数,这是所有的名字

这里是Accounting, Research Engineering

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

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

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

【问题讨论】:

  • BusinessArea 的 masterid 计数是什么意思
  • @Bhavya 示例 Accounting 在测试中的 masterid 中出现两次

标签: python elasticsearch dsl


【解决方案1】:

如果您想获取字段的单个计数,可以使用terms aggregation,这是一个基于多桶值源的聚合,其中动态构建桶 - 每个唯一值一个。

搜索查询:

{
  "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:

如果您想对 DesignationBusinessArea 的字段进行单独计数

搜索查询:

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

【讨论】:

  • @Nons 你想要单个字段的计数还是所有文档的计数?
  • 明白了,谢谢你的回答,点赞
  • 最后一件事/我有更新问题,我可以将Designation 作为所有关于DesignationBusinessArea 的信息作为关键以及关于BusinessArea 的所有信息
  • @Nons 如果你只是想改变bucket的名字,你也可以把它们改成DesignationBusinessArea
  • @Nons 默认情况下,包含聚合的搜索会返回搜索命中和聚合结果。为了只返回聚合结果,我将size 设置为 0
【解决方案2】:

您可以简单地使用count API of elasticsearch 来获取 elasticsearch 索引中的所有文档的计数或基于同一文档中显示的条件。

对于你的情况,应该是这样的

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

或者,如果 masterid 是您文档中的唯一 ID,您可以简单地使用

 GET /<your-index-name>/_count

【讨论】:

  • python怎么写?
  • @Nons,对不起,我不熟悉 python 语法 :(,但我相信它非常简单,你只需要使用 python 进行 HTTP 调用 :)
  • @Nons 如果有帮助,请不要忘记为答案投票。你也试过这种方式吗?顺便说一句,如果这是你想要的,它会更优化
猜你喜欢
  • 2017-11-26
  • 2021-04-23
  • 2017-01-20
  • 1970-01-01
  • 1970-01-01
  • 2021-03-22
  • 2016-07-05
  • 2014-02-21
  • 1970-01-01
相关资源
最近更新 更多