# Body optimisation with nested objects / filter

**URL:** <https://discuss.elastic.co/t/body-optimisation-with-nested-objects-filter/295964>\
**Category:** Elasticsearch\
**Created:** [February 1, 2022, 4:51pm UTC](https://discuss.elastic.co/t/body-optimisation-with-nested-objects-filter/295964 "2022-02-01T16:51:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emporea](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emporea/32/101071_2.png) [@Emporea](https://discuss.elastic.co/u/Emporea)\
**Post date:** [February 1, 2022, 4:51pm UTC](https://discuss.elastic.co/t/body-optimisation-with-nested-objects-filter/295964/1 "2022-02-01T16:51:06Z")

</div>

I have a working query, what I am asking is if there is a more simple way for my query.  
I dont understand why using filter has to be so complicated. Am I missing something? I have such a terribly nested body.

I have an array of object as a field and declared it as nested.

2 sample rows of my document `title` field are this:

```auto
[
  [ #row 1 of document, field "title"
    { token: "the", POS: "DET" },
    { token: "life", POS: "PROPN" },
    { token: "legacy", POS: "PROPN" },
    { token: "poitier", POS: "PROPN" },
  ],
  [ #row 2 of document, field "title"
    { token: "life", POS: "PROPN" },
    { token: "legacy", POS: "PROPN" },
    { token: "’", POS: "PART" },
    { token: "reasoning", POS: "NOUN" },
    { token: "expulsion", POS: "PROPN" },
  ],
]

```

When I am aggregating I filter for the POS. In this example, I am doing a term aggregation for every token whose POS is "NOUN" or "PROPN". In the result I want to include the POS `(if the tokenizer was bad it could have multiple POS per token, thats why I am also doing a term aggregation with size 0)`

My response should ideally look like this:

```auto
[
  { key: "life", doc_count: 2, includePOS: "PROPN" },
  { key: "legacy", doc_count: 2, includePOS: "PROPN" },
  { key: "poitier", doc_count: 1, includePOS: "PROPN" },
  { key: "reasoning", doc_count: 1, includePOS: "NOUN" },
  { key: "expulsion", doc_count: 1, includePOS: "PROPN" },
]

```

But because my search query is so nested the body is reponse is also very nested. This is my query:

```auto
{
  query: {
    query_string: {
      query: "date:[now/d TO now]",
    },
  },
  aggs: {
    title: {
      nested: {
        path: "title",
      },
      aggs: {
        title: {
          filter: {
            terms: {
              "title.POS.keyword": ["NOUN", "PROPN"],
            },
          },
          aggs: {
            title: {
              terms: {
                field: "title.token.keyword",
                size: 10,
              },
              aggs: {
                includesome: {
                  terms: {
                    field: "title.POS.keyword",
                    size: 1,
                  },
                },
              },
            },
          },
        },
      },
    },
  },
}

```

Is there any way of writing my query more simple?

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [February 2, 2022, 2:44am UTC](https://discuss.elastic.co/t/body-optimisation-with-nested-objects-filter/295964/2 "2022-02-02T02:44:37Z")

</div>

> [@Emporea](#):
>
> I dont understand why using filter has to be so complicated.

One reason might be using nested fields. Though it may have side effects, if you use flat data structure for tokens the query will be much more simple:

```auto
{ book_id: 1, token_seq:0, token: "the", POS: "DET"}
{ book_id: 1, token_seq:1, token: "life", POS: "PROPN" }...

```

---

<div class="post-metadata">

**Author:** ![Emporea](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emporea/32/101071_2.png) [@Emporea](https://discuss.elastic.co/u/Emporea)\
**Post date:** [February 2, 2022, 11:06am UTC](https://discuss.elastic.co/t/body-optimisation-with-nested-objects-filter/295964/3 "2022-02-02T11:06:46Z")

</div>

I need to use nested fields, otherwise the connection between token and POS gets lost.

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [February 2, 2022, 11:10am UTC](https://discuss.elastic.co/t/body-optimisation-with-nested-objects-filter/295964/4 "2022-02-02T11:10:41Z")

</div>

I meant the structure that one document for one token though it is a radical change.

---

<div class="post-metadata">

**Author:** ![Emporea](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emporea/32/101071_2.png) [@Emporea](https://discuss.elastic.co/u/Emporea)\
**Post date:** [February 2, 2022, 11:11am UTC](https://discuss.elastic.co/t/body-optimisation-with-nested-objects-filter/295964/5 "2022-02-02T11:11:57Z")

</div>

Thats not really possible, there are alot of other fields. One Array refers to data in one doc.

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [February 2, 2022, 12:09pm UTC](https://discuss.elastic.co/t/body-optimisation-with-nested-objects-filter/295964/6 "2022-02-02T12:09:22Z")

</div>

I have no idea, sorry.  
I'm not sure whether you are already using, one thing I can tell is that it is possible to filter the output by filter\_path parameter.  
`GET /your_index/_search?filter_path=aggregations.title`

---

<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:** [March 2, 2022, 12:09pm UTC](https://discuss.elastic.co/t/body-optimisation-with-nested-objects-filter/295964/7 "2022-03-02T12:09:33Z")

</div>

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