# Range 查询(范围查询)

通过某一个包含项的字段匹配文档。 **Lucene**查询的类型取决于字段类型，对于*string* （字符串字段），查询是个**TermRangeQuery**，而对于*number / date*字段，查询是一个 **NumericRangeQuery** 。下面这个示例返回*age*在*10*到*20*之间的所有文档。

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

范围查询接受以下参数：

| 参数    | 说明                     |
| ----- | ---------------------- |
| gte   | 大于或等于                  |
| gt    | 大于                     |
| lte   | 小于或等于                  |
| lt    | 小于                     |
| boost | 设置查询的*boost*值，默认为*1.0* |

## date （日期）字段的范围查询 <a href="#id-fan-wei-cha-xun-date-ri-qi-zi-duan-de-fan-wei-cha-xun" id="id-fan-wei-cha-xun-date-ri-qi-zi-duan-de-fan-wei-cha-xun"></a>

对类型为[**date**](https://www.elastic.co/guide/en/elasticsearch/reference/current/date.html)（日期）的字段运行**range** （范围）查询时，可以使&#x7528;*“*[*Date Math*](https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#date-math)*”*&#x90E8;分指定范围：

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

### Date math 和 rounding （四舍五入） <a href="#id-fan-wei-cha-xun-datemath-he-rounding-si-she-wu-ru" id="id-fan-wei-cha-xun-datemath-he-rounding-si-she-wu-ru"></a>

当使用[**date math**](https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#date-math)将日期*round*（四舍五入）到最近的日，月，小时等时，舍入日期取决于范围的结束是包含还是排除。

向上舍入移动到舍入范围的最后一个毫秒，并向下舍入到舍入范围的第一个毫秒。

例如：

| 参数  | 说明                                                                    |
| --- | --------------------------------------------------------------------- |
| gt  | 大于向上舍入的日期：*2014-11-18 \|\| / M*变成*2014-11-30T23：59：59.999*，即不包括整个月。   |
| gte | 大于或等于向下舍入的日期：*2014-11-18 \|\| / M*即成为*2014-11-01*，即包括整个月。             |
| lt  | 小于向下舍入的日期：*2014-11-18 \|\| / M*成为*2014-11-01*，即不包括整个月。                |
| lte | 小于或等于向上舍入的日期：*2014-11-18 \|\| / M*成为*2014-11-30T23：59：59.999*，即包括整个月。 |

### Range （范围）查询中的日期格式化 <a href="#id-fan-wei-cha-xun-range-fan-wei-cha-xun-zhong-de-ri-qi-ge-shi-hua" id="id-fan-wei-cha-xun-range-fan-wei-cha-xun-zhong-de-ri-qi-ge-shi-hua"></a>

格式化的日期将使用指定的默认[**format**](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html)（格式）解析[**date**](https://www.elastic.co/guide/en/elasticsearch/reference/current/date.html)（日期）字段，但可以通过将格式参数传递到**range** （范围）查询来覆盖默认格式：

```
GET _search
{
    "query": {
        "range" : {
            "born" : {
                "gte": "01/01/2012",
                "lte": "2013",
                "format": "dd/MM/yyyy||yyyy"
            }
        }
    }
}
```

### Range （范围）查询中的时区 <a href="#id-fan-wei-cha-xun-range-fan-wei-cha-xun-zhong-de-shi-qu" id="id-fan-wei-cha-xun-range-fan-wei-cha-xun-zhong-de-shi-qu"></a>

通过在日期值本身指定时区（如果[**format**](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html)允许），日期可以从另一个时区转换为**UTC**，或者可以将其指定为*time\_zone*参数：

```
GET _search
{
    "query": {
        "range" : {
            "timestamp" : {
                "gte": "2015-01-01 00:00:00", ①
                "lte": "now", ②
                "time_zone": "+01:00"
            }
        }
    }
}
```

①此日期将转换为*2014-12-31T23：00：00 UTC*。

② *now*不受*time\_zone*参数的影响（日期必须存储为UTC）。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xiaoxiami.gitbook.io/elasticsearch/ji-chu/35query-dsldslfang-shi-cha-8be229/354zhu-yu-ji-bie-cha-8be228-term-level-queries/range-cha-8be228-fan-wei-cha-8be229.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
