ES 搜索API
Elasticsearch Search APIs
Elasticsearch 提供了搜索 API,用于跨索引和所有类型搜索数据。它有助于通过执行搜索查询来搜索 Elasticsearch 中的数据,并返回与查询匹配的搜索结果。这个 API 使您能够在 Elasticsearch。您可以通过两种方式搜索数据-
通过发送带有字符串参数的查询的 GET 请求或
使用在消息正文中包含查询的 POST 请求。
查看这两种类型的每个示例-
使用 GET 请求
执行以下查询。它将显示 new_student 索引中存在的所有记录。
复制代码
GET /new_student/
_search
{
"match_all": { }
}
响应
在下面的响应中,您可以看到执行上述请求后找到了两条记录。
{
"took": 93,
"timed_out": false,
"_shards": {
"total":2,
"successful":1,
"skipped":0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"index": "new_student",
"type": "_doc",
"id": "01",
"score": 1,
"_seq_no": 0,
"_primary_term": 1,
"found": true,
"_source": {
"name ": "Alen Paul",
"gender": "Male",
"phone": "9876543210",
"street": "Malen Park",
"city": "California",
"country": "United States",
"zip": "94025"
}
}
{
"index": "new_student",
"type": "_doc",
"id": "01",
"score": 1,
"_seq_no": 0,
"_primary_term": 1,
"found": true,
"_source": {
"name ": "Alen Paul",
"gender": "Male",
"phone": "9876543210",
"street": "Malen Park",
"city": "California",
"country": "United States",
"zip": "94025"
}
}
]
}
}
使用POST请求
复制代码
POST
_search
{
"query ": {
"match_all": { }
}
}
响应
响应将类似于 GET 方法请求。查看下面的屏幕截图以查看 elasticsearch 插件中的输出-
在上面的截图,你可以看到有两条记录返回与GET请求输出相同。
现在,让我们转向搜索API的类型,它分为三种类型:
多索引
多类型
URI 搜索
多索引是搜索最多的API。但是,我们将详细讨论每个搜索 API 以及示例。在此之前,下面是一些可以在搜索操作中传递的请求参数-
参数 |
说明 |
问 |
用于指定查询字符串。 |
排序 |
此参数用于对响应进行排序。 |
字段 |
它从选择性字段中获取结果。 |
宽容 |
该参数用于通过将值设置为true来忽略基于格式的错误。 |
来自 |
此参数有助于指定索引的起点。默认值为 0。 |
尺寸 |
指定要返回的点击次数。默认情况下,大小为 10、 |
超时 |
它限制了搜索时间。 |
terminate_after |
负责将响应限制为每个分片中特定数量的文档。 |
现在,让我们详细讨论每个 API-
1.多索引
多索引API是一种搜索API,用于在索引中搜索文档。它搜索某些特定索引或所有索引中的所有文档。对于某些特定的索引搜索,请指定要对其执行搜索操作的以逗号(,) 分隔的索引名称。如果要搜索所有索引,请指定_all 关键字而不是索引名称。
<块引用>
多索引 API 允许用户一次搜索两个或多个索引。
例如,我们将使用一个查询在两个不同的索引(student 和 new_student)中搜索所有选修大众传播课程的学生。
方法: GET
API: _search
索引: student, new_student
参数: q
执行以下查询并搜索文档。
复制代码
GET /student, new_student/
_search?q=course:Mass Communication
{
}
响应
在下面的响应中,您可以看到执行上述查询后找到了两条记录。
{
"took": 102,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.9333868,
"hits": [
{
"index": "student",
"type": "_doc",
"id": "01",
"score": 1.9333868,
"_source": {
"name ": "Denial Parygen",
"dob": "07/Aug/1998",
"course": "Mass Communication",
"Addmission year": "2018",
"email": "denial@gmail.com",
"street": "3511 Rodney Street",
"state": "Missouri",
"country": "United States",
"zip": "62208"
}
},
{
"index": "new_student",
"type": "_doc",
"id": "03",
"score": 1.8132977,
"_source": {
"name ": "Lerry Page",
"dob": "07/Aug/1996",
"course": "Mass Communication",
"Addmission year": "2017",
"email": "Lerypg@gmail.com",
"street": "3511 Rodney Street",
"state": "Missouri",
"country": "United States",
"zip": "62208"
}
}
]
}
}
查看下面的截图如何在elasticsearch插件中执行查询-
2、多类型
多类型搜索 API 提供了在所有类型或某些特定类型中搜索特定索引中的所有文档的工具。在下面的示例中,我们将执行以下查询以在所有类型下从"student"索引中搜索 JSON 文档,其中邮政编码为 30501、
方法: GET
API: _search
索引: student
参数: q
执行以下查询并搜索文档。
复制代码
GET /student, new_student/
_search?q=zip:30501
{ }
Response
在下面的响应中,您可以看到学生索引中的一个 JSON 文档,其 zip 为 30501、
{
"took": 511,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.2039728,
"hits": [
{
"index": "student",
"type": "_doc",
"id": "02",
"score": 1.2039728,
"_source": {
"name ": "Alex Paul",
"dob": "24/May/1994",
"course": "MSW",
"Addmission year": "2016",
"email": "alexl@gmail.com",
"street": "2253 Hillview Drive",
"state": "Georgia",
"country": "United States",
"zip": "30501"
}
}
]
}
}
查看下面的截图如何在elasticsearch插件中执行查询-

