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
字段包含10
到20
之间词条的文档。
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 thefield
's mapping. This value overrides that mapping format.For valid syntax, seeformat
.默认情况下,Elasticsearch使用的
field
在mapping中提供的date format
。这里的format值将覆盖mapping中的格式。其语法,请参见format
。警告:
If a
formatand
datevalue are incomplete, Elasticsearch replaces any missing year, month, or date component with the start of
Unix time, which is January 1st, 1970.
如果
format
和date
值不完整,Elasticsearch会使用Unix time的开始(1970年1月1日)替换所有缺少的年,月或日期部分 。For example, if the
formatvalue is
dd, Elasticsearch converts a
gtevalue of
10to
1970-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 asAmerica/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 inrange
queries.例如用
time_zone
参数查询,参见 在时区range
查询。The
time_zone
parameter does not affect the date math value ofnow
.now
is always the current system time in UTC.警告:
time_zone
参数不不影响now
的date math。now
始终是当前系统时间的UTC。However, the
time_zone
parameter does convert dates calculated usingnow
and date math rounding. For example, thetime_zone
parameter will convert a value ofnow/d
.但是,
time_zone
参数会转换使用now
和 date 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 between0
and1.0
decreases the relevance score. A value greater than1.0
increases the relevance score.boost的值是相对于默认值
1.0
的。介于0
和1.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 to2014-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 to2014-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 to2014-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 to2014-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 to2019-12-31T23:00:00 UTC
. -
The
time_zone
parameter does not affect thenow
value. -
指示
date
值使用UTC偏移量+01:00
。 -
使用UTC偏移量
+01:00
,Elasticsearch将此日期转换为2019-12-31T23:00:00 UTC
。 -
time_zone
参数不影响该now
值。