# Filter documents using AND OR on array field

**URL:** https://discuss.elastic.co/t/filter-documents-using-and-or-on-array-field/166330
**Category:** Elasticsearch
**Created:** [January 30, 2019, 10:30am UTC](https://discuss.elastic.co/t/filter-documents-using-and-or-on-array-field/166330 "2019-01-30T10:30:47Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Wassim\_Ben\_Amor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wassim_ben_amor/32/39799_2.png) [@Wassim\_Ben\_Amor](https://discuss.elastic.co/u/Wassim_Ben_Amor)
#### Post date: [January 30, 2019, 10:30am UTC](https://discuss.elastic.co/t/filter-documents-using-and-or-on-array-field/166330/1 "2019-01-30T10:30:47Z")

</div>

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 ?

---

<div class="post-metadata">

### Author: ![Wassim\_Ben\_Amor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wassim_ben_amor/32/39799_2.png) [@Wassim\_Ben\_Amor](https://discuss.elastic.co/u/Wassim_Ben_Amor)
#### Post date: [January 30, 2019, 3:14pm UTC](https://discuss.elastic.co/t/filter-documents-using-and-or-on-array-field/166330/2 "2019-01-30T15:14:20Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [February 27, 2019, 3:14pm UTC](https://discuss.elastic.co/t/filter-documents-using-and-or-on-array-field/166330/3 "2019-02-27T15:14:36Z")

</div>

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