Brian Anderson
Brian Anderson

Reputation: 621

How can I query/filter an elasticsearch index by an array of values?

I have an elasticsearch index with numeric category ids like this:

{
  "id": "50958",
  "name": "product name",
  "description": "product description",
  "upc": "00302590602108",
  "**categories**": [
    "26",
    "39"
  ],
  "price": "15.95"
}

I want to be able to pass an array of category ids (a parent id with all of it's children, for example) and return only results that match one of those categories. I have been trying to get it to work with a term query, but no luck yet.

Also, as a new user of elasticsearch, I am wondering if I should use a filter/facet for this...

ANSWERED! I ended up using a terms query (as opposed to term). I'm still interested in knowing if there would be a benefit to using a filter or facet.

Upvotes: 2

Views: 5189

Answers (1)

Andy
Andy

Reputation: 8949

As you already discovered, a termQuery would work. I would suggest a termFilter though, since filters are faster, and cache-able.

Facets won't limit result, but they are excellent tools. They count hits within your total results of specific terms, and be used for faceted navigation.

Upvotes: 0

Related Questions