繁体   English   中英

Elasticsearch。 获取特定索引字段中的值计数

[英]Elasticsearch. Get count of values in a field of specific index

试图找到任何方法来检索特定字段中值的计数。 但是到目前为止,我在Elastic API文档中找不到合适的方法。

在此处输入图片说明

让我们考虑一下我有以下基本代码:

var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
  host: 'localhost:9200',
  log: 'trace'
});

我需要从索引“人”,字段“ dri”中获取计数

这将为您提供人员索引中某个字段的编号(如果该字段存在于所有文档中,则将与索引中的文档数量相同):

 const getFieldCount = (field) => client.search({
    size: 0,
    index: 'persons',
    body: {
      query: { match_all: {} },
      aggs: {
        field_count: {
            value_count: {
            field,
          },
        },
      },
    },
  });

  getFieldCount('dri')
    .then((res) => console.log(res));

按照上面的示例,以Tarek的答案为起点,我研究了此主体查询,该查询恰好返回了我需要的内容:精确字段中的值计数。

const getFieldCount = (field) => client.count({
            index: 'institutions',
            body: {
                query: {
                    bool: {
                        must:[
                            { "exists": {"field": "library_type_id"} }
                        ],
                    },
                },
            },
        });

暂无
暂无

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

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