How to filter nested object in Elasticsearch 7.5?

I have a mapping:

    {
  "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:

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:

 {
  "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!

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

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