Range queries with should clause not working

Hi,

I'm trying below range query with must and should clause:

Product id can range from 1 to 1000.
I'm using below query to fetch product_id between 1 to 99 or product_id = 100. However I can only see the must clause executed and the result has all the records with product_id in range of 1 to 99. I also want to fetch a record with product_id 100.

{
"query": {
"bool": {
"must": [
{
"range": {
"product_id ": {
"from": 90,
"to": "99"
}
}
}
],
"should": [
{"bool": {
"must": [
{
"term": {
"product_id ": {
"value": "100"
}
}
}]}}
]
}
}
}

Please suggest.

Could you provide a full recreation script as described in About the Elasticsearch category. It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script is something anyone can copy and paste in Kibana dev console, click on the run button to reproduce your use case. It will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

Hi,

Here is the recreation steps:

PUT /product
{
    "mappings" : {
        "properties" : {
			"product_id": { "type" : "long" },
			"product_name": { "type" : "keyword" }
        }}}
        

POST product/_doc
{
			"product_id": 22,
			"product_name": "abc"
}

POST product/_doc
{
			"product_id": 36,
			"product_name": "mobile"
}

POST product/_doc
{
			"product_id": 58,
			"product_name": "tablet"
}

POST product/_doc
{
			"product_id": 87,
			"product_name": "watch"
}

POST product/_doc
{
			"product_id": 100,
			"product_name": "footwear"
}

Execute below query-

GET product/_search?size=100
{
    "query": {
        "bool": {
            "must": [
                {
                    "range": {
                        "product_id": {
                            "from": 1,
                            "to": "99"
                        }
                    }
                }
            ],
            "should": [
              
                        {
                            "term": {
                                "product_id": {
                                    "value": "100"
                                }
                            }
                        }
            ]
        }
    }
}

Result:
{
  "took" : 735,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "product",
        "_type" : "_doc",
        "_id" : "P0GzcYgB6QnR66-pMZvJ",
        "_score" : 1.0,
        "_source" : {
          "product_id" : 36,
          "product_name" : "mobile"
        }
      },
      {
        "_index" : "product",
        "_type" : "_doc",
        "_id" : "3_CycYgBdvuNheCty-RK",
        "_score" : 1.0,
        "_source" : {
          "product_id" : 22,
          "product_name" : "abc"
        }
      },
      {
        "_index" : "product",
        "_type" : "_doc",
        "_id" : "4PCzcYgBdvuNheCtsuTl",
        "_score" : 1.0,
        "_source" : {
          "product_id" : 87,
          "product_name" : "watch"
        }
      },
      {
        "_index" : "product",
        "_type" : "_doc",
        "_id" : "QEGzcYgB6QnR66-pbptD",
        "_score" : 1.0,
        "_source" : {
          "product_id" : 58,
          "product_name" : "tablet"
        }
      }
    ]
  }
}

Expected result: Result should also display the record with product_id - 100.
Actual Result: Result doesn't have the record with product_id - 100.

Nope. Because of the must clause.

Put everything within the should array.

2 Likes

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