javascript 如何在elasticsearch中获取每种索引类型的计数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17557580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:45:33  来源:igfitidea点击:

How to get a count for each type of an index in elasticsearch?

javascriptapicountelasticsearch

提问by cdietschrun

I have an index and want to get a count for the entries in every type of one particular index in elasticsearch, but might not know the types ahead of time.

我有一个索引,想在 elasticsearch 中获取一个特定索引的每种类型的条目计数,但可能不知道提前类型。

So, for example, the index is

因此,例如,索引是

/events

and the types could be

类型可能是

/events/type1
/events/type2
...
/events/typeN

And I'd like to query the index and say "Give me the count of each of the types under index events", so maybe a result set like

我想查询索引并说“给我索引事件下每种类型的计数”,所以结果集可能像

/events/type1 : 40
/events/type2: 20
/events/typeN: 10

where /events/_count would give me

/events/_count 会给我的地方

/events: 70

Edit:

编辑

imotov's answer is great. I'm having trouble figuring how to get it working in JavaScript/Ajax easily though. I have something like this right now:

imotov 的回答很棒。不过,我很难弄清楚如何让它在 JavaScript/Ajax 中轻松工作。我现在有这样的事情:

$.ajax({
type: 'GET',
url: 'http://localhost:9200/events/_search?search_type=count',
data: '{ "facets" : { "count_by_type" : { "terms" : { "field": "_type" }}}}',
success: function(text) {
    console.log(text);
}
)}'

But am only getting the total count of elements in the ES, the facets portion of the answer seems to be missing.

但是我只得到了 ES 中元素的总数,答案的方面部分似乎丢失了。

回答by imotov

You can use terms aggregations on the _typefield to get this information:

您可以在_type字段上使用术语聚合来获取此信息:

curl "localhost:9200/test-idx/_search?search_type=count" -d '{
    "aggs": {
        "count_by_type": {
            "terms": {
                "field": "_type"
            }
        }
    }
}'

回答by Akshay Bijawe

For Elasticsearch v5.0, search_type=countis removed. The same query from the above answers can be written as follows:

对于 Elasticsearch v5.0,删除了search_type=count。来自上述答案的相同查询可以写成如下:

GET  indexname/_search
{
    "aggs": {
        "count_by_type": {
            "terms": {
                "field": "_type"
            }
        }
    },
    "size": 0
}

Ref: https://www.elastic.co/guide/en/elasticsearch/reference/5.0/breaking_50_search_changes.html#_literal_search_type_count_literal_removed

参考:https: //www.elastic.co/guide/en/elasticsearch/reference/5.0/breaking_50_search_changes.html#_literal_search_type_count_literal_removed

回答by Roberto

The "facets" are deprecated in ES v. 1.5+ However you can use "aggregations", the use and results are quite similar:

ES v. 1.5+ 中不推荐使用“facets”但是您可以使用“aggregations”,使用和结果非常相似:

curl "localhost:9200/events/_search?search_type=count" -d '{
    "aggregations": {
        "count_by_type": {
            "terms": {
                "field": "_type"
            }
        }
    },
    "size": 0
}'

You'll get something like:

你会得到类似的东西:

{
   "took": 21,
   "timed_out": false,
   "_shards": {
      "total": 10,
      "successful": 10,
      "failed": 0
   },
   "hits": {
      "total": 150,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "count_by_type": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
            {
               "key": "type1",
               "doc_count": 141
            },
            {
               "key": "type2",
               "doc_count": 6
            },
            {
               "key": "other_type",
               "doc_count": 3
            }
         ]
      }
   }
}

回答by Srihari Sridharan

Answers by @Askshay and @Roberto highlight another important aspect. Setting size to 0 is really important, especially in low-bandwidth use-cases (say in mobile networks). It reduces the data payload size and that makes a huge difference when document size is large. Note "size": 0

@Askshay 和 @Roberto 的回答突出了另一个重要方面。将 size 设置为 0 非常重要,尤其是在低带宽用例中(例如在移动网络中)。它减少了数据有效负载的大小,当文档大小很大时,这会产生巨大的差异。注意“大小”:0

GET  index/_search
{
    "aggs": {
        "countByType": {
            "terms": {
                "field": "_type"
            }
        }
    },
    "size": 0
}