Filter documents using AND OR on array field

Hello,
I have documents having a field called "filters" which is an array containing strings.
Example : ['A','B','C']
I want to make queries to filter documents, example of a query : ((('A' and 'C') or 'B') and ('A' or 'B'))
How can I do such queries ?

While there is no response to this.
I have used a queryBuilder in my client which produces rules composed of AND & OR.
An example of an output of this queryBuilder of the query ((('A' and 'C') or 'B') and ('A' or 'B')) =>

{
"id":"g-7R0daQv3t",
"type":"group",
"rules":[
    {
        "id":"g-n0nstzDhT",
        "type":"group",
        "rules":[
            {
                "id":"r-433oy3t9K",
                "field":"Value",
                "type":"rule",
                "value":{
                    "value":"A",
                    "label":"A"
                },
                "operator":"null"
            },
            {
                "id":"r-WHIWFvdwB",
                "field":"Value",
                "type":"rule",
                "value":{
                    "value":"B",
                    "label":"B"
                },
                "operator":"null"
            }
        ],
        "combinator":"or"
    },
    {
        "id":"g-mdOYCwPwI",
        "type":"group",
        "rules":[
            {
                "id":"r-B6KAkD7AK",
                "field":"Value",
                "type":"rule",
                "value":{
                    "value":"B",
                    "label":"B"
                },
                "operator":"null"
            },
            {
                "id":"g-BNREJgHF2",
                "type":"group",
                "rules":[
                    {
                        "id":"r-AJpmP7NGc",
                        "field":"Value",
                        "type":"rule",
                        "value":{
                            "value":"A",
                            "label":"A"
                        },
                        "operator":"null"
                    },
                    {
                        "id":"r-ZxTyFxCHC",
                        "field":"Value",
                        "type":"rule",
                        "value":{
                            "value":"C",
                            "label":"C"
                        },
                        "operator":"null"
                    }
                ],
                "combinator":"and"
            }
        ],
        "combinator":"or"
    }
],
"combinator":"and"
}

After this, I have created a recursive function that parse this query, and transform it to an ES query, "must" is dedicated to AND combinator, "should" is dedicated to OR combinator.
the function :

var transformFilter = (obj) => {
  if (obj.type == 'group') {
    return ({
      "bool": {
        [obj.combinator == 'and' ? 'must' : 'should']: obj.rules.map(rule => (transformFilter(rule)))
      }
    })
  }
  else {
    return ({
      "term": {
        "filters": obj.value.value
      }
    })
  }
}

the output :

> {
> "query":{
>     "bool":{
>         "must":[
>             {
>                 "bool":{
>                     "should":[
>                         {
>                             "term":{
>                                 "filters":"A"
>                             }
>                         },
>                         {
>                             "term":{
>                                 "filters":"B"
>                             }
>                         }
>                     ]
>                 }
>             },
>             {
>                 "bool":{
>                     "should":[
>                         {
>                             "term":{
>                                 "filters":"B"
>                             }
>                         },
>                         {
>                             "bool":{
>                                 "must":[
>                                     {
>                                         "term":{
>                                             "filters":"A"
>                                         }
>                                     },
>                                     {
>                                         "term":{
>                                             "filters":"C"
>                                         }
>                                     }
>                                 ]
>                             }
>                         }
>                     ]
>                 }
>             }
>         ]
>     }
> }
> }

Good Luck.

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