elasticsearchelasticsearch-dslelasticsearch-dsl-py

Elasticsearch nested field not exist query in DSL python


I have a nested data mapping in a document and I want to query if a nested field does not exist.

This elastic query is working but what will be the elastic DSL representation on this query?

GET products/_search

{
  "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "attributes",
            "query": {
              "exists": {
                "field": "attributes.value"
              }
            }
          }
        }
      ]
    }
  }
}

I have tried with this, but it's not working

ProductDocument.search().query(
    "nested",
    path="attributes",
    query=(~Q('exists', field='attributes.value'))
)

This DSL query represent as and it's wrong I think

{
  "query": {
    "nested": {
      "path": "attributes",
      "query": {
        "bool": {
          "must_not": [{
            "exists": {
              "field": "attributes.value"
            }
          }]
        }
      }
    }
  }
}

N.B: I am using elastic 6.7, elasticsearch-dsl 6.4.2


Solution

  • Finally, I find out the query

    ProductDocument.search().query(~Q(
        "nested",
        path="attributes",
        query=Q("exists", field='attributes.value'))
    ))
    

    It might be helpful for someone