ElasticSearch 查询语法
match 查询
match 查询是最常用的查询类型之一,用于全文搜索。它会对查询字符串进行分词,然后在指定字段中搜索这些词。
GET /my_index/_search
{
"query": {
"match": {
"message": "ElasticSearch 查询语法"
}
}
}term 查询
term 查询用于精确匹配一个具体的值。适用于结构化数据的精确搜索。
GET /my_index/_search
{
"query": {
"term": {
"status": "active"
}
}
}terms 查询
terms 查询是一种用于匹配多个值的查询类型。它通常用于在一个字段中搜索多个不同的值。
{
"query": {
"terms": {
"country": ["USA", "Canada", "Mexico"]
}
}
}range 查询
range 查询用于范围搜索,可以在指定字段上查找落在某个范围内的值。
GET /my_index/_search
{
"query": {
"range": {
"age": {
"gte": 30,
"lte": 40
}
}
}
}wildcard 查询
wildcard 查询用于进行通配符搜索,可以匹配字段值中的任意字符。
GET /my_index/_search
{
"query": {
"wildcard": {
"user": "ki*y"
}
}
}bool 查询
bool 查询用于组合多个查询条件,它包含四种子句:must、filter、should 和 must_not。
GET /my_index/_search
{
"query": {
"bool": {
"must": [{ "match": { "title": "ElasticSearch" } }],
"filter": [{ "term": { "status": "active" } }],
"should": [{ "range": { "age": { "gte": 30 } } }],
"must_not": [{ "term": { "status": "inactive" } }]
}
}
}must、filter、should 和 must_not 是 ElasticSearch 中 bool 查询的四个重要子句,它们允许你组合多种查询条件以实现更复杂的查询逻辑。下面我们详细介绍每一个子句的作用及其用法。
must 子句
- 作用:
must子句中的条件必须全部满足,相当于 AND 逻辑。 - 用法: 在查询中指定必须匹配的条件。
{
"query": {
"bool": {
"must": [
{ "match": { "title": "ElasticSearch" } },
{ "term": { "status": "published" } }
]
}
}
}在这个例子中,文档必须同时满足 title 包含 “ElasticSearch” 和 status 为 “published”。
filter 子句
- 作用:
filter子句中的条件也必须全部满足,但它们不会影响相关性得分(_score)。适用于结构化数据的过滤。 - 用法: 在查询中指定必须匹配的过滤条件,但不影响相关性得分。
{
"query": {
"bool": {
"filter": [
{ "term": { "status": "published" } },
{ "range": { "publish_date": { "gte": "2023-01-01" } } }
]
}
}
}在这个例子中,文档必须满足 status 为 “published” 并且 publish_date 在 2023 年 1 月 1 日之后。
should 子句
- 作用:
should子句中的条件如果满足,会增加文档的相关性得分。相当于 OR 逻辑,但如果至少有一个must子句存在时,它们不会强制要求所有should子句都满足。 - 用法: 在查询中指定一个或多个应当匹配的条件,这些条件会影响相关性得分。
{
"query": {
"bool": {
"should": [
{ "match": { "description": "fast" } },
{ "match": { "description": "reliable" } }
]
}
}
}在这个例子中,文档如果包含 “fast” 或 “reliable” 会有更高的相关性得分。
must_not 子句
- 作用:
must_not子句中的条件必须不满足,相当于 NOT 逻辑。 - 用法: 在查询中指定不应匹配的条件。
{
"query": {
"bool": {
"must_not": [{ "term": { "status": "draft" } }]
}
}
}在这个例子中,文档的 status 不能为 “draft”。
在 ElasticSearch 查询中,size 和 sort 是两个非常重要的参数,它们用于控制返回结果的数量和排序方式。下面我们详细介绍这两个参数的用法及其示例。
from 参数
from 参数用于指定查询结果的起始位置。通常用于分页(pagination),默认值为 0,即从第一条记录开始。
- 用法示例:
假设我们希望从第 10 条记录开始返回结果,可以这样设置:
GET /my_index/_search
{
"query": {
"match_all": {}
},
"from": 10
}size 参数
size 参数用于限制查询返回的结果数量。默认情况下,ElasticSearch 会返回前 10 条结果。如果你希望返回更多或更少的结果,可以通过设置 size 参数来实现。
GET /my_index/_search
{
"query": {
"match_all": {}
},
"size": 20
}在这个示例中,size: 20 告诉 ElasticSearch 只返回前 20 条结果。
sort 参数
sort 参数用于对查询结果进行排序。你可以根据一个或多个字段对结果进行排序,并指定升序(asc)或降序(desc)排列。
GET /my_index/_search
{
"query": {
"match_all": {}
},
"sort": [
{ "publish_date": "desc" }
]
}你也可以根据多个字段进行排序。例如,首先按 status 字段升序排序,然后按 publish_date 字段降序排序:
GET /my_index/_search
{
"query": {
"match_all": {}
},
"sort": [
{ "status": "asc" },
{ "publish_date": "desc" }
]
}嵌套查询
嵌套查询 (nested query) 用于在嵌套对象(nested object)内进行搜索。嵌套对象允许你在单个文档中存储复杂的对象数组,而每个对象都可以有独立的字段。嵌套查询使得我们可以在这些对象内进行复杂的查询。
嵌套对象是一个特殊的数据类型,用于在单个文档中存储多个对象。例如,你有一个包含订单的索引,每个订单包含多个产品,那么每个产品可以作为嵌套对象存储在一个订单文档中。
假设我们想找到那些包含某个特定产品的订单,并且该产品的价格在某个范围内。我们可以使用嵌套查询来实现这一点:
GET /orders/_search
{
"query": {
"nested": {
"path": "products",
"query": {
"bool": {
"must": [
{ "match": { "products.name": "Laptop" } },
{ "range": { "products.price": { "gte": 1000, "lte": 2000 } } }
]
}
}
}
}
}