# How to filter nested object in Elasticsearch 7.5?

**URL:** <https://discuss.elastic.co/t/how-to-filter-nested-object-in-elasticsearch-7-5/234121>\
**Category:** Elasticsearch\
**Created:** [May 25, 2020, 9:35am UTC](https://discuss.elastic.co/t/how-to-filter-nested-object-in-elasticsearch-7-5/234121 "2020-05-25T09:35:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tanobi92](https://avatars.discourse-cdn.com/v4/letter/t/a9a28c/32.png) [@tanobi92](https://discuss.elastic.co/u/tanobi92)\
**Post date:** [May 25, 2020, 9:35am UTC](https://discuss.elastic.co/t/how-to-filter-nested-object-in-elasticsearch-7-5/234121/1 "2020-05-25T09:35:01Z")

</div>

I have a mapping:

```auto
    {
  "ntol-2020-05" : {
    "mappings" : {
      {
        "properties": {
          "_createdAt": {
            "type": "date"
          },
          "_logType": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "device": {
            "properties": {
              ...
            }
          },
          "resp": {
            "type": "nested",
            "properties": {
              "data": {
                "type": "nested",
                "properties": {
                  ...
                }
              }
            }
          }
        }
      }
    }
  }
}

```

I filter with three condition:

- "\_logType" is "crawler".
- "\_createdAt" on "2020-05-23".
- Size of "resp" = 0.

I am trying to filter with query:

```auto
GET ntol-2020-*/_search
   {
     "query": {
       "bool": {
         "must": [
           {
             "term": {
               "_logType": {
                 "value": "crawler"
               }
             }
           },
           {
             "range": {
               "_createdAt": {
                 "gte": "2020-05-23",
                 "lte": "2020-05-23",
                 "time_zone": "+07:00"
               }
             }
           },
           {
             "nested": {
               "path": "resp",
               "query": {
                 "script": {
                   "script": {
                     "source": "doc['resp'].size() > 0"
                   }
                 }
               }
             }
           }
         ]
       }
     },
     "from": 0,
     "size": 10
   }

```

It return error:

```auto
 {
  "type": "script_exception",
  "reason": "runtime error",
  "script_stack": [
    "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:94)",
    "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:41)",
    "doc['resp'].size() > 0",
    " ^---- HERE"
  ],
  "script": "doc['resp'].size() > 0",
  "lang": "painless",
  "caused_by": {
    "type": "illegal_argument_exception",
    "reason": "No field found for [resp] in mapping with types []"
  }
}

```

If I use script "doc.containsKey('resp') && doc['resp'].size() \> 0" then It will return hits length = 0.  
Help me. Thanks!

---

<div class="post-metadata">

**Author:** ![tanobi92](https://avatars.discourse-cdn.com/v4/letter/t/a9a28c/32.png) [@tanobi92](https://discuss.elastic.co/u/tanobi92)\
**Post date:** [June 5, 2020, 4:05am UTC](https://discuss.elastic.co/t/how-to-filter-nested-object-in-elasticsearch-7-5/234121/2 "2020-06-05T04:05:04Z")

</div>

It worked by this supprot: [https://stackoverflow.com/questions/62056365/how-to-filter-nested-object-in-elasticsearch-7-5](https://stackoverflow.com/questions/62056365/how-to-filter-nested-object-in-elasticsearch-7-5)

---

<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, 2020, 4:05am UTC](https://discuss.elastic.co/t/how-to-filter-nested-object-in-elasticsearch-7-5/234121/3 "2020-07-03T04:05:06Z")

</div>

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