How to query for two fields in one and the same tuple in an array in ElasticSearch? -


let's there documents in index this:

{   "category":"2020",   "properties":[     {      "name":"foo",      "value":"2"     },     {      "name":"boo",      "value":"2"     }   ] }, {        "category":"2020",   "properties":[     {      "name":"foo",      "value":"8"     },     {      "name":"boo",      "value":"2"     }   ] } 

i'd query index in way return documents match "foo":"2"but not "boo":"2".

i tried write query matches both properties.name and properties.value, i'm getting false positives. need way tell elasticsearch name , value have part of same properties tuple.

how can that?

you need map properties nestedtype. mapping similar this:

{   "your_type": {     "properties": {       "category": {         "type": "string"       },       "properties": {         "type": "nested",         "properties": {           "name": {             "type": "string"           },           "value": {             "type": "string"           }         }       }     }   } } 

then, query match documents having "foo=2" in same tuple not "boo=2" in same tuple need use nested query accordingly, 1 below.

{   "query": {     "bool": {       "must": [         {           "nested": {             "path": "properties",             "query": {               "bool": {                 "must": [                   {                     "match": {                       "properties.name": "foo"                     }                   },                   {                     "match": {                       "properties.value": "2"                     }                   }                 ]               }             }           }         }       ],       "must_not": [         {           "nested": {             "path": "properties",             "query": {               "bool": {                 "must": [                   {                     "match": {                       "properties.name": "boo"                     }                   },                   {                     "match": {                       "properties.value": "2"                     }                   }                 ]               }             }           }         }       ]     }   } } 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -