# Filter by count of inner\_hits?

**URL:** <https://discuss.elastic.co/t/filter-by-count-of-inner-hits/271298>\
**Category:** Elasticsearch\
**Created:** [April 26, 2021, 8:08pm UTC](https://discuss.elastic.co/t/filter-by-count-of-inner-hits/271298 "2021-04-26T20:08:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bkazez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bkazez/32/25991_2.png) [@bkazez](https://discuss.elastic.co/u/bkazez)\
**Post date:** [April 26, 2021, 8:08pm UTC](https://discuss.elastic.co/t/filter-by-count-of-inner-hits/271298/1 "2021-04-26T20:08:12Z")

</div>

I'm using ES to [search movements of baroque music](https://www.vmii.org/), so someone can find e.g. `music for flute, violin, and soprano` or for `2 violins and soprano`. The following works:

1. **Multilingual instrument names** `flûte à bec` should find music for recorder
2. **Generic instrument names** `violin` should find music for [viola d'amore](https://en.wikipedia.org/wiki/Viola_d%27amore) but not vice versa
3. **Meta instruments**"violin" should find music for "dessus" (high instrument), but rank it lower
4. **Boolean instruments**"violin AND flute" should find music for both of those, but shouldn't find something for "violin XOR flute" I solved this with nested docs:

```auto
{
  "movement_title": "...",
  "instrumentations": [
    {
      "meta_instrument": "dessus",
      "instrument_role_options": [
        {
          "instrument_name": "violon",
          "instrument_name_generic": "violin"
        },
        {
          "instrument_name": "flûte allemande",
          "instrument_name_generic": "flute"
        }
       ]
    },
    {
      "meta_instrument": null,
      "instrument_role_options": [
        {
          "instrument_name": "soprano",
          "instrument_name_generic": "soprano"
         }
      ]
    }
  ]
}

```

However, I'm stuck on these:

1. **Multiple instruments** `violin and flute` should find anything for violin and flute, plus anything with "2 dessus" (2 high instruments) but with range OK for violin and flute.
2. **Multiple instrument ranges** `playable on 2 flutes` should find music for 2 violins that is within the flute's range.

I considered storing counts like

```auto
    { "instrument": "flute", "count": 2 },
    { "instrument": "violin", "count": 1 },
    { "instrument": "dessus", "count": 3 },

```

But that won't work for #5 or even #3.

**Can anyone think of a way to structure this better so that I can filter by counts of nested items, like "2 flutes"?**

**Otherwise, is it possible to write a script filter that operates on analyzed text fields?** (It looks like [there are no doc\_values on analyzed text](https://www.elastic.co/guide/en/elasticsearch/reference/current/doc-values.html).) I saw that Painless even has a Dictionary type that could help me count everything necessary all at once, but I thought there must be a better way…

Thanks for any ideas!

Ben

---

<div class="post-metadata">

**Author:** ![bkazez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bkazez/32/25991_2.png) [@bkazez](https://discuss.elastic.co/u/bkazez)\
**Post date:** [April 27, 2021, 7:16pm UTC](https://discuss.elastic.co/t/filter-by-count-of-inner-hits/271298/2 "2021-04-27T19:16:36Z")

</div>

It seems like the best option is to filter by `inner_hits` on the client side — that way ES can handle the complex synonym logic. But then I can't get a count of total results, and I'll run into performance problems as the dataset grows. And I don't think scripts can access `inner_hits`?

---

<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:** [May 25, 2021, 7:17pm UTC](https://discuss.elastic.co/t/filter-by-count-of-inner-hits/271298/3 "2021-05-25T19:17:08Z")

</div>

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