# Combine has\_parent with AND & OR

**URL:** <https://discuss.elastic.co/t/combine-has-parent-with-and-or/39208>\
**Category:** Elasticsearch\
**Created:** [January 14, 2016, 9:14am UTC](https://discuss.elastic.co/t/combine-has-parent-with-and-or/39208 "2016-01-14T09:14:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![horte](https://avatars.discourse-cdn.com/v4/letter/h/9fc348/32.png) [@horte](https://discuss.elastic.co/u/horte)\
**Post date:** [January 14, 2016, 9:14am UTC](https://discuss.elastic.co/t/combine-has-parent-with-and-or/39208/1 "2016-01-14T09:14:10Z")

</div>

Hi, I have a parent-child document setup that looks like this:

```auto
{
    "mappings": {
        "campaigns":{
            "properties":{
                "name": {
                    "type": "string"
                },
                "paused": {
                    "type":"boolean"
                }
            }
        },
        "ads":{
            "_parent": {
                "type": "campaigns"
            },
            "properties": {
                "id": { "type": "string" },
                "categories":{ "type": "string","index":"not_analyzed" }
            }
        }
    }
}

```

I want to do a query that returns all `ads` that have certain categories and where the campaign is not paused.  
Is it possible to query on both has\_parent and the child in one query?  
In plain SQLish syntax it would be like this `select ad where campaign.paused = false and (ad.categories = sports or ad.categories = health)`.  
I have tried various queries but can't get it to work as i expect, e.g. this query below.

```auto
{
    "custom_filters_score" : {
        "query" : {
            "match_all" : {}
        },
        "filter": {
            "bool": {              
                "must": {
                    "has_parent": {
                        "type": "campaigns",
                        "filter": {
                            "term": {
                                "paused": false
                            }
                        }
                    }
                },
                "should": [
                        {
                            "term" : { "categories" : "finance" }
                        },
                        {
                            "term" : { "categories" : "sports" }
                        }
                ]                                
            }
        }       
    }
}

```

Any suggestion of how I can solve this in one query?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [January 14, 2016, 9:29am UTC](https://discuss.elastic.co/t/combine-has-parent-with-and-or/39208/2 "2016-01-14T09:29:34Z")

</div>

Right now your "should" criteria is just that - a nice to have rather than mandatory.  
Turn your "must" clause into an arrray and shift the ad-category criteria in there as a {bool-\>should} clause alongside the has\_parent clause

---

<div class="post-metadata">

**Author:** ![horte](https://avatars.discourse-cdn.com/v4/letter/h/9fc348/32.png) [@horte](https://discuss.elastic.co/u/horte)\
**Post date:** [January 14, 2016, 10:19am UTC](https://discuss.elastic.co/t/combine-has-parent-with-and-or/39208/3 "2016-01-14T10:19:01Z")

</div>

Thank you for the pointers. I got it to work like this:

```auto
{
    "query": {
        "function_score" : {
            "query" : {
                "match_all" : {}
            },
            "filter": {
                "bool": {              
                    "must": {
                        "has_parent": {
                            "type": "campaigns",
                            "filter": {
                                "term": {
                                    "paused": false
                                }
                            }
                        }
                    },
                    "should": [
                        {
                            "term" : { "categories" : "finance" }
                        },
                        {
                            "term" : { "categories" : "sports" }
                        }    
                    ]
                }
            }       
        }
    }    
}

```

---

<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:** [July 5, 2017, 11:24pm UTC](https://discuss.elastic.co/t/combine-has-parent-with-and-or/39208/4 "2017-07-05T23:24:35Z")

</div>


