Elasticsearch 2.4, Exists filter for nested objects not working

你离开我真会死。 提交于 2020-01-02 08:52:36

问题


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?


回答1:


I found the correct syntax, it should have been:

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



回答2:


Try using the parent of the user, here obj

GET users/users/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "exists": {
            "field": "obj.user"
          }
        }
      ]
    }
  }
}   


来源:https://stackoverflow.com/questions/41262668/elasticsearch-2-4-exists-filter-for-nested-objects-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!