原文地址: https://www.elastic.co/guide/en/elasticsearch/reference/7.7/range.html, 原文档版权归 www.elastic.co 所有

Range datatypesedit

The following range types are supported:

integer_range

A range of signed 32-bit integers with a minimum value of -231 and maximum of 231-1.

float_range

A range of single-precision 32-bit IEEE 754 floating point values.

long_range

A range of signed 64-bit integers with a minimum value of -263 and maximum of 263-1.

double_range

A range of double-precision 64-bit IEEE 754 floating point values.

date_range

A range of date values represented as unsigned 64-bit integer milliseconds elapsed since system epoch.

ip_range

A range of ip values supporting either IPv4 or IPv6 (or mixed) addresses.

Below is an example of configuring a mapping with various range fields followed by an example that indexes several range types.

PUT range_index
{
  "settings": {
    "number_of_shards": 2
  },
  "mappings": {
    "properties": {
      "expected_attendees": {
        "type": "integer_range"
      },
      "time_frame": {
        "type": "date_range", 
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
      }
    }
  }
}

PUT range_index/_doc/1?refresh
{
  "expected_attendees" : { 
    "gte" : 10,
    "lte" : 20
  },
  "time_frame" : { 
    "gte" : "2015-10-31 12:00:00", 
    "lte" : "2015-11-01"
  }
}

date_range types accept the same field parameters defined by the date type.

Example indexing a meeting with 10 to 20 attendees.

Date ranges accept the same format as described in date range queries.

Example date range using date time stamp. This also accepts date math formatting. Note that "now" cannot be used at indexing time.

The following is an example of a term query on the integer_range field named "expected_attendees".

GET range_index/_search
{
  "query" : {
    "term" : {
      "expected_attendees" : {
        "value": 12
      }
    }
  }
}

The result produced by the above query.

{
  "took": 13,
  "timed_out": false,
  "_shards" : {
    "total": 2,
    "successful": 2,
    "skipped" : 0,
    "failed": 0
  },
  "hits" : {
    "total" : {
        "value": 1,
        "relation": "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "range_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "expected_attendees" : {
            "gte" : 10, "lte" : 20
          },
          "time_frame" : {
            "gte" : "2015-10-31 12:00:00", "lte" : "2015-11-01"
          }
        }
      }
    ]
  }
}

The following is an example of a date_range query over the date_range field named "time_frame".

GET range_index/_search
{
  "query" : {
    "range" : {
      "time_frame" : { 
        "gte" : "2015-10-31",
        "lte" : "2015-11-01",
        "relation" : "within" 
      }
    }
  }
}

Range queries work the same as described in range query.

Range queries over range fields support a relation parameter which can be one of WITHIN, CONTAINS, INTERSECTS (default).

This query produces a similar result:

{
  "took": 13,
  "timed_out": false,
  "_shards" : {
    "total": 2,
    "successful": 2,
    "skipped" : 0,
    "failed": 0
  },
  "hits" : {
    "total" : {
        "value": 1,
        "relation": "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "range_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "expected_attendees" : {
            "gte" : 10, "lte" : 20
          },
          "time_frame" : {
            "gte" : "2015-10-31 12:00:00", "lte" : "2015-11-01"
          }
        }
      }
    ]
  }
}

IP Rangeedit

In addition to the range format above, IP ranges can be provided in CIDR notation:

PUT range_index/_mapping
{
  "properties": {
    "ip_whitelist": {
      "type": "ip_range"
    }
  }
}

PUT range_index/_doc/2
{
  "ip_whitelist" : "192.168.0.0/16"
}

Parameters for range fieldsedit

The following parameters are accepted by range types:

coerce

Try to convert strings to numbers and truncate fractions for integers. Accepts true (default) and false.

boost

Mapping field-level query time boosting. Accepts a floating point number, defaults to 1.0.

index

Should the field be searchable? Accepts true (default) and false.

store

Whether the field value should be stored and retrievable separately from the _source field. Accepts true or false (default).