ElasticSearch: Filter for documents where any value in an array field is not in a list -


i have situation have field containing array of integers. need able implement query filters out documents contain only set of values in array.

for example, created following index:

put /test/stuff/1 {     "foo": [1,2,3] }  put /test/stuff/2 {     "foo": [2] }  put /test/stuff/3 {     "foo": [1,3] } 

now documents "foo" contains value not in [2,4]. want have documents ids 1 , 3 returned. not document 1 contain value 2, contains other values. simple must_not filter out document 1:

post /test/stuff/_search {     "query": {          "bool": {             "must" : {                 "match_all": {}             },             "filter" : {                 "bool": {                     "must_not": [                         {                             "terms" : {"foo" : [2,4]}                         }                     ]                 }             }         }     } } 

the above query match document 3. there way rewrite include document 1?

this doable script query, illustrated painless follows:

{     "query": {          "bool": {             "must" : {                 "match_all": {}             },             "filter" : {                 "bool": {                     "must": {                         "script" : {                             "script" : {                             "inline" : "for(int i: doc['foo']) { boolean matches = true; for(int j: params.param1){if(i==j) matches = false;} if(matches) return true;} ",                             "lang"   : "painless",                             "params" : { "param1": [2,4]}                             }                         }                     }                  }             }         }     } } 

Comments