Unable to do range and script query on the same field

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
}

you cannot just list different queries behind each other. Take a look at the bool query and it's filter clause, which can be an array of queries.

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