Term, nested documents and must_not query incompatible in ElasticSearch? Term, nested documents and must_not query incompatible in ElasticSearch? elasticsearch elasticsearch

Term, nested documents and must_not query incompatible in ElasticSearch?


Your original query would search in each individual nested object and eliminate the objects that don't match, but if there are some nested objects left, they do match with your query and so you get your results. This is because nested objects are indexed as a hidden separate document

Original code:

{  "query": {    "nested": {      "path": "tags",      "query": {        "bool": {          "must_not": {            "term": {              "tags.type": "delete"            }          }        }      }    }  }}

The solution is then quite simple really, you should bring the bool query outside the nested documents. Now all the documents are discarded who have a nested object with the "DELETE" type. Just what you wanted!

The solution:

{  "query": {    "bool": {      "must_not": {        "nested": {          "path": "tags",          "query": {            "term": {              "tags.type": "DELETE"            }          }        }      }    }  }}

NOTE: Your strings are "not analyzed" and you searched for "delete" instead of "DELETE". If you want to search case insensitive, make your strings analyzed


This should fix your problem: http://sense.qbox.io/gist/f4694f542bc76c29624b5b5c9b3ecdee36f7e3ea

Two most important things:

  1. include_in_root on "tags.type". This will tell ES to index tag types as "doc.tags.types" : ['DELETE', 'POSTS'], so you can access an array of those values "flattened" on the root doc . This means you no longer need a nested query (see #2)

  2. Drop the nested query.

 

{    "mappings": {        "docs" : {            "properties": {                "tags" : {                    "type": "nested",                    "properties" : {                        "type": {                           "type": "string",                           "index": "not_analyzed"                        }                    },                    "include_in_root": true                },                "label" : {                    "type": "string"                }            }        }    }}

 

{   "query": {      "bool": {         "must_not": {            "term": {               "tags.type": "DELETE"            }         }      }   }}