Range query on numeric types

Suppose we are storing products with their prices in an Elasticsearch index and we want to get all products within a range. The following is the query to get products in the range of $10 to $20:

GET /amazon_products/_search
{
"query": {
"range": {
"price": {
"gte": 10,
"lte": 20
}
}
}
}

The response of this query looks like the following:

{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total" : {
"value" : 201, 1
"relation" : "eq"
},
"max_score": 1.0, 2
"hits": [
{
"_index": "amazon_products",
"_type": "_doc",
"_id": "AV5lK4WiaMctupbz_61a",
"_score": 1, 3
"_source": {
"price": "19.99", 4
"description": "reel deal casino championship edition (win 98 me nt 2000 xp)",
"id": "b00070ouja",
"title": "reel deal casino championship edition",
"manufacturer": "phantom efx",
"tags": []
}
},

Please take a note of the following:

  • The hits.total.value field in the response shows how many search hits were found. Here, there were 201 search hits.
  • The hits.max_score field shows the score of the best matching document for the query. Since a range query is a structured query without any importance or relevance, it is executed as a filter. It doesn't do the scoring. All documents have a score of one.
  • The hits.hits array lists all the actual hits. Elasticsearch doesn't return all 201 hits in a single pass by default. It just returns the first 10 records. If you wish to scroll through all results, you can do so easily by issuing multiple queries, as we will see later.
  • The price field in all search hits would be within the requested range, that is, 10: <= price <= 20.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset