# Unable to do range and script query on the same field

**URL:** <https://discuss.elastic.co/t/unable-to-do-range-and-script-query-on-the-same-field/196862>\
**Category:** Elasticsearch\
**Created:** [August 27, 2019, 2:36am UTC](https://discuss.elastic.co/t/unable-to-do-range-and-script-query-on-the-same-field/196862 "2019-08-27T02:36:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aniruddha.raje](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aniruddha.raje/32/52148_2.png) [@aniruddha.raje](https://discuss.elastic.co/u/aniruddha.raje)\
**Post date:** [August 27, 2019, 2:36am UTC](https://discuss.elastic.co/t/unable-to-do-range-and-script-query-on-the-same-field/196862/1 "2019-08-27T02:36:35Z")

</div>

I am trying to do a query which has range and script operations on a field,  
I am able to run the query only if use either of the range or script operation, but not together.

**Following is the query -**

```
GET /listings/_search
{
		"query": {
		 "range" : {
        "forecast_quantity" : {
            "gte" : 250
        }
      },
			"constant_score": {
				"filter": {
					"bool": {
						"must": [{
							"script": {
								"script": {
									"source": "doc['forecast_quantity'].value > doc['minimum_quantity'].value",
									"params": {}
								}
							}
						}]
					}
				}
			}
		}
	}

```

**Following the data the I have -**

```
{
	"took": 0,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 2,
			"relation": "eq"
		},
		"max_score": 1.0,
		"hits": [{
				"_index": "listings",
				"_type": "_doc",
				"_id": "2",
				"_score": 1.0,
				"_source": {
					"listing_id": 2,
					"forecast_quantity": 222,
					"minimum_quantity": 200
				}
			},
			{
				"_index": "listings",
				"_type": "_doc",
				"_id": "3",
				"_score": 1.0,
				"_source": {
					"listing_id": 3,
					"forecast_quantity": 333,
					"minimum_quantity": 300
				}
			}
		]
	}
}

```

**I am getting the following error -**

```
{
  "error": {
    "root_cause": [
      {
        "type": "parsing_exception",
        "reason": "[range] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
        "line": 8,
        "col": 7
      }
    ],
    "type": "parsing_exception",
    "reason": "[range] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
    "line": 8,
    "col": 7
  },
  "status": 400
}
```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 28, 2019, 11:22am UTC](https://discuss.elastic.co/t/unable-to-do-range-and-script-query-on-the-same-field/196862/2 "2019-08-28T11:22:32Z")

</div>

you cannot just list different queries behind each other. Take a look at the [bool query](https://www.elastic.co/guide/en/elasticsearch/reference/7.3/query-dsl-bool-query.html) and it's `filter` clause, which can be an array of queries.

---

<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:** [September 25, 2019, 11:22am UTC](https://discuss.elastic.co/t/unable-to-do-range-and-script-query-on-the-same-field/196862/3 "2019-09-25T11:22:43Z")

</div>

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