我有一个名为" myindex"的ElasticSearch索引。我将文档加载到三种不同的映射类型(人员,事件和供应商)......
curl -XPOST localhost:9200/myindex/person/_bulk --data-binary @../JSON_DATA/persons.json
curl -XPOST localhost:9200/myindex/event/_bulk --data-binary @../JSON_DATA/events.json
curl -XPOST localhost:9200/myindex/vendor/_bulk --data-binary @../JSON_DATA/vendors.json
我可以看到使用以下命令成功创建了索引:
curl 'localhost:9200/_cat/indices?v'
我可以使用以下命令成功列出所有映射类型:
curl -s -XGET 'http://localhost:9200/myindex/_mapping/?pretty'
我的问题 - A部分:如何在明确命名的索引中获取每个映射类型的文档的聚合/总计数(" myindex&#34)?换句话说,我想知道每种映射类型的文档数量。
注意:我尝试curl -s -XGET 'http://localhost:9200/myindex/_count/?pretty'
但它只返回所有映射类型的总计数,而不是每种映射类型。换句话说,它没有打破县海滩地图类型。
我的问题 - B部分:鉴于能够获得每种映射类型的文档的聚合/总计数,如何获取存储在显式中的任何一种特定映射类型的聚合计数命名索引(" myindex")?换句话说,我想知道索引" myindex"下的一个明确命名的映射类型(例如" event")的文档数量。
答案 0 :(得分:2)
您使用的是哪种版本的弹性搜索?
在2.x下,您的查询应该可以正常工作,您需要添加的是查询中的文档类型,这样您就可以获得每种类型的特定计数。
例如,看看这个完整的例子:
DELETE testindex
PUT testindex
PUT testindex/_mapping/users
{
"properties": {
"name": {
"type": "string",
"analyzer": "standard"
}
}
}
PUT testindex/_mapping/users2
{
"properties": {
"name": {
"type": "string",
"analyzer": "standard"
}
}
}
PUT testindex/users/1
{
"name" : "Albert"
}
PUT testindex/user2/1
{
"name" : "Albert"
}
GET testindex/_count
{
"count": 2,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
}
}
GET testindex/users/_count
{
"count": 1,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
}
}
GET testindex/users2/_count
{
"count": 1,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
}
}
修改强>
要根据@IanGabes评论更正我的评论并回答B部分,这就是你如何获得每个映射的计数:
GET /testindex/_search
{
"size": 0,
"aggs": {
"countPerMapping": {
"terms": {
"field": "_type"
}
}
}
}
请注意大小0 是为了避免在查询中显示结果并仅关注聚合。其中每个桶的关键是映射的名称和doc_count文件的数量。
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0,
"hits": []
},
"aggregations": {
"countPerMapping": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "user2",
"doc_count": 1
},
{
"key": "users",
"doc_count": 1
}
]
}
}
}
或使用curl
curl -XGET "http://localhost:9200/testindex/_search" -d'
{
"size": 0,
"aggs": {
"countPerMapping": {
"terms": {
"field": "_type"
}
}
}
}'