Elasticsearch- get all values for a given field?

Solution 1:

How to get all possible values for field author?

curl -XGET  http://localhost:9200/articles/_search?pretty -d '
{
    "aggs" : {
        "whatever_you_like_here" : {
            "terms" : { "field" : "author", "size":10000 }
        }
    },
    "size" : 0
}'

Note

  • "size":10000 Get at most 10000 unique values. Default is 10.

  • "size":0 By default, "hits" contains 10 documents. We don't need them.

  • By default, the buckets are ordered by the doc_count in decreasing order.


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.

Solution 2:

I think what you want is a faceted search. Have a look at this example from the documenation:

http://www.elasticsearch.org/guide/reference/api/search/facets/index.html

curl -X POST "http://localhost:9200/articles/_search?pretty=true" -d '
  {
    "query" : { "query_string" : {"query" : "*"} },
    "facets" : {
      "tags" : { "terms" : {"field" : "author"} }
    }
  }
'

See if you can tailor this to work for you.

Hope this helps, Matt

Solution 3:

another example

request

curl -X POST "http://localhost:9200/_search?pretty=true" -d '
{
  "facets" : {
    "tags" : { "terms" : {"field" : "network.platform"} },
    "size" : 60
  },
  "size" : 0
}
'

response

{
  "took" : 266,
  "timed_out" : false,
  "_shards" : {
    "total" : 650,
    "successful" : 650,
    "failed" : 0
  },
  "hits" : {
    "total" : 41,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "facets" : {
    "tags" : {
      "_type" : "terms",
      "missing" : 15,
      "total" : 26,
      "other" : 0,
      "terms" : [ {
        "term" : "platform name 1",
        "count" : 20
      }, {
        "term" : "platform name 2",
        "count" : 6
      } ]
    }
  }
}

Solution 4:

I think the optimal way is to use elasticsearch aggregation https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html

GET {index}/{type}/_search
{
  "size": 0, <-- to not display search hits
  "aggs": {
    "{aggregation_name}": {
      "terms": {
        "field": "{filed_value}",
        "size": 10
      }
    }
  }
}