# Filtering with nested query inner\_hits count

**URL:** <https://discuss.elastic.co/t/filtering-with-nested-query-inner-hits-count/335202>\
**Category:** Elasticsearch\
**Created:** [June 5, 2023, 10:08am UTC](https://discuss.elastic.co/t/filtering-with-nested-query-inner-hits-count/335202 "2023-06-05T10:08:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![LaySoft](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/laysoft/32/65555_2.png) [@LaySoft](https://discuss.elastic.co/u/LaySoft)\
**Post date:** [June 5, 2023, 10:08am UTC](https://discuss.elastic.co/t/filtering-with-nested-query-inner-hits-count/335202/1 "2023-06-05T10:08:26Z")

</div>

I have the following query:

```auto
    "query": {
        "nested": {
            "path": "cuccok",
            "inner_hits": {},
            "query": {
                "bool": {
                    "must": [
                        {
                            "terms": {
                                "cuccok.point": [
                                    1,
                                    2
                                ]
                            }
                        },
                        {
                            "terms": {
                                "cuccok.tag": [
                                    3,
                                    4
                                ]
                            }
                        }
                    ]
                }
            }
        }
    }
}

```

The result is:

```auto
{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 2,
        "hits": [
            {
                "_index": "cucc",
                "_id": "IkREdogBdZE48qyFJ4QS",
                "_score": 2,
                "_source": {
                    "itemid": 1,
                    "cuccok": [
                        {
                            "point": 1,
                            "tag": 3
                        },
                        {
                            "point": 2,
                            "tag": 4
                        }
                    ]
                },
                "inner_hits": {
                    "cuccok": {
                        "hits": {
                            "total": {
                                "value": 2,
                                "relation": "eq"
                            },
                            "max_score": 2,
                            "hits": [
                                {
                                    "_index": "cucc",
                                    "_id": "IkREdogBdZE48qyFJ4QS",
                                    "_nested": {
                                        "field": "cuccok",
                                        "offset": 0
                                    },
                                    "_score": 2,
                                    "_source": {
                                        "point": 1,
                                        "tag": 3
                                    }
                                },
                                {
                                    "_index": "cucc",
                                    "_id": "IkREdogBdZE48qyFJ4QS",
                                    "_nested": {
                                        "field": "cuccok",
                                        "offset": 1
                                    },
                                    "_score": 2,
                                    "_source": {
                                        "point": 2,
                                        "tag": 4
                                    }
                                }
                            ]
                        }
                    }
                }
            },
            {
                "_index": "cucc",
                "_id": "I0REdogBdZE48qyFJ4Qf",
                "_score": 2,
                "_source": {
                    "itemid": 2,
                    "cuccok": [
                        {
                            "point": 1,
                            "tag": 3
                        },
                        {
                            "point": 2,
                            "tag": 5
                        }
                    ]
                },
                "inner_hits": {
                    "cuccok": {
                        "hits": {
                            "total": {
                                "value": 1,
                                "relation": "eq"
                            },
                            "max_score": 2,
                            "hits": [
                                {
                                    "_index": "cucc",
                                    "_id": "I0REdogBdZE48qyFJ4Qf",
                                    "_nested": {
                                        "field": "cuccok",
                                        "offset": 0
                                    },
                                    "_score": 2,
                                    "_source": {
                                        "point": 1,
                                        "tag": 3
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        ]
    }
}

```

I'd like to return only the first (itemid = 1) doc, where the nested query inner\_hits count = 2:

```auto
 "inner_hits": {
                    "cuccok": {
                        "hits": {
                            "total": {
                                "value": 2,
                                "relation": "eq"
                            },

```

How can I implement this?

---

<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 3, 2023, 10:08am UTC](https://discuss.elastic.co/t/filtering-with-nested-query-inner-hits-count/335202/2 "2023-07-03T10:08:37Z")

</div>

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