5

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?

4

2 回答 2

10

我找到了正确的语法,它应该是:

GET /index/type/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "user",
            "query": {
              "exists": {
                "field": "user"
              }
            }
          }
        }
      ]
    }
  }
}
于 2017-01-04T10:30:24.813 回答
0

尝试使用用户的父级,这里是obj

GET users/users/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "exists": {
            "field": "obj.user"
          }
        }
      ]
    }
  }
}   
于 2016-12-21T15:09:25.667 回答