# Help with 'or' filter needed

**URL:** <https://discuss.elastic.co/t/help-with-or-filter-needed/141829>\
**Category:** Elasticsearch\
**Created:** [July 26, 2018, 5:56pm UTC](https://discuss.elastic.co/t/help-with-or-filter-needed/141829 "2018-07-26T17:56:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jevgenij](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jevgenij/32/33543_2.png) [@Jevgenij](https://discuss.elastic.co/u/Jevgenij)\
**Post date:** [July 26, 2018, 5:56pm UTC](https://discuss.elastic.co/t/help-with-or-filter-needed/141829/1 "2018-07-26T17:56:55Z")

</div>

Days are passing, but still can't get used to the syntax of queries.

My current query structure:

```
{
    "query":{
        "bool":{
            "must":{ /* --- Here is my full-text search query --- */ 
                "match":{
                    "full_text": {
                    	"query": "test",
                    	"operator": "and",
                    	"fuzziness": "AUTO"
                    }
                }
            },
            "filter":[
                { /* --- Here are all of my optional faceted search terms goes --- */
                    "term":{
                        "status":"Published"
                    }
                },
                { /* --- Here are my 2 required 'AND' search terms:` range` and `script` --- */
                	"range":{
                		"price":{"gt":0}
                	}
                },
                {
                	"script":{
                		"script":{
                			"source":"doc['activated'].value.getMillis() + doc['duration'].value*24*60*60*1000 > System.currentTimeMillis()",
                			"lang":"painless"
                		}
                	}
                }]
        }
    }
}

```

At this point I'm happy with it.

But there is also a need to have my filter query with my 'AND' search terms (`range` and `script`) as 'OR' search terms. And here I'm stuck with syntax.

Any help would be appreciated,  
thanks in advance!

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [July 26, 2018, 8:13pm UTC](https://discuss.elastic.co/t/help-with-or-filter-needed/141829/2 "2018-07-26T20:13:32Z")

</div>

Just put another `bool` query inside filter and put all clauses that should be ORed into `should` clauses.

---

<div class="post-metadata">

**Author:** ![Jevgenij](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jevgenij/32/33543_2.png) [@Jevgenij](https://discuss.elastic.co/u/Jevgenij)\
**Post date:** [July 27, 2018, 9:17am UTC](https://discuss.elastic.co/t/help-with-or-filter-needed/141829/3 "2018-07-27T09:17:15Z")

</div>

Wasn't aware the `bool` query can be nested inside `filter`...

Many thanks for the tip!

Now it's much clearer how to construct sophisticated search queries.

My final ORed query looks like this:

```
{
    "query":{
        "bool":{
            "must":{ /* --- Here is my full-text search query --- */
                "match":{
                    "full_text":{
                        "query":"test",
                        "operator":"and",
                        "fuzziness":"AUTO"
                    }
                }
            },
            "filter":[
                { /* --- Here are all of my optional faceted search terms --- */
                    "term":{
                        "status":"Published"
                    }
                },
                {
                    "bool":{ /* --- Here are my `OR` search terms --- */
                        "should":[
                            {
                                "term":{
                                    "price":0
                                }
                            },
                            {
                                "script":{
                                    "script":{
                                        "source":"doc['activated'].value.getMillis() + doc['duration'].value*24*60*60*1000 < System.currentTimeMillis()",
                                        "lang":"painless"
                                    }
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

```

And it works flawlessly!

---

<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:** [August 24, 2018, 9:17am UTC](https://discuss.elastic.co/t/help-with-or-filter-needed/141829/4 "2018-08-24T09:17:17Z")

</div>

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