elasticsearch - Nested search bool query with must and should operators -


i'm trying nested aggregation , apply filter on it.

see following example:

car

  • brand
  • name
  • features

features

  • name
  • value

here's test data:

car_a: {  brand :"vw",  name: "golf",  features: [   {name: "color", value: "black"},   {name: "power", value: "150"}  ] }  car_b: {  brand :"vw",  name: "golf",  features: [   {name: "color", value: "blue"},   {name: "power", value: "150"}  ] }  car_c: {  brand :"vw",  name: "golf",  features: [   {name: "color", value: "white"},   {name: "power", value: "150"}  ] }  car_d: {  brand :"bmw",  name: "x3",  features: [   {name: "color", value: "white"},   {name: "power", value: "180"}  ] }  car_e: {  brand :"bmw",  name: "x5",  features: [   {name: "color", value: "blue"},   {name: "power", value: "250"}  ] }  car_f: {  brand :"bmw",  name: "x3",  features: [   {name: "color", value: "blue"},   {name: "power", value: "150"}  ] } 

and here's query:

"query": { "nested": {   "path": "features",   "query": {     "bool": {       "should": [         {           "match": {             "features.color": "blue"           }         },         {           "match": {             "features.color": "white"           }         }       ],       "must": [         {"match": {           "features.power": 150         }}       ]     }   }  } }  

the query result a,b,c,f

the expected result should b,c,f (color=blue or color=white) and power=150

try query:

"query": { "nested": {   "path": "features",   "query": {     "bool": {       "should": [         {           "match": {             "features.color": "blue"           }         },         {           "match": {             "features.color": "white"           }         }       ],       "must": [         {"match": {           "features.power": 150         }}       ]     }   }  } }  

meaning, fields use in query should prefixed name of path: features.color, features.power.

edit:

try query (i missed essential here - need 2 musts, 1 being bool shoulds):

{   "query": {     "nested": {       "path": "features",       "query": {         "bool": {           "must": [             {               "match": {                 "features.power": 150               }             },             {               "bool": {                 "should": [                   {                     "match": {                       "features.color": "blue"                     }                   },                   {                     "match": {                       "features.color": "white"                     }                   }                 ]               }             }           ]         }       }     }   } } 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -