Query all unique values of a field with Elasticsearch
You could make a terms facet on your 'full_name' field. But in order to do that properly you need to make sure you're not tokenizing it while indexing, otherwise every entry in the facet will be a different term that is part of the field content. You most likely need to configure it as 'not_analyzed' in your mapping. If you are also searching on it and you still want to tokenize it you can just index it in two different ways using multi field.
You also need to take into account that depending on the number of unique terms that are part of the full_name field, this operation can be expensive and require quite some memory.
For Elasticsearch 1.0 and later, you can leverage terms aggregation
to do this,
query DSL:
{ "aggs": { "NAME": { "terms": { "field": "", "size": 10 } } }}
A real example:
{ "aggs": { "full_name": { "terms": { "field": "authors", "size": 0 } } }}
Then you can get all unique values of authors
field.size=0 means not limit the number of terms(this requires es to be 1.1.0 or later).
Response:
{ ... "aggregations" : { "full_name" : { "buckets" : [ { "key" : "Ken", "doc_count" : 10 }, { "key" : "Jim Gray", "doc_count" : 10 }, ] } }}
Working for Elasticsearch 5.2.2
curl -XGET http://localhost:9200/articles/_search?pretty -d '{ "aggs" : { "whatever" : { "terms" : { "field" : "yourfield", "size":10000 } } }, "size" : 0}'
The "size":10000
means get (at most) 10000 unique values. Without this, if you have more than 10 unique values, only 10 values are returned.
The "size":0
means that in result, "hits"
will contain no documents. By default, 10 documents are returned, which we don't need.
Reference: bucket terms aggregation
Also note, according to this page, facets have been replaced by aggregations in Elasticsearch 1.0, which are a superset of facets.