Search a field with a lookup value list

Hi,

I guess my use case is quite the opposite than normal case.

I have a document, where it has a field, like below:

{
   'productCategoryId': 2
}

And in my system, I have a list of product types, like below:

  • 1 = foo
  • 2 = bar
  • 3 = something else

I would like the search to only return those documents that match the given list of product type.

For example, if I only want to search documents with product type being 2 or 3, how do I do write the query?

Hi @Wilson_Chen

Did you try with Terms Query?

Hi @RabBit_BR ,

I guess I didn't explain it clearly.

The document is like below:

{
   'productCategoryId': 2
}

productCategoryId can only be either 1, 2 or 3.

And the user may choose to only search for 1, or 2, or 3, or any combination of them, for example, 2 and 3.

For example:

  • user chooses 1, it will not match the above doc
  • user chooses 2, it will match
  • user chooses 3, it will not match
  • user chooses 2 and 3, it will match

When we use TermsQuery, I believe the use case is the opposite. As I understand it, the document would have any combination of of the category ids, like [2, 3], and the client/consumer will provide any combination to match.

Which is not my use case.

Terms query:

A document will match if it contains at least one of the terms

If your use case is exactly like the example you provided, you will only retrieve the document if there is at least one match in the list of IDs.
Are you sure that the Terms query didn't work?

From Elastic Search to Elasticsearch

@RabBit_BR you are right, TermsQuery is the way to go, somehow I thought I thought it needs some reversed way to lookup the fixed values.

Thanks a lot for the help!