Reputation: 2333
I have an ElasticSearch index called "myindex" that I load documents into for three different mapping types (person, event, and vendor)...
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
I can see that the index was created successfully by using the command:
curl 'localhost:9200/_cat/indices?v'
I can successfully list all mapping types by using the command:
curl -s -XGET 'http://localhost:9200/myindex/_mapping/?pretty'
MY QUESTION - PART A: How do I get an aggregated/total count of documents for each and every mapping type in an explicitly named index ("myindex")? In other words, I'd like to know the number of documents for each and every mapping type.
NOTE: I tried curl -s -XGET 'http://localhost:9200/myindex/_count/?pretty'
but it only returns the total count, across all mapping types, not by each mapping type. In other words, it did not break down the county beach mapping type.
MY QUESTION - PART B: Given being able to get an aggregate/total count of documents for each mapping type, how do I get an aggregated count for any one specific mapping type stored in an explicitly named index ("myindex")? In other words, I'd like to know the number of documents for one explicitly named mapping type (e.g. "event") under the index "myindex".
Upvotes: 5
Views: 4262
Reputation: 935
What version of Elastic search are you using?
Under 2.x , your queries should work just as fine , all you need to add is the document type in your query so you can get an specific count per type.
For example , take a look at this full example :
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
}
}
EDIT
To correct my comment based on @IanGabes comment and answer to part B , this is how you get the count per mapping :
GET /testindex/_search
{
"size": 0,
"aggs": {
"countPerMapping": {
"terms": {
"field": "_type"
}
}
}
}
Please note that size 0 is to avoid showing you results in the query and focus only in the aggregation. Where each bucket's key is the name of the mapping and doc_count the number of documents.
{
"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
}
]
}
}
}
Or using curl
curl -XGET "http://localhost:9200/testindex/_search" -d'
{
"size": 0,
"aggs": {
"countPerMapping": {
"terms": {
"field": "_type"
}
}
}
}'
Upvotes: 5