- Elasticsearch 教程
- Elasticsearch - 主页
- Elasticsearch - 基本概念
- Elasticsearch - 安装
- Elasticsearch - 填充
- 版本之间的迁移
- Elasticsearch - API 约定
- Elasticsearch - 文档 API
- Elasticsearch - 搜索 API
- Elasticsearch - 聚合
- Elasticsearch - 索引 API
- Elasticsearch - CAT API
- Elasticsearch - 集群 API
- Elasticsearch - 查询 DSL
- Elasticsearch - 映射
- Elasticsearch - 分析
- Elasticsearch - 模块
- Elasticsearch - 索引模块
- Elasticsearch - 摄取节点
- Elasticsearch - 管理索引生命周期
- Elasticsearch - SQL 访问
- Elasticsearch - 监控
- Elasticsearch - 汇总数据
- Elasticsearch - 冻结索引
- Elasticsearch - 测试
- Elasticsearch - Kibana 仪表板
- Elasticsearch - 按字段过滤
- Elasticsearch - 数据表
- Elasticsearch - 区域地图
- Elasticsearch - 饼图
- Elasticsearch - 面积图和条形图
- Elasticsearch - 时间序列
- Elasticsearch - 标签云
- Elasticsearch - 热图
- Elasticsearch - 画布
- Elasticsearch - 日志 UI
- Elasticsearch 有用资源
- Elasticsearch - 快速指南
- Elasticsearch - 有用的资源
- Elasticsearch - 讨论
Elasticsearch - 搜索 API
该API用于在Elasticsearch中搜索内容。用户可以通过发送带有查询字符串作为参数的 get 请求来进行搜索,也可以在 post 请求的消息正文中发布查询。主要是所有的搜索API都是多索引、多类型的。
多索引
Elasticsearch 允许我们搜索所有索引或某些特定索引中存在的文档。例如,如果我们需要搜索名称包含central的所有文档,我们可以这样做 -
GET /_all/_search?q=city:paprola
运行上面的代码,我们得到以下响应 -
{
"took" : 33,
"timed_out" : false,
"_shards" : {
"total" : 7,
"successful" : 7,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.9808292,
"hits" : [
{
"_index" : "schools",
"_type" : "school",
"_id" : "5",
"_score" : 0.9808292,
"_source" : {
"name" : "Central School",
"description" : "CBSE Affiliation",
"street" : "Nagan",
"city" : "paprola",
"state" : "HP",
"zip" : "176115",
"location" : [
31.8955385,
76.8380405
],
"fees" : 2200,
"tags" : [
"Senior Secondary",
"beautiful campus"
],
"rating" : "3.3"
}
}
]
}
}
统一资源定位符搜索
可以使用统一资源标识符在搜索操作中传递许多参数 -
| 序列号 | 参数及说明 |
|---|---|
| 1 | 问 该参数用于指定查询字符串。 |
| 2 | 宽容 该参数用于指定查询字符串。只需将此参数设置为 true 即可忽略基于格式的错误。默认情况下为 false。 |
| 3 | 领域 该参数用于指定查询字符串。 |
| 4 | 种类 我们可以通过该参数得到排序后的结果,该参数的可能值为fieldName,fieldName:asc/fieldname:desc |
| 5 | 暂停 我们可以使用此参数限制搜索时间,并且响应仅包含指定时间内的命中。默认情况下,没有超时。 |
| 6 | 终止后 我们可以将每个分片的响应限制为指定数量的文档,达到该数量后查询将提前终止。默认情况下,没有terminate_after。 |
| 7 | 从 要返回的命中索引的起始位置。默认为 0。 |
| 8 | 尺寸 它表示返回的命中数。默认为 10。 |
请求正文搜索
我们还可以在请求正文中使用查询 DSL 来指定查询,前面的章节已经给出了很多示例。这里给出了一个这样的例子 -
POST /schools/_search
{
"query":{
"query_string":{
"query":"up"
}
}
}
运行上面的代码,我们得到以下响应 -
{
"took" : 11,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.47000363,
"hits" : [
{
"_index" : "schools",
"_type" : "school",
"_id" : "4",
"_score" : 0.47000363,
"_source" : {
"name" : "City Best School",
"description" : "ICSE",
"street" : "West End",
"city" : "Meerut",
"state" : "UP",
"zip" : "250002",
"location" : [
28.9926174,
77.692485
],
"fees" : 3500,
"tags" : [
"fully computerized"
],
"rating" : "4.5"
}
}
]
}
}