Query bool with must and should

I'm trying to build a bool query.

{
    "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

{
    "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?

Hi @sphawk

Do you query filter all records with language = "es_ES" or ignore them?

Hi @RabBit_BR
tnx for your answer.
I need all records with language = es_ES

In that case I would use Filter Query (and its benefits). I imagine language is keyword type.

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

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

{ "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"
I think this is not the best method...

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

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

1 Like

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