Filtering the bool query results by the score or getting results only from one query

Let's say I got the below query, two match queries inside a should clause of a bool query.

I want to be able to conditionally select which queries inside my bool query are executed, I mean for example if the match query on the name field doesn't have results then don't bother executing the match query on the category field.

At least, is it possible to filter my results based on the score? I mean even if the second match query on the category field is executed, Would I be able to eliminate(filter) results coming from the second match query and keep only the results coming from the first?

{
"query": {
    "bool": {
        "should": [
            {
                "match": {
                    "name": {
                        "query": "textToSearch",
                        "fuzziness": 1,
                        "boost": 20
                    }
                }
            },
            {
                "match": {
                    "category": {
                        "query": "textToSearch",
                        "fuzziness": 1,
                        "boost": 10
                    }
                }
            }

        ]
    }	
}

}

I don't know if this would fulfill your requirements, but another approach, would be to filter the second match clause (to get a constant_score of 0). So the documents matching the should clause will get a score, and will appear on top.

POST index/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "match": {
            "category": {
              "query": "textToSearch",
              "fuzziness": 1,
              "boost": 10
            }
          }
        }
      ],
      "should": [
        {
          "match": {
            "name": {
              "query": "textToSearch",
              "fuzziness": 1,
              "boost": 20
            }
          }
        }
      ]
    }
  }
}

@klof Thanks for your answer, However, actually I don't want the second query results to appear,even as the last results of docs, I would like to eliminate them if the first query don't have results. If the first query had results then it's okay to have the second query results

Yes I had this use case once, you should make it nested and use a must clause :

POST index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": {
              "query": "textToSearch",
              "fuzziness": 1,
              "boost": 20
            }
          }
        },
        {
          "bool": {
            "should": [
              {
                "match": {
                  "category": {
                    "query": "textToSearch",
                    "fuzziness": 1,
                    "boost": 10
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}
1 Like

@klof I appreciate your answer, that's what I'm looking for

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.