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

**URL:** https://discuss.elastic.co/t/filtering-the-bool-query-results-by-the-score-or-getting-results-only-from-one-query/135491
**Category:** Elasticsearch
**Created:** [June 12, 2018, 9:06am UTC](https://discuss.elastic.co/t/filtering-the-bool-query-results-by-the-score-or-getting-results-only-from-one-query/135491 "2018-06-12T09:06:16Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![intern](https://avatars.discourse-cdn.com/v4/letter/i/f04885/32.png) [@intern](https://discuss.elastic.co/u/intern)
#### Post date: [June 12, 2018, 9:06am UTC](https://discuss.elastic.co/t/filtering-the-bool-query-results-by-the-score-or-getting-results-only-from-one-query/135491/1 "2018-06-12T09:06:16Z")

</div>

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
                    }
                }
            }

        ]
    }	
}

```

}

---

<div class="post-metadata">

### Author: ![klof](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/klof/32/31590_2.png) [@klof](https://discuss.elastic.co/u/klof)
#### Post date: [June 13, 2018, 6:56am UTC](https://discuss.elastic.co/t/filtering-the-bool-query-results-by-the-score-or-getting-results-only-from-one-query/135491/2 "2018-06-13T06:56:46Z")

</div>

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
            }
          }
        }
      ]
    }
  }
}
```

---

<div class="post-metadata">

### Author: ![intern](https://avatars.discourse-cdn.com/v4/letter/i/f04885/32.png) [@intern](https://discuss.elastic.co/u/intern)
#### Post date: [June 13, 2018, 8:41am UTC](https://discuss.elastic.co/t/filtering-the-bool-query-results-by-the-score-or-getting-results-only-from-one-query/135491/3 "2018-06-13T08:41:26Z")

</div>

@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

---

<div class="post-metadata">

### Author: ![klof](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/klof/32/31590_2.png) [@klof](https://discuss.elastic.co/u/klof)
#### Post date: [June 13, 2018, 10:40am UTC](https://discuss.elastic.co/t/filtering-the-bool-query-results-by-the-score-or-getting-results-only-from-one-query/135491/4 "2018-06-13T10:40:28Z")

</div>

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
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}
```

---

<div class="post-metadata">

### Author: ![intern](https://avatars.discourse-cdn.com/v4/letter/i/f04885/32.png) [@intern](https://discuss.elastic.co/u/intern)
#### Post date: [June 13, 2018, 11:55am UTC](https://discuss.elastic.co/t/filtering-the-bool-query-results-by-the-score-or-getting-results-only-from-one-query/135491/5 "2018-06-13T11:55:16Z")

</div>

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

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 11, 2018, 11:55am UTC](https://discuss.elastic.co/t/filtering-the-bool-query-results-by-the-score-or-getting-results-only-from-one-query/135491/6 "2018-07-11T11:55:17Z")

</div>

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