afghani
afghani

Reputation: 676

Elasticsearch 6.7.0 Nested "bool", "should","must" queries

I am using ES_6.7 and sending Payload into it.

I'm getting correct output when I send "Payload" for single query but face problem for multiple.

Payload for single query: PAYLOAD =

  {
    "query": {"bool": {"must": [{"match": {"coreid": {"query": "2"} } }, "match": {"program_id": {"query": "86328" }}}]}}, 
    }

But face ERROR when I replicate above Query for three Inputs with "should" option:

{"query":{"bool":{"should":
[{"must":[{"match": {"coreid": {"query": "2"} } },
                            {"match": {"program_id": {"query": "86328" }}}
                            
                            ]
                    },
                    
                    {"must":[
                            {"match": {"coreid": {"query": "4"} } },
                            {"match": {"program_id": {"query": "86819" }}}
                            
                            ]
                    },
                    
                    {"must":[
                            {"match": {"coreid": {"query": "5"} } },
                            {"match": {"program_id": {"query": "95142" }}}
                            
                            ]
                    }
            ]       
        }
        }
}

ERROR message:

"type": "parsing_exception",

"reason": "no [query] registered for [query]",

"line": 5,

"col": 22,

"status": 400

Upvotes: 3

Views: 262

Answers (2)

NorwegianClassic
NorwegianClassic

Reputation: 1115

A bit late, but yes: encapsulate "must" with a "bool", also you can use "term" instead of "match" and "query". Not sure if the performance will be any better but in my opinion it's a bit cleaner.

{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "coreid": "2"
                }
              },
              {
                "term": {
                  "program_id": "35"
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "term": {
                  "coreid": "758"
                }
              },
              {
                "term": {
                  "program_id": "45678"
                }
              }
            ]
          }
        }

      ]
    }
  }
}

Upvotes: 1

afghani
afghani

Reputation: 676

This is working for me. I just added one extra "bool" condition at each level. Can I have any more optimization upon this??

{
"_source" :["coreid", "program_id", "program_name", "university_name", "city", "country"] , 
"query":{"bool": 
        {
            "should":
            [


                    {"bool":{
                        "must":[
                            {"match": {"coreid": {"query": "2"} } },
                            {"match": {"program_id": {"query": "86328" }}}

                            ]
                            }
                    },

                    {"bool":{
                        "must":[
                            {"match": {"coreid": {"query": "59"} } },
                            {"match": {"program_id": {"query": "95142" }}}

                            ]
                            }
                    },

                    {"bool":{
                        "must":[
                            {"match": {"coreid": {"query": "2"} } },
                            {"match": {"program_id": {"query": "86819" }}}

                            ]
                            }
                    }
            ],
            "minimum_should_match" : 1
            }       
        }
}

Upvotes: 0

Related Questions