# Query bool with must and should

**URL:** <https://discuss.elastic.co/t/query-bool-with-must-and-should/323135>\
**Category:** Elasticsearch\
**Created:** [January 13, 2023, 1:37pm UTC](https://discuss.elastic.co/t/query-bool-with-must-and-should/323135 "2023-01-13T13:37:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sphawk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sphawk/32/76990_2.png) [@sphawk](https://discuss.elastic.co/u/sphawk)\
**Post date:** [January 13, 2023, 1:37pm UTC](https://discuss.elastic.co/t/query-bool-with-must-and-should/323135/1 "2023-01-13T13:37:51Z")

</div>

I'm trying to build a bool query.

```auto
{
    "query": {
        "bool" : {
            "must" : [
                {"term": { "language": "es_ES" }}
            ],
            "should": [
                {
                    "query_string": {
                        "query": "ejemplo",
                        "fields": ["title^100", "file^1000", "subtitle^50", "file^20", "meta^10", "text"]
                    }
                }
            ]
        }
    }
}

```

this return

```auto
{
    "took": 11,
    "timed_out": false,
    "_shards": {
        "total": 69,
        "successful": 69,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 0,
        "max_score": null,
        "hits": []
    }
}

```

in theory, all record without "language" equal to "es\_ES" should be ignored, correct?

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [January 13, 2023, 1:59pm UTC](https://discuss.elastic.co/t/query-bool-with-must-and-should/323135/2 "2023-01-13T13:59:13Z")

</div>

Hi @sphawk

> [@sphawk](#):
>
> in theory, all record without "language" equal to "es\_ES" should be ignored, correct?

Do you query filter all records with language = "es\_ES" or ignore them?

---

<div class="post-metadata">

**Author:** ![sphawk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sphawk/32/76990_2.png) [@sphawk](https://discuss.elastic.co/u/sphawk)\
**Post date:** [January 13, 2023, 2:48pm UTC](https://discuss.elastic.co/t/query-bool-with-must-and-should/323135/3 "2023-01-13T14:48:28Z")

</div>

Hi @RabBit_BR  
tnx for your answer.  
I need all records with language = es\_ES

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [January 13, 2023, 3:02pm UTC](https://discuss.elastic.co/t/query-bool-with-must-and-should/323135/4 "2023-01-13T15:02:26Z")

</div>

In that case I would use [Filter Query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html#filter-context) (and its benefits). I imagine language is keyword type.

```auto
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "language": "es_ES"
          }
        }
      ],
      "should": [
        {
          "query_string": {
            "query": "ejemplo",
            "fields": [
              "title^100",
              "file^1000",
              "subtitle^50",
              "file^20",
              "meta^10",
              "text"
            ]
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![sphawk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sphawk/32/76990_2.png) [@sphawk](https://discuss.elastic.co/u/sphawk)\
**Post date:** [January 13, 2023, 3:12pm UTC](https://discuss.elastic.co/t/query-bool-with-must-and-should/323135/5 "2023-01-13T15:12:59Z")

</div>

I've not defined the type... 🤔 just created the bulk files like

```auto
{ "index" : { "_index" : "test", "_type" : "_doc", "_id" : "1935b0ba3b6fbe7045562a5a507b390a" } }
{"file":"example.pdf","external_link":"","date":"1991-07-16","date_approx":false,"title":"a title","subtitle":"","caption_it_IT":"{}",...,"language":"es_ES"}

```

and post using "[http://127.0.0.1:9200/\_bulk](http://127.0.0.1:9200/_bulk)"  
I think this is not the best method...

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [January 13, 2023, 3:15pm UTC](https://discuss.elastic.co/t/query-bool-with-must-and-should/323135/6 "2023-01-13T15:15:33Z")

</div>

So, try like this:

```
   {
      "term": {
        "language.keyword": "es_ES"
      }
    }

```

to check type Field Language try:

> GET idx\_name?filter\_path=idx\_name.mappings.properties.language

---

<div class="post-metadata">

**Author:** ![sphawk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sphawk/32/76990_2.png) [@sphawk](https://discuss.elastic.co/u/sphawk)\
**Post date:** [January 13, 2023, 3:21pm UTC](https://discuss.elastic.co/t/query-bool-with-must-and-should/323135/7 "2023-01-13T15:21:57Z")

</div>

awesome!  
with language.keyword work perfectly.  
thanks a lot @RabBit_BR

---

<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:** [February 10, 2023, 3:22pm UTC](https://discuss.elastic.co/t/query-bool-with-must-and-should/323135/8 "2023-02-10T15:22:12Z")

</div>

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