# Nested Query

**URL:** <https://discuss.elastic.co/t/nested-query/369752>\
**Category:** Elasticsearch\
**Created:** [October 29, 2024, 4:03pm UTC](https://discuss.elastic.co/t/nested-query/369752 "2024-10-29T16:03:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![palkema](https://avatars.discourse-cdn.com/v4/letter/p/a9a28c/32.png) [@palkema](https://discuss.elastic.co/u/palkema)\
**Post date:** [October 29, 2024, 4:03pm UTC](https://discuss.elastic.co/t/nested-query/369752/1 "2024-10-29T16:03:28Z")

</div>

Hi,  
I have an elastic query where I would like to filter on the property userData where the windowsId = 'palkema', so I can retrieve those through the inner\_hits response, however this is impacting my overall result.

For example, the first part of this query is looking for the word Test in the ProductName field, which may return say.. 10 results.

The second part is returning any result where the userData.windowsId = palkema, but what I want, is basically to not filter by that as a whole, just filter the sub property. So if I have 100 records where userData.windowsId = palkema, I get a potential of 110 records, but I only want the 10 records that match the query string lookup, and for those that don't have the match for the windowsId, I just want nulls in the inner\_hits property.

Is this possible?

```auto
    "query": {
        "bool": {
            "should": [
                {
                    "multi_match": {
                        "query": "Test",
                        "fields": [
                            "productName^1"
                        ],
                        "type": "best_fields",
                        "operator": "and"
                    }
                },
                {
                    "nested": {
                        "path": "userData",
                        "query": {
                            "bool": {
                                "should": [
                                    {
                                        "term": {
                                            "userData.windowsId": "palkema"
                                        }
                                    }
                                ]
                            }
                        },
                        "inner_hits": {},
                    }
                }
            ]
        }
    }

```

---

<div class="post-metadata">

**Author:** ![jessgarson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jessgarson/32/129841_2.png) [@jessgarson](https://discuss.elastic.co/u/jessgarson)\
**Post date:** [October 31, 2024, 6:45pm UTC](https://discuss.elastic.co/t/nested-query/369752/2 "2024-10-31T18:45:40Z")

</div>

Thanks for reaching out, @palkema. Would something like this work for you?

```auto
{
    "query": {
        "bool": {
            "must": [
                {
                    "multi_match": {
                        "query": "Test",
                        "fields": [
                            "productName^1"
                        ],
                        "type": "best_fields",
                        "operator": "and"
                    }
                }
            ],
            "should": [
                {
                    "nested": {
                        "path": "userData",
                        "query": {
                            "term": {
                                "userData.windowsId": "palkema"
                            }
                        },
                        "inner_hits": {}
                    }
                }
            ],
            "minimum_should_match": 0
        }
    }
}

```
