Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch 2.4, Exists filter for nested objects not working

My mapping is:

"properties": {
  "user": {
    "type": "nested",
    "properties": {
      "id": {
        "type": "integer"
      },
      "is_active": {
        "type": "boolean",
        "null_value": false
      },
      "username": {
        "type": "string"
      }
    }
  },

I want to get all documents that do not have a user field.

I tried:

GET /index/type/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "exists": {
            "field": "user"
          }
        }
      ]
    }
  }
}

Which returns all documents. Based on ElasticSearch 2.x exists filter for nested field doesn't work, I also tried:

GET /index/type/_search
{
  "query": {
    "nested": {
      "path": "user",
      "query": {
        "bool": {
          "must_not": [
            {
              "exists": {
                "field": "user"
              }
            }
          ]
        }
      }
    }
  }
}

Which returns 0 documents.

What is the correct query to get all documents missing the user field?

like image 657
pmishev Avatar asked Dec 21 '16 12:12

pmishev


People also ask

How do you query a nested field?

You can search nested fields using dot notation that includes the complete path, such as obj1.name . Multi-level nesting is automatically supported, and detected, resulting in an inner nested query to automatically match the relevant nesting level, rather than root, if it exists within another nested query.

What is nested in elastic search?

The nested type is a specialised version of the object data type that allows arrays of objects to be indexed in a way that they can be queried independently of each other.

What is nested datatype?

Nested data types are structured data types for some common data patterns. Nested data types support structs, arrays, and maps. A struct is similar to a relational table. It groups object properties together.

What is a nested field?

When a packed class contains an instance field that is a packed type, the data for that field is packed directly into the containing class. The field is known as a nested field .


1 Answers

I found the correct syntax, it should have been:

GET /index/type/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "user",
            "query": {
              "exists": {
                "field": "user"
              }
            }
          }
        }
      ]
    }
  }
}
like image 170
pmishev Avatar answered Nov 15 '22 06:11

pmishev