In elasticsearch must match one of the array, with an additional additional query term

I am trying to create an elasticsearch query that must match one of the three queries in separate fields, and also have an additional query that is not required to match. The problem with executing bool requests and the MUST clause is that it must match all 3, and with SHOULD this not always match those that are required, unless minis_should_match is set to 2. In this case, it does not match the documents that match one of the three required due to minimal matching.

My current request is like this (sorry, I have no code here)

query - bool - must - query 1 - query 2 - query 3 - should - query 4

I also tried the following, but it does not make the 4th request optional

query - bool - should - query 1 - query 2 - query 3 - query 4 - minimum_should_match: 2

How to do this and create a query that MUST match one of the three queries and, if desired, evaluate those with a fourth query higher.

+4
source share
1 answer

The bool nested queries should provide you with the results you are looking for. We need to match either query one, two or three, and optionally the corresponding query 4.

"query": {
    "bool": {
        "must": [
           {
               "bool": {
                   "should": [
                      {
                          //query1
                      },
                      {
                          //query2
                      },
                      {
                          //query3
                      }
                   ],
                   "minimum_number_should_match": 1
               }
           }
        ],
        "should": [
           {
               //query4
           }
        ]
    }
}

The request must match at least one of the request sentences from 1 to 3 and does not have to match request 4.

Documents that match queries 1,2,3 and 4 will be simultaneously ranked highest, as I suggested that you did not mean exceptional or.

+6
source

All Articles