user3215227
user3215227

Reputation: 11

ElasticSearch facet: how do I do this?

I have an ElasticSearch index that contains several million products with over a thousand brands.

What query would I have to use to get a list of all the brands in the index?

Sample product entry:

{
    _index: main
    _type: one
    _id: LA37dcdc7D70QygoV4KjfRU0hqUDhPs=
    _version: 4
    _score: 1
    _source: {
        pid: S2dcdcd528950_C243
        mid: 6540
        url: http://being.successfultogether.co.uk/
        price: 4
        currency: GBP
        brand: Reebok
        store: Matalan
    }

}

Upvotes: 1

Views: 112

Answers (2)

Nathan Smith
Nathan Smith

Reputation: 8347

Here is an example of generating facets against a selected field within your documents -

curl -XPUT <host>:9200/indices/type/_search?
{
  "query": {
    "match": {
      "store": "Matalan"
    }
  },
  "facets": {
    "brand": {
      "terms": {
        "field": "brand"
      }
    }
  }
}'

Upvotes: 2

mconlin
mconlin

Reputation: 8733

I think the all terms facet will get every term for a field:

POST /_all/_search
{
    "query" : {
        "match_all" : {  }
    },
    "facets" : {
        "tag" : {
            "terms" : {
                "field" : "stub",
                "all_terms" : true
            }
        }
    }
}

Terms aggregation as seen below ES 1.0 style, with a very high size count will probably return you every term and its count, it is not efficient nor is it for sure going to get them all.

You can read more about size and shard size params with aggregations/faceting here:
Elasticsearch Doco 1.0

POST /_all/_search
{
    "aggs" : {
        "genders" : {
            "terms" : { 
                "field" : "stub", 
                "size":1000
            }
        }
    },
    "size":0
}

ALSO, There are faceting plugins to get every term for a field as a list, see here: Approx Plugin

Upvotes: 1

Related Questions