Home > OS >  Elasticsearch query that returns a result if all values ​are equal to a given parameter
Elasticsearch query that returns a result if all values ​are equal to a given parameter

Time:10-06

I need query which gives the following result: With multi values I want to give a result only if all values ​​is equal to given parameter. For example I have an array of ticket type. Then my query should only return a result if all ticket types is equal to, for example, 10. In this case if I have the next two objects then I only want to get objectTwo with parameter 10:

objectOne: [{ticketType:10}, {ticketType:20}]
objectTwo: [{ticketType:10}, {ticketType:10}]

I actually need some kind of != operator

CodePudding user response:

Boolean query with MUST_NOT is what you're looking for, ie :

POST yourindex/_search
{
  "query": {
        "bool" : {
          "must_not" : {
            "term" : {
              "ticketType" : 10
            }
          }
        }
  }
}

CodePudding user response:

I solved this with the combination of must and mus_not. Only must is not enough.

POST myIndex/_search
{
  "query": {
        "bool" : {
          "must" : {
            "term" : {
              "ticketType" : {
               "value": "10" // selected ticket type
               }
            }
          },
         "must_not" : {
            "terms" : {
              "ticketType" : [{
               "20","30"  // all other unselected ticket types
              }]
            }
        }
    }
  }
} 
  • Related