<块引用>
记住-在 elasticsearch 数据库中,查询是区分大小写的,例如,_search 和 _Search 是不同的。同样,zip 和 Zip 在 elasticsearch 中的处理方式不同。因此,在创建查询时要小心。
3. URI 搜索
URI 搜索是一种统一资源标识符搜索。它允许我们在使用 URI 的搜索操作中传递多个请求参数,以便可以纯粹地执行请求。每当您使用此模式执行搜索查询时,都不会公开所有搜索选项。但是,它对于 curl 测试很方便。
执行以下查询并搜索学生索引中的文档。
复制代码
GET /student/
_search
{
"query": {
"query_string": {
"query": "up"
}
}
}
Response
上述查询将返回new_student索引中存在的所有文档。
{
"took": 244,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"index": "new_student",
"type": "_doc",
"id": "03",
"score": 1,
"_source": {
"name ": "Lerry Page",
"dob": "07/Aug/1996",
"course": "Mass Communication",
"Addmission year": "2017",
"email": "Lerrypg@gmail.com",
"street": "3511 Rodney Street",
"state": "Missouri",
"country": "United States",
"zip": "62208"
}
},
{
"index": "new_student",
"type": "_doc",
"id": "02",
"score": 1,
"_source": {
"name ": "Jass Fernandiz",
"dob": "07/Aug/1996",
"course": "Bcom (H)",
"Addmission year": "2019",
"email": "jassf@gmail.com",
"street": "4225 Ersel Street",
"state": "Texas",
"country": "United States",
"zip": "77063"
}
},
{
"index": "new_student",
"type": "_doc",
"id": "01",
"score": 1,
"_source": {
"name ": "Camila Cabello",
"dob": "09/Feb/1999",
"course": "MSW",
"Addmission year": "2018",
"email": "camilacab@gmail.com",
"street": "729 Monroe Street",
"state": "Hauston",
"country": "United States",
"zip": "77063"
}
},
]
}
}
查看下面的截图,在elasticsearch插件中URI搜索操作是如何执行的-
通过 id 获取多条记录
我们甚至可以在单个查询请求中通过 id 搜索多个文档,以及所有这些搜索操作。为此,我们需要指定我们要查找的每个文档的 id。
例如
我们将使用 POST 用于从索引中检索多个文档的 API。那还不够;我们还需要在请求字符串中附加 _mget 并定义文档 ID 查询空间以获取多条记录。
复制代码
POST student/ _mget/
{
"docs": [
{
"_index": "new_student", "_type": "_doc", "_id": "02"
},
{
"_index": "new_student", "_type": "_doc", "_id": "04"
}
]
}
在这里,我们正在获取 ID 为 02 和 04 的文档。
响应
您将获得类似于以下响应的输出。
"docs": [
{
"index": "student",
"type": "_doc",
"id": "02",
"version": 1,
"_seq_no": 0,
"_primary_term": 1,
"found": true,
"source": {
"name": "Alen Paul",
"gender": "Male",
"phone": "9876543210",
"street": "Malen Park",
"city": "California",
"country": "United State",
"zip": "94025",
}
}
"index": "student",
"type": "_doc",
"id": "04",
"version": 1,
"_seq_no": 3,
"_primary_term": 1,
"found": true,
"source": {
"name": "Stan Lee",
"gender": "Male",
"phone": "9876543211",
"street": "New Rochelle",
"city": "New York",
"country": "United State",
"zip": "10029",
}
}
]
}
块引用>
块引用>