Query trouble with multiple OR conditions

Hey Folks!

I'm having weird trouble with enterprise app search hosted on Elastic Cloud.

I've several documents indexed with a field called customer_groups, when I make the following query: customer_groups:"112" OR customer_groups:"118" I get the expected results (documents with 112 or 118 in their customer_groups field) but when I add another condition, for example, customer_groups:"112" OR customer_groups:"118" OR customer_groups:"322" it doesn't return any result.

If I keep adding more OR conditions don't work either, I've tried this other way with the same result:

customer_groups:("112" OR "118" OR "322")

What can be the problem here?

Thanks

hi @lvalladares , welcome to the Community!

The reason for the behaviour you're observing is the Search Precision Tuning.

When adding terms to a query, a minimum of the fields should match in order for the query to succeed. At the default precision level (2), less than half of the terms must match. That means that even though you're using OR clauses, at least half of them needs to match for a result to be returned. This is related to how Lucene (the underlying search Engine for Elasticsearch) works.

For solving this, you can tweak precision and use precision level 1 for your query.

You can set precision:

Based on your use case, it might be possible that you need to perform filtering instead. You can combine filters for retrieving results that match any of your customer_groups field:

{
  "query": "your-query-here",
  "filters": {
    "all": [
      { "customer_groups": ["112", "118", "322"]}
    ]
  }
}

Hopefully this helps!

The filters did the trick for my use case. Thanks a lot

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.