原文链接:https://xiets.blog.csdn.net/article/details/132348478
版权声明:原创文章禁止转载
通过简单的 创建索引、写入文档、搜索文档 介绍 ES 搜索引擎的简单入门。
1. 创建索引
创建索引官网文档:Create index API
要存储数据到 ES 搜索引擎,首先需要创建索引。以网上书店的书籍搜索为例,创建一个用于存储书籍信息的索引,简单包含书名、作者、价格三个字段。书名使用文本(text)类型,作者使用关键字(keyword)类型,价格使用浮点数(float)类型。
创建索引 的请求:PUT /<index>
创建一个名词为book
的索引,映射中有 3 个不同数据类型的字段,分别为 name
、author
和 price
,使用 CURL 命令完整索引创建语句如下:
curl -H "Content-Type: application/json" -X PUT "http://127.0.0.1:9200/book" -d '
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"author": {
"type": "keyword"
},
"price": {
"type": "float"
}
}
}
}
'
与 ES 搜索引擎交互,一般以 JSON 的格式进行交互,创建索引使用 PUT 请求方式,为了简化语句表达,可以省略的部分(执行时必须是合法的 cURL Shell 命令,不能省略),并且可以加上注释,上面语句省略后如下:
PUT /book // book 是索引名称
{
"mappings": {
// 索引的映射结构
"properties": {
// 映射包含的字段结构
"name": {
// 字段名称
"type": "text" // 字段的数据类型
},
"author": {
"type": "keyword"
},
"price": {
"type": "float"
}
}
}
}
运行 CURL 命令,创建成功将返回:
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "book"
}
查看索引映射 的请求:GET /<index>/_mappings
,查看 book 索引的映射:
GET /book/_mappings
// 返回
{
"book":{
"mappings":{
"properties":{
"author":{
"type":"keyword"},"name":{
"type":"text"},"price":{
"type":"float"}}}}}
ES 返回的 JSON 默认没有格式化,如果要返回格式化 JSON,可以在请求 URI 后面加上 pretty
参数:
GET /book/_mappings?pretty
// 返回
{
"book" : {
"mappings" : {
"properties" : {
"author" : {
"type" : "keyword"
},
"name" : {
"type" : "text"
},
"price" : {
"type" : "float"
}
}
}
}
}
查看索引 请求:GET /<index>
,包括索引的别名、映射、设置 等:
GET /book
// 返回
{
"book": {
"aliases": {
},
"mappings": {
"properties": {
"author": {
"type": "keyword"
},
"name": {
"type": "text"
},
"price": {
"type": "float"
}
}
},
"settings": {
"index": {
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_content"
}
}
},
"number_of_shards": "1",
"provided_name": "book",
"creation_date": "1689939514483",
"number_of_replicas": "1",
"uuid": "cCAjKMNGTlagzm2VVaXxtg",
"version": {
"created": "8080299"
}
}
}
}
}
2. 写入文档
读写文档官网链接:Index API
索引创建好后,相当于关系型数据库创建好了表,然后就是写入数据。ES中的数据称为文档,一个文档就相当于关系型数据库表中的一行数据。
写入文档 的请求:
PUT /<index>/_doc/<_id>
POST /<index>/_doc/
PUT /<index>/_create/<_id>
POST /<index>/_create/<_id>
写入文档使用 PUT 或 POST 请求方法,可以直接指定文档ID(如果ID已存在,则会直接覆盖现有文档),如果没有指定文档ID,则必须使用 POST 请求。在上面创建的 book 索引中写入一些文档。
指定文档ID,PUT 写入文档,请求数据是由文档各映射字段的值组成的JSON:
PUT /book/_doc/001
{
"name": "Python基础教程",
"author": "张三",
"price": 30.00
}
// 写入成功, 返回
{
"_index":"book","_id":"001","_version":1,"result":"created","_shards":{
"total":2,"successful":1,"failed":0},"_seq_no":14,"_primary_term":1}
不指定ID,POST 写入文档:
POST /book/_doc/
{
"name": "Java基础教程",
"author": "张三",
"price": 40.00
}
// 写入成功, 返回
{
"_index":"book","_id":"vG6feIkBBJJU3DRUtVVz","_version":1,"result":"created","_shards":{
"total":2,"successful":1,"failed":0},"_seq_no":16,"_primary_term":1}
写入文档时,如果没有指定文档ID,则自动随机生成文档ID,_version
表示文档的版本,每一次对文档的修改,版本就会递增。
3. 获取文档
获取文档官网链接:Get API
根据文档 ID 获取文档,请求格式:
GET <index>/_doc/<_id>
HEAD <index>/_doc/<_id>
GET <index>/_source/<_id>
HEAD <index>/_source/<_id>
获取 ID 为 “001” 的文档:
GET /book/_doc/001
// 返回
{
"_index": "book",
"_id": "001",
"_version": 1,
"_seq_no": 14,
"_primary_term": 1,
"found": true,
"_source": {
"name": "Python基础教程",
"author": "张三",
"price": 30.0
}
}
其中 _source
节点展示的就是文档的原始数据,也可以值获取该部分的数据:
GET /book/_source/001
// 返回
{
"name": "Python基础教程",
"author": "张三",
"price": 30.00
}
4. 搜索文档
搜索文档 请求格式:POST /<index>/_search
也可以使用 GET 请求搜索文档,不提供请求数据,将返回部分文档:GET /<index>/_search
4.1 term 搜索
根据一般字段搜索文档:
POST /book/_search
{
"query": {
// 查询内容
"term": {
// term搜索 (不支持模糊查找)
"price": 30.00 // 搜索的字段和匹配的值
}
}
}
// 返回
{
"took": 1,
"timed_out": false,
"_shards": {
// 命中的分片信息
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
// 命中的文档总数
"value": 1,
"relation": "eq"
},
"max_score": 1.0, // 命中文档集合中最高分
"hits": [ // 命中的所有文档
{
"_index": "book", // 文档所在索引
"_id": "001", // 文档ID
"_score": 1.0, // 命中的分数
"_source": {
// 文档原始内容
"name": "Python基础教程",
"author": "张三",
"price": 30.0
}
}
]
}
}
4.2 match 搜索
根据文本(text)字段搜索文档:
POST /book/_search
{
"query": {
// 查询内容
"match": {
// match搜索 (支持模糊查找)
"name": "Python" // 搜索的字段和匹配的值
}
}
}
// 返回
{
"took": 22,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.6931471,
"hits": [
{
"_index": "book",
"_id": "001",
"_score": 0.6931471,
"_source": {
"name": "Python基础教程",
"author": "张三",
"price": 30.0
}
}
]
}
}
使用 GET 请求搜索文档,不提供请求数据,将返回部分文档:
GET book/_search
// 返回
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "book",
"_id": "vG6feIkBBJJU3DRUtVVz",
"_score": 1.0,
"_source": {
"name": "Java基础教程",
"author": "张三",
"price": 40.0
}
},
{
"_index": "book",
"_id": "001",
"_score": 1.0,
"_source": {
"name": "Python基础教程",
"author": "张三",
"price": 30.0
}
}
]
}
}
5. 更新文档
更新文档官网链接:Update API
更新文档 的请求格式:POST /<index>/_update/<_id>
更新文档 ID 为 “001” 的文档:
POST /book/_update/001
{
"doc": {
// 更新后的数据 (可以同时更新多个字段, 没提供的保持不变)
"name": "Python教程(第2版)",
"price": 50.00
}
}
// 返回
{
"_index":"book","_id":"001","_version":2,"result":"updated","_shards":{
"total":2,"successful":1,"failed":0},"_seq_no":18,"_primary_term":1}
更新后,文档版本(_version
)变为了2,获取更新后的文档:
GET /book/_doc/001
{
"_index": "book",
"_id": "001",
"_version": 2,
"_seq_no": 18,
"_primary_term": 1,
"found": true,
"_source": {
"name": "Python教程(第2版)",
"author": "张三",
"price": 50.0
}
}
6. 删除文档
删除文档官网链接:Delete API
删除文档 的请求格式:DELETE /<index>/_doc/<_id>
删除文档 ID 为 “001” 的文档:
DELETE /book/_doc/001
// 返回
{
"_index":"book","_id":"001","_version":3,"result":"deleted","_shards":{
"total":2,"successful":1,"failed":0},"_seq_no":19,"_primary_term":1}
删除后再获取文档,将返回找不到:
GET /book/_doc/001
// 返回
{
"_index":"book","_id":"001","found":false}