簡體   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