Dashboards 查询语言 (DQL)
Dashboards 查询语言 (DQL) 是一种简单的基于文本的查询语言,用于在 OpenSearch Dashboards 中过滤数据。
DQL 和 查询字符串查询 (Lucene) 语言是 Discover 和 Dashboards 中搜索栏的两个语言选项。本页面提供 DQL 语法的参考。有关 Lucene 语法,请参阅查询字符串查询。有关语法比较,请参阅命令快速参考。
默认情况下,OpenSearch Dashboards 使用 DQL 语法。要切换到查询字符串查询 (Lucene),请选择搜索框旁边的 DQL 按钮,然后切换 开 关,如下图所示。
语法变为 Lucene。要切换回 DQL,请选择 Lucene 按钮并切换 关 关。
分析文本上的查询
运行查询时,了解您的字段是已分析(text
类型)还是未分析(keyword
类型)至关重要,因为它会显著影响搜索行为。在已分析字段中,文本会经过分词和过滤,而未分析字段则存储精确值。对于像 wind
这样的简单字段查询,针对已分析字段的搜索会匹配包含 wind
的文档,不区分大小写,而对关键字字段的相同查询则需要精确匹配整个字符串。有关已分析字段的更多信息,请参阅文本分析。
设置
要在 OpenSearch Dashboards 中学习本教程,请展开以下设置步骤。
设置
使用以下步骤准备示例数据以进行查询。
步骤 1:设置索引的映射
在主菜单上,选择 Management > Dev Tools 以打开开发工具。发送以下请求以创建索引映射
PUT testindex
{
"mappings" : {
"properties" : {
"date" : {
"type" : "date",
"format" : "yyyy-MM-dd"
}
}
}
}
步骤 2:将文档摄取到索引中
在 Dev Tools 中,将以下文档摄取到索引中
PUT /testindex/_doc/1
{
"title": "The wind rises",
"description": "A biographical film",
"media_type": "film",
"date": "2013-07-20",
"page_views": 100
}
PUT /testindex/_doc/2
{
"title": "Gone with the wind",
"description": "A well-known 1939 American epic historical film",
"media_type": "film",
"date": "1939-09-09",
"page_views": 200
}
PUT /testindex/_doc/3
{
"title": "Chicago: the historical windy city",
"media_type": "article",
"date": "2023-07-29",
"page_views": 300
}
PUT /testindex/_doc/4
{
"article title": "Wind turbines",
"media_type": "article",
"format": "2*3"
}
步骤 3:创建索引模式
按照以下步骤为您的索引创建索引模式
- 在主菜单上,选择 Management > Dashboards Management。
- 选择 Index patterns,然后选择 Create index pattern。
- 在 Index pattern name 中,输入
testindex*
。选择 Next step。 - 在 Time field 中,选择
I don't want to use the time filter
。 - 选择 Create index pattern。
有关索引模式的更多信息,请参阅索引模式。
步骤 4:导航到 Discover 并选择索引模式
在主菜单上,选择 Discover。在左上角,从 Index patterns 下拉列表中选择 testindex*
。主面板显示索引中的文档,您现在可以尝试本页描述的 DQL 查询。
DQL 和查询字符串查询快速参考
下表提供了两种查询语言命令的快速参考。
功能 | DQL | 查询字符串查询 (Lucene) |
---|---|---|
基本术语搜索 | wind | wind |
多个术语 | wind gone (查找包含 wind 或 gone 的文档) | wind gone (查找包含 wind 或 gone 的文档) |
精确短语搜索 | "wind rises" | "wind rises" |
特定字段搜索 | title: wind | title:wind |
字段是否存在 | description:* | _exists_:description |
字段中的多个术语 | title: (wind OR rises) | title:(wind OR rises) |
包含空格的字段 | article*title: wind | article\ title:wind |
转义特殊字符 | format: 2\*3 | format:2\*3 |
多字段搜索 | title: wind OR description: film | title:wind OR description:film |
嵌套字段搜索 | 请参阅嵌套字段 | 不支持 |
数值范围 | page_views >= 100 and page_views <= 300 not page_views: 100 (结果包含不包含 page_views 字段的文档)请参阅范围 | page_views:[100 TO 300] page_views:(>=100 AND <=300) page_views:(+>=100 +<=300) page_views:[100 TO *] page_views:>=100 NOT page_views:100 (结果包含不包含 page_views 字段的文档)请参阅范围 |
日期范围 | date >= "1939-01-01" and date <= "2013-12-31" not date: "1939-09-08" | date:[1939-01-01 TO 2013-12-31] NOT date:1939-09-08 支持所有数值范围语法结构 |
独占范围 | 不支持 | page_views: {100 TO 300} (返回 page_views 在 100 和 300 之间(不包括 100 和 300 )的文档) |
布尔 AND | media_type:film AND page_views:100 media_type:film and page_views:100 | media_type:film AND page_views:100 +media_type:film +page_views:100 |
布尔 NOT | NOT media_type: article not media_type: article | NOT media_type:article -media_type:article |
布尔 OR | title: wind OR description: film title: wind or description: film | title: wind OR description: film |
必需/禁止运算符 | 不支持 | 支持 + (必需运算符)和 - (禁止运算符)+title:wind -media_type:article (返回 title 包含 wind 但 media_type 不包含 article 的文档) |
通配符 | title: wind* titl*: wind 不支持短语搜索中的通配符(在引号内) 仅支持 * (多个字符) | title:wind* 或 title:w?nd 不支持字段名中的通配符 不支持短语搜索中的通配符(在引号内) 支持 * (多个字符)和 ? (单个字符) |
正则表达式 | 不支持 | title:/w[a-z]nd/ |
模糊搜索 | 不支持 | title:wind~2 |
近邻搜索 | 不支持 | "wind rises"~2 |
提升术语 | 不支持 | title:wind^2 |
保留字符 | \ ( ) : < > " * | + - = && \|\| > < ! ( ) { } [ ] ^ " ~ * ? : \ / |
搜索术语
默认情况下,DQL 在索引上设置为默认字段的字段中进行搜索。如果未设置默认字段,DQL 会搜索所有字段。例如,以下查询搜索在其任何字段中包含单词 rises
或 wind
的文档
rises wind
上述查询匹配任何搜索词出现(无论顺序如何)的文档。默认情况下,DQL 使用 or
组合搜索词。要了解如何创建包含搜索词的布尔表达式,请参阅布尔运算符。
要搜索短语(按顺序排列的单词序列),请用引号将文本括起来。例如,以下查询搜索精确文本“wind rises”
"wind rises"
连字符是 Lucene 中的保留字符,因此如果您的搜索词包含连字符,DQL 可能会提示您切换到 Lucene 语法。为避免这种情况,请在短语搜索中用引号将搜索词括起来,或在常规搜索中省略连字符。
保留字符
以下是 DQL 中的保留字符列表
\
, (
, )
, :
, <
, >
, "
, *
使用反斜杠(\
)转义保留字符。例如,要搜索表达式 2*3
,请将查询指定为 2\*3
2\*3
在字段中搜索
要在特定字段中搜索文本,请在冒号前指定字段名
title: rises wind
您正在搜索的字段的分析器将查询文本解析为令牌,并匹配出现任何令牌的文档。
DQL 忽略空格字符,因此 title:rises wind
和 title: rises wind
是相同的。
使用通配符引用包含空格的字段名。例如,article*title
匹配 article title
字段。
字段名
在冒号前指定字段名。下表包含带有字段名的示例查询。
查询 | 文档匹配条件 | 来自 testindex 索引的匹配文档 |
---|---|---|
title: wind | title 字段包含单词 wind 。 | 1, 2 |
title: (wind OR windy) | title 字段包含单词 wind 或单词 windy 。 | 1, 2, 3 |
title: "wind rises" | title 字段包含短语 wind rises 。 | 1 |
title.keyword: The wind rises | title.keyword 字段精确匹配 The wind rises 。 | 1 |
title*: wind | 任何以 title 开头(例如 title 和 title.keyword )的字段都包含单词 wind | 1, 2 |
article*title: wind | 以 article 开头并以 title 结尾的字段包含单词 wind 。匹配字段 article title 。 | 4 |
description:* | 字段 description 存在的文档。 | 1, 2 |
通配符
DQL 支持搜索词和字段名中的通配符(仅限 *
),例如
t*le: *wind and rise*
范围
DQL 支持使用 >
、<
、>=
和 <=
运算符的数值不等式,例如
page_views > 100 and page_views <= 300
您可以在日期上使用范围运算符。例如,以下查询搜索包含 2013-2023 范围内的日期(包括首尾)的文档
date >= "2013-01-01" and date < "2024-01-01"
您可以使用 not
和字段名查询“不等于”,例如
not page_views: 100
请注意,上述查询返回的文档中,page_views
字段不包含 100
或该字段不存在。要根据包含 page_views
字段的文档进行过滤,请使用以下查询
page_views:* and not page_views: 100
布尔运算符
DQL 支持 and
、or
和 not
布尔运算符。DQL 不区分大小写,因此 AND
和 and
是相同的。例如,以下查询是两个布尔子句的合取
title: wind and description: epic
布尔运算符遵循 not
、and
和 or
的逻辑优先级顺序,因此在以下示例中,title: wind and description: epic
首先被评估
media_type: article or title: wind and description: epic
要指定评估顺序,请将布尔子句分组在括号中。例如,在以下查询中,带括号的表达式首先被评估
(media_type: article or title: wind) and description: epic
字段前缀指的是紧跟在冒号后面的令牌。例如,以下查询搜索 title
字段中包含 windy
的文档,或在任何字段中包含单词 historical
的文档
title: windy or historical
要搜索 title
字段中包含 windy
或 historical
的文档,请将术语分组在括号中
title: (windy or historical)
上述查询等同于 title: windy or title: historical
。
要否定查询,请使用 not
运算符。例如,以下查询搜索 title
字段中包含单词 wind
,不属于 media_type
article
类型,并且 description
字段中不包含 epic
的文档
title: wind and not (media_type: article or description: epic)
查询可以包含多个分组级别,例如
title: ((wind or windy) and not rises)
对象字段
要引用对象的内部字段,请列出字段的点路径。
要索引包含对象的文档,请遵循对象字段类型示例中的步骤。要搜索 patient
对象的 name
字段,请使用以下语法
patient.name: john
嵌套字段
要引用嵌套对象,请列出字段的 JSON 路径。
要索引包含对象的文档,请遵循嵌套字段类型示例中的步骤。
要搜索 patients
对象的 name
字段,请使用以下语法
patients: {name: john}
要检索匹配多个字段的文档,请指定所有字段。例如,考虑以下文档中的附加 status
字段
{
"status": "Discharged",
"patients": [
{"name" : "John Doe", "age" : 56, "smoker" : true},
{"name" : "Mary Major", "age" : 85, "smoker" : false}
]
}
要搜索名为 John 的已出院患者,请在查询中指定 name
和 status
patients: {name: john} and status: discharged
您可以组合多个布尔和范围查询以创建更精确的查询,例如
patients: {name: john and smoker: true and age < 57}
双重嵌套字段
考虑一个具有双重嵌套字段的文档。在此文档中,patients
和 names
字段的类型均为 nested
{
"patients": [
{
"names": [
{ "name": "John Doe", "age": 56, "smoker": true },
{ "name": "Mary Major", "age": 85, "smoker": false}
]
}
]
}
要搜索 patients
对象的 name
字段,请使用以下语法
patients: {names: {name: john}}
相反,考虑一个文档,其中 patients
字段的类型为 object
,但 names
字段的类型为 nested
{
"patients":
{
"names": [
{ "name": "John Doe", "age": 56, "smoker": true },
{ "name": "Mary Major", "age": 85, "smoker": false}
]
}
}
要搜索 patients
对象的 name
字段,请使用以下语法
patients.names: {name: john}