This is my 24th day of The August Update Challenge

Range query: Retrieves documents of a specific range of numeric, date, and string fields. Filtering numeric ranges is especially useful for data analysis. SQL > select * from commodity where the price is greater than 20 and less than 30

select good from goods where price>20 and price<30;
Copy the code

Select * from Elasticsearch; select * from Elasticsearch;

The operator instructions
gt Greater than (acronym of great than)
lt Less than (acronym for less than)
gte Greater than or equal (acronym of great than or equal)
lte (Acronym for less than or equal)

Numeric range query

Construct point test data:

POST /num_range_test_index/_doc/1
{
  "price": 10
}

POST /num_range_test_index/_doc/2
{
  "price": 20
}

POST /num_range_test_index/_doc/3
{
  "price": 30
}

POST /num_range_test_index/_doc/4
{
  "price": 22
}

POST /num_range_test_index/_doc/5
{
  "price": 15
}
Copy the code

Select * from Elasticsearch where price > 20 and less than 30

GET /num_range_test_index/_search { "query": { "constant_score": { "filter": { "range": { "price": { "gte": 20, "lte": 30}}}}}}Copy the code

Result returned:

{ "took" : 225, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : , "hits" : {0} "total" : {" value ": 3," base ":" eq "}, "max_score" : 1.0, "hits" : [{" _index ": "Num_range_test_index _type", "" :" _doc ", "_id" : "2", "_score" : 1.0, "_source" : {" price ": 20}}, {" _index" : "Num_range_test_index _type", "" :" _doc ", "_id" : "3", "_score" : 1.0, "_source" : {" price ": 30}}, {" _index" : "Num_range_test_index _type", "" :" _doc ", "_id" : "4", "_score" : 1.0, "_source" : {" price ": 22}}]}}Copy the code

Date-based range query

Construct point test data:

POST /date_range_test_index/_doc/1
{
  "market_at": "2021-07-01"
}

POST /date_range_test_index/_doc/2
{
  "market_at": "2021-08-01"
}

POST /date_range_test_index/_doc/3
{
  "market_at": "2021-08-22"
}
Copy the code

Select * from Elasticsearch where available date > 2021-08-01 and less than 2021-08-24

GET /date_range_test_index/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "range": {
          "market_at": {
            "gt": "2021-08-01",
            "lt": "2021-08-24"
          }
        }
      }
    }
  }
}
Copy the code

Result returned:

{ "took" : 0, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : {" total ": {" value" : 1, the "base" : "eq"}, "max_score" : 1.0, "hits" : [{" _index ": "Date_range_test_index _type", "" :" _doc ", "_id" : "3", "_score" : 1.0, "_source" : {" market_at ": "2021-08-22"}}]}Copy the code

String range query

String range queries are sorted by character order. For example, ‘a’ < ‘b’. Construct point test data:

POST /string_range_test_index/_doc/1
{
  "goods_name": "apple"
}

POST /string_range_test_index/_doc/2
{
  "goods_name": "banana"
}

POST /string_range_test_index/_doc/3
{
  "goods_name": "orange"
}
Copy the code

Select * from Elasticsearch where the name of the item is greater than or less than

GET /string_range_test_index/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "range": {
          "goods_name": {
            "gte": "a",
            "lt": "c"
          }
        }
      }
    }
  }
}
Copy the code

Result returned:

{ "took" : 0, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : {" total ": {" value" : 2, the "base" : "eq"}, "max_score" : 1.0, "hits" : [{" _index ": "String_range_test_index _type", "" :" _doc ", "_id" : "1", "_score" : 1.0, "_source" : {" goods_name ": "Apple"}}, {" _index ":" string_range_test_index ", "_type" : "_doc", "_id" : "2", "_score" : 1.0, "_source" : { "goods_name" : "banana" } } ] } }Copy the code