Best Way to Convert Dynamic Frontend Filters to Elasticsearch Query DSL (Deeply Nested + AND/OR Support)

Hi everyone,

I'm working on a frontend application that includes a flexible filter UI, allowing users to build complex search conditions using nested groups and logical operators (AND, OR). Each filter has a structure like:

{
  "field": "action",
  "operator": "is one of",
  "value": ["blocked", "allowed"]
}

We also support nested conditions like:

{
  "logic": "and",
  "conditions": [
    { "field": "tags", "operator": "exists" },
    {
      "logic": "or",
      "conditions": [
        { "field": "score", "operator": "<", "value": 10 },
        { "field": "severity", "operator": "is", "value": "High" }
      ]
    }
  ]
}

We're currently trying to convert this frontend structure into Elasticsearch Query DSL with the correct nesting and logic.

Is there an official or recommended approach or library from Elastic to convert structured filters into Elasticsearch queries? We can update the input but as per operator we need to build Elasticsearch query is there any apis or functions available for it ?

You’ll need to write a custom translator.
The main thing to watch out for is your “OR” lists - these should use a “bool” query with only a “should” clause as the container. If you try add other ANDed clauses in the same bool query eg using “must” alongside the “should” clause the OR list reverts to a nice-to-have list of optional clauses for the mandatory clause.
See this for details https://youtu.be/y9xwzsKrTiA?feature=shared