- 添加索引库
- 创建文档
- 查询文档
- 查询所有文档
- 根据id查询文档
- 条件查询
- 模糊查询
- 修改文档
- 关于修改的特殊情况
- 删除文档
- 关于数据库的优化
添加索引库发送put请求. 在http://localhost:9200
后面加上你想要的索引库的名称即可 例如添加索引库名为tensquare_elasticsearch http://localhost:9200/tensquare_elasticsearch
响应数据如下
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "tensquare_elasticsearch"
}
创建文档
创建文档时,先需要有类型,只需在创建文档时,把路径后面加上文档名称就行. 发送post请求,请求体中用json数据,来传递文档的参数, 在索引库tensquare_elasticsearch中,创建article类型的文档 http://localhost:9200/tensquare_elasticsearch/article
文档的内容为
{
"title":"spring教程",
"content":"spring框架教程"
}
响应的数据如下
{
"_index": "tensquare_elasticsearch",
"_type": "article",
"_id": "AWhpMrWB71TIQ56XPqYw",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
created为true和"result": "created"代表是创建的操作. _id为自动生成的数据.
查询文档 查询所有文档get请求查询所有 http://localhost:9200/tensquare_elasticsearch/article/_search
返回如下的json数据
{
"took": 1685,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "tensquare_elasticsearch",
"_type": "article",
"_id": "AWhpMrWB71TIQ56XPqYw",
"_score": 1,
"_source": {
"title": "spring教程",
"content": "spring框架教程"
}
}
]
}
}
根据id查询文档
发送get请求,请求路径的最后一位为文档的id http://localhost:9200/tensquare_elasticsearch/article/1
响应的数据如下图
查询title为spring教程的文档. 发送如下的get请求 在_search?q后面进行条件参数的拼接 http://localhost:9200/tensquare_elasticsearch/article/_search?q=title:spring教程
响应的数据如下
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.7594807,
"hits": [
{
"_index": "tensquare_elasticsearch",
"_type": "article",
"_id": "AWhpMrWB71TIQ56XPqYw",
"_score": 0.7594807,
"_source": {
"title": "spring教程",
"content": "spring项目教程"
}
},
{
"_index": "tensquare_elasticsearch",
"_type": "article",
"_id": "1",
"_score": 0.7594807,
"_source": {
"title": "spring教程",
"content": "spring框架教程"
}
}
]
}
}
模糊查询
发送 如下的get请求, http://localhost:9200/tensquare_elasticsearch/article/_search?q=title:s
得到的响应如下
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
可以看到默认的是匹配查询, 匹配title中,有没有词条为s的文档. 而在英文中,是一个单词一个词条,没有字母为s的词条,因此就查询不到数据了. 在中文中,如果不用中文的分词器,默认的是一个汉字一个词条.
模糊查询,在搜索的关键词前后加上* ,即为模糊查询 发送如下的get请求 http://localhost:9200/tensquare_elasticsearch/article/_search?q=title:*s*
响应的数据如下. 模糊查询是可以匹配到数据的.
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "tensquare_elasticsearch",
"_type": "article",
"_id": "AWhpMrWB71TIQ56XPqYw",
"_score": 1,
"_source": {
"title": "spring教程",
"content": "spring项目教程"
}
},
{
"_index": "tensquare_elasticsearch",
"_type": "article",
"_id": "1",
"_score": 1,
"_source": {
"title": "spring教程",
"content": "spring框架教程"
}
}
]
}
}
修改文档
修改文档为put请求,请求路径上要拼接上要修改的文档的id 例如此处要修改的文档的id为AWhpMrWB71TIQ56XPqYw http://localhost:9200/tensquare_elasticsearch/article/AWhpMrWB71TIQ56XPqYw
在请求体中要带上修改的文档的json数据
{
"title":"spring教程",
"content":"spring项目教程"
}
响应的数据如下
{
"_index": "tensquare_elasticsearch",
"_type": "article",
"_id": "AWhpMrWB71TIQ56XPqYw",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false
}
“created”: false 代表不是新增的操作 “result”: “updated”, 代表是更新的操作 “_version”: 2, 代表为第二个版本
关于修改的特殊情况如果在修改的请求中,路径带上的id,在索引库中并不存在,那么会直接创建该请求路径上的id 例如发送put请求,路径带上1,此时索引库中是没有id为1的索引的 http://localhost:9200/tensquare_elasticsearch/article/1
响应的数据如下
{
"_index": "tensquare_elasticsearch",
"_type": "article",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
响应了created": true,代表是新增的操作, 并且id直接为1.
删除文档发送delete请求 http://localhost:9200/tensquare_elasticsearch/article/1
路径的最后面为删除文档的id 响应的数据如下
{
"found": true,
"_index": "tensquare_elasticsearch",
"_type": "article",
"_id": "1",
"_version": 3,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
关于数据库的优化
数据库的优化, 可以创建索引. 主键和唯一约束的列,会自动创建索引. 创建索引是双刃剑, 因为有索引,查询会快速,但是增删改会慢. 因为索引的底层为二叉树, 每次进行增删改,都会对树的结构进行调整, 非常的影响性能. 但是对查询来说,建立索引,会提供查询的速度.