Query DSL - Range Query

Range Query

范围查询

Returns documents that contain terms within a provided range.

返回包含指定范围内词条的文档。

Example request

The following search returns documents where the age field contains a term between 10 and 20.

下面的搜索返回age字段包含1020之间词条的文档。

GET /_search
{
    "query": {
        "range" : {
            "age" : {
                "gte" : 10,
                "lte" : 20,
                "boost" : 2.0
            }
        }
    }
}

Copy as cURLView in Console

Top-level parameters for range

range的一级参数

  • field

    (Required, object) Field you wish to search.

    (必填,对象)您要搜索的字段。

Parameters for field

field的参数

  • gt

    (Optional) Greater than.

    (可选)大于。

  • gte

    (Optional) Greater than or equal to.

    (可选)大于或等于。

  • lt

    (Optional) Less than.

    (可选)小于。

  • lte

    (Optional) Less than or equal to.

    (可选)小于或等于。

  • format

    (Optional, string) Date format used to convert date values in the query.

    (可选,字符串)用于转换date查询中的值的日期格式。

    By default, Elasticsearch uses the date format provided in the field's mapping. This value overrides that mapping format.For valid syntax, see format.

    默认情况下,Elasticsearch使用的field在mapping中提供的date format。这里的format值将覆盖mapping中的格式。其语法,请参见format

    警告:

    If aformatanddatevalue are incomplete, Elasticsearch replaces any missing year, month, or date component with the start ofUnix time, which is January 1st, 1970.

    如果formatdate值不完整,Elasticsearch会使用Unix time的开始(1970年1月1日)替换所有缺少的年,月或日期部分 。

    For example, if theformatvalue isdd, Elasticsearch converts agtevalue of10to1970-01-10T00:00:00.000Z`.

    例如,如果format值为dd,则Elasticsearch会将gte 10转换为1970-01-10T00:00:00.000Z

  • relation

    (Optional, string) Indicates how the range query matches values for range fields. Valid values are:

    (可选,字符串)指示范围查询如何匹配range 字段的值。有效值为:

    • INTERSECTS (Default)

      Matches documents with a range field value that intersects the query’s range.

      匹配文档的字段值范围与查询范围有交集。

    • CONTAINS

      Matches documents with a range field value that entirely contains the query’s range.

      匹配文档的字段值范围包含查询范围。

    • WITHIN

      Matches documents with a range field value entirely within the query’s range.

      匹配文档的字段值范围在查询范围内。

  • time_zone

    (Optional, string) Coordinated Universal Time (UTC) offset or IANA time zone used to convert date values in the query to UTC.

    (可选,字符串) 用于将查询中的date值转换为UTC的世界协调时间(UTC)的偏移量IANA时区

    Valid values are ISO 8601 UTC offsets, such as +01:00 or -08:00, and IANA time zone IDs, such as America/Los_Angeles.

    有效值是ISO 8601 UTC偏移,如+01:00或- 08:00和IANA时区的ID,如America/Los_Angeles

    For an example query using the time_zone parameter, see Time zone in range queries.

    例如用time_zone参数查询,参见 在时区range查询

    The time_zone parameter does not affect the date math value of now. now is always the current system time in UTC.

    警告:

    time_zone参数不影响nowdate mathnow始终是当前系统时间的UTC。

    However, the time_zone parameter does convert dates calculated using now and date math rounding. For example, the time_zone parameter will convert a value of now/d.

    但是,time_zone参数会转换使用nowdate math rounding的日期计算。例如,time_zone参数会将now/d转换为一个值。

  • boost

    (Optional, float) Floating point number used to decrease or increase the relevance scores of a query. Defaults to 1.0.

    (可选,float)浮点数,用于降低或增加查询的 相关性分数。默认为1.0

    You can use the boost parameter to adjust relevance scores for searches containing two or more queries.

    您可以使用该boost参数来调整包含两个或多个查询的搜索的相关性得分。

    Boost values are relative to the default value of 1.0. A boost value between 0 and 1.0 decreases the relevance score. A value greater than 1.0 increases the relevance score.

    boost的值是相对于默认值1.0的。介于01.0之间的值会降低相关性得分。大于1.0的值会增加相关性得分。

Notes

Using the range query with date fields

date字段使用range查询

When the field parameter is a date field datatype, you can use date math with the following parameters:

field参数是date数据类型时,可以将 date math与以下参数一起使用:

  • gt
  • gte
  • lt
  • lte

For example, the following search returns documents where the timestamp field contains a date between today and yesterday.

例如,下面的搜索返回timestamp字段的日期值在今天和昨天之间文档。

GET /_search
{
    "query": {
        "range" : {
            "timestamp" : {
                "gte" : "now-1d/d",
                "lt" :  "now/d"
            }
        }
    }
}

Copy as cURLView in Console

Date math and rounding

Elasticsearch rounds date math values in parameters as follows:

Elasticsearch 对参数中的date math值用下面的方式进行取整:

  • gt

    Rounds up to the lastest millisecond.

    For example, 2014-11-18||/M rounds up to 2014-11-30T23:59:59.999, excluding the entire month.

    向上取整到最近的毫秒。

    例如,2014-11-18||/M向上取整到2014-11-30T23:59:59.999`,不包括整个月。

  • gte

    Rounds down to the first millisecond.

    For example, 2014-11-18||/M rounds down to 2014-11-01, including the entire month.
    向下取整到第一毫秒。

    例如,2014-11-18||/M向下取整到2014-11-01,包括整个月。

  • lt

    Rounds down to the first millisecond.

    For example, 2014-11-18||/M rounds down to 2014-11-01, excluding the entire month.

    向下取整到第一毫秒。

    例如,2014-11-18||/M向下取整为2014-11-01,不包括整个月。

  • lte

    Rounds up to the lastest millisecond.

    For example, 2014-11-18||/M rounds up to 2014-11-30T23:59:59.999, including the entire month.
    向上取整到最近的毫秒。

    例如,2014-11-18||/M向上取整到2014-11-30T23:59:59.999,包括整个月。

Example query using time_zone parameter

You can use the time_zone parameter to convert date values to UTC using a UTC offset. For example:

您可以使用time_zone参数用UTC偏移量将date的值转换为UTC。例如:

GET /_search
{
  "query": {
    "range": {
      "timestamp": {
        "time_zone": "+01:00",        
        "gte": "2020-01-01T00:00:00", 
        "lte": "now"                  
      }
    }
  }
}

Copy as cURLView in Console

  • Indicates that date values use a UTC offset of +01:00.

  • With a UTC offset of +01:00, Elasticsearch converts this date to 2019-12-31T23:00:00 UTC.

  • The time_zone parameter does not affect the now value.

  • 指示date值使用UTC偏移量+01:00

  • 使用UTC偏移量+01:00,Elasticsearch将此日期转换为 2019-12-31T23:00:00 UTC

  • time_zone参数不影响该now值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值