# FieldData or Nested optimization. Doubt

**URL:** <https://discuss.elastic.co/t/fielddata-or-nested-optimization-doubt/214877>\
**Category:** Elasticsearch\
**Created:** [January 13, 2020, 4:41pm UTC](https://discuss.elastic.co/t/fielddata-or-nested-optimization-doubt/214877 "2020-01-13T16:41:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![EliuFlorez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eliuflorez/32/40884_2.png) [@EliuFlorez](https://discuss.elastic.co/u/EliuFlorez)\
**Post date:** [January 13, 2020, 4:41pm UTC](https://discuss.elastic.co/t/fielddata-or-nested-optimization-doubt/214877/1 "2020-01-13T16:41:51Z")

</div>

Hi guys, I find myself making some changes to an old scrits and I have the doubt that it would be more optimal if I continue using ' **fielddata**' or ' **nested**' that is to say the scripts do the searches or aggregations from a field like this: `"categoryId ":" L: 1001 L: 1016 L: 1023 L: 1029 L: 1036 "` so what I want is to establish a ' **nested**' to search by ID within it. according to reading 'fielddata' consumes a lot of memory but nested consumes more or is it more optimal at the time of truth?

**Example FieldData:**

```auto
POST listing/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "status": true
          }
        }
      ]
    }
  },
  "post_filter": {
    "bool": {
      "filter": [
        {
          "terms": {
            "categoryId": [
              1
            ]
          }
        }
      ]
    }
  },
  "aggs": {
    "category": {
      "terms": {
        "field": "categoryId"
      }
    }
  },
  "size": 1
}

```

**Example Nested:**

```auto
POST listing/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "status": true
          }
        }
      ]
    }
  },
  "post_filter": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "categoryData",
            "query": {
              "bool": {
                "must": [
                  {
                    "terms": {
                      "categoryData.id": [
                        1
                      ]
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "aggsCategory": {
      "nested": {
        "path": "categoryData"
      },
      "aggs": {
        "category": {
          "terms": {
            "field": "categoryData.id",
            "size": 100
          }
        }
      }
    }
  },
  "size": 10
}

```

---

<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:** [February 10, 2020, 4:41pm UTC](https://discuss.elastic.co/t/fielddata-or-nested-optimization-doubt/214877/2 "2020-02-10T16:41:57Z")

</div>

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