Search code examples
elasticsearchfilternesteddate-range

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:

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


Solution

  • You can use exists to return documents where "nested" field "resp" has value.

    {
      "query": {
        "bool": {
          "must": [
            {
              "nested": {
                "path": "resp",
                "query": {
                  "bool": {
                    "filter": {
                      "exists": {
                        "field": "resp"
                      }
                    }
                  }
                }
              }
            }
          ]
        }
      },
      "from": 0,
      "size": 10
    }