Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple should queries with must query

I am building a query to Elastic 5 (using nest in .net), i am trying to achive this result:

Must have value1 and value 2

Should have value3 or value 4

and should have value5 or value6

Here is my query:

    {
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "code": {
              "query": "value1"
            }
          }
        },
        {
          "match": {
            "code": {
              "query": "value2"
            }
          }
        }
      ],
      "should": [
        {
          "match": {
            "code": {
              "query": "value3"
            }
          }
        },
        {
          "match": {
            "code": {
              "query": "value4"
            }
          }
        }
        ],
        "should": [
        {
          "match": {
            "code": {
              "query": "value5"
            }
          }
        },
        {
          "match": {
            "code": {
              "query": "value6"
            }
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

I dont get the desired answer (for example i dont have anywhere value 5 and value 6 but still getting results)

Thank you

like image 256
IB. Avatar asked Nov 13 '16 12:11

IB.


People also ask

Should and must query Elasticsearch?

must means: Clauses that must match for the document to be included. should means: If these clauses match, they increase the _score ; otherwise, they have no effect. They are simply used to refine the relevance score for each document. Yes you can use multiple filters inside must .

Should must not Elasticsearch?

Using must_not tells Elasticsearch that document matches cannot include any of the queries that fall under the must_not clause. should – It would be ideal for the matching documents to include all of the queries in the should clause, but they do not have to be included. Scoring is used to rank the matches.

What is bool query?

Boolean queryedit. A query that matches documents matching boolean combinations of other queries. The bool query maps to Lucene BooleanQuery . It is built using one or more boolean clauses, each clause with a typed occurrence.

What is bool Elasticsearch query?

Boolean, or a bool query in Elasticsearch, is a type of search that allows you to combine conditions using Boolean conditions. Elasticsearch will search the document in the specified index and return all the records matching the combination of Boolean clauses.


1 Answers

Then you need something like this:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "code": {
              "query": "value1"
            }
          }
        },
        {
          "match": {
            "code": {
              "query": "value2"
            }
          }
        },
        {
          "bool": {
            "minimum_should_match": 1,
            "should": [
              {
                "match": {
                  "code": {
                    "query": "value3"
                  }
                }
              },
              {
                "match": {
                  "code": {
                    "query": "value4"
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "minimum_should_match": 1,
            "should": [
              {
                "match": {
                  "code": {
                    "query": "value5"
                  }
                }
              },
              {
                "match": {
                  "code": {
                    "query": "value6"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}
like image 128
Val Avatar answered Oct 20 '22 02:10

Val