文章目录 什么是Elasticsearch全文搜索引擎 docker安装Elasticsearch 安装es Elasticsearch的基本查询 索引创建,查询,删除 创建文档,修改文档,删除文档, 创建文……
文
章
目
录
- 什么是Elasticsearch全文搜索引擎
- docker安装Elasticsearch
- 安装es
- Elasticsearch的基本查询
- 索引创建,查询,删除
- 创建文档,修改文档,删除文档,
- 创建文档
- 查看文档
- 修改文档
- 删除文档
- 条件删除文档
- 总结
什么是Elasticsearch全文搜索引擎
Elastic Stack是一个包括Elasticsearch、Kibana、Beats和Logstash(也称为ELK Stack)的技术栈。它能够安全可靠地获取各种来源和格式的数据,并实时进行搜索、分析和可视化。
其中,Elasticsearch(简称为ES)是整个Elastic Stack技术栈的核心。它是一个开源的高扩展性的分布式全文搜索引擎。ES可以近乎实时地存储和检索数据,具有良好的扩展性,可以扩展到上百台服务器,处理PB级别的数据。
传统的关系型数据库在全文检索方面的支持相对有限。对于非结构化的文本数据,如网页内容、应用日志等,传统数据库的全文检索功能性能较差。全文检索需要扫描整个表,即使对SQL语法进行优化,效果也有限。而且建立和维护索引也比较麻烦,对于插入和更新操作,需要重新构建索引。
因此,在一些生产环境中,使用传统的搜索方式性能不佳的情况包括:
- 需要搜索大量非结构化文本数据的对象。
- 文件记录数量达到数十万甚至数百万个甚至更多。
- 需要支持大量基于交互式文本的查询。
- 对全文搜索查询的需求非常灵活。
- 对高度相关的搜索结果有特殊需求,但没有可用的关系数据库满足。
- 需要处理不同记录类型、非文本数据操作或安全事务处理的需求较少。
为了解决结构化数据和非结构化数据的搜索性能问题,我们需要专业、健壮和强大的全文搜索引擎。全文搜索引擎通过扫描文章中的每个词,建立索引来进行搜索。当用户查询时,检索程序根据预先建立的索引进行查找,并将结果返回给用户。这类似于使用字典的检索字表查字的过程。
因此,Elastic Stack提供了强大的全文搜索引擎Elasticsearch,以满足对于大规模非结构化文本数据的高性能搜索和分析需求。
docker安装Elasticsearch
Spring Boot的版本号与Spring Data Elasticsearch的版本号是有关联的。您需要确保使用的Spring Data Elasticsearch版本与您的Spring Boot版本兼容。
这里我使用的是spring boot2.7.12,elasticsearch则使用7.17.3
安装es
docker run --name es7
-p 9200:9200 -p 9300:9300
-e \"discovery.type=single-node\"
-e ES_JAVA_OPTS=\"-Xms256m -Xmx256m\"
-d elasticsearch:7.17.3
- -d:后台启动
- –name:容器名称
- -p:端口映射
- -e:设置环境变量
discovery.type=single-node:单机运行
如果启动不了,可以加大内存设置:-e ES_JAVA_OPTS=”-Xms512m -Xmx512m”
注意: es默认内存占用是1G以上,但是在平常测试使用中不会用这么大的,我们修改为256m者512m
进入容器修改config下的elasticsearch.yml配置,修改
cluster.name: \"docker-cluster\"
network.host: 0.0.0.0
http.cors.enabled: true
xpack.security.enabled: false
http.cors.allow-origin: \"*\"
进入容器安装ik分词器。默认分词不好用,在plugins下去下载ik分词器
elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.17.3/elasticsearch-analysis-ik-7.17.3.zip
上面步骤完成后重启 容器 docker restart 容器id
4.安装kibana界面,或者使用api工具软件等
docker run --name kibana
-e ELASTICSEARCH_HOSTS=http://esIP地址:9200
-p 5601:5601 -d kibana:7.17.3
进入容器修改config下的kibana.yml配置,重启容器
server.name: kibana
server.host: \"0\"
elasticsearch.hosts: [ \"http://esIP地址:9200\" ]
xpack.monitoring.ui.container.elasticsearch.enabled: true
i18n.locale: zh-CN
Elasticsearch的基本查询
索引创建,查询,删除
对比关系型数据库,创建索引就等同于创建数据库,索引(index)就是文档数据的开始,也必须要有索引。
在接口测试工具中,创建索引必须使用put请求,也就是http://ip地址:端口号/索引名称。
查看全部索引
GET _cat/indices?v ?V可以查看更全的信息
创建索引
PUT index 注意:索引名不能参杂大写字母,可以写中文
删除索引
DELETE index
索引查询
GET index
查看索引对应头的含义
创建文档,修改文档,删除文档,
在Elasticsearch中,创建索引后,我们需要创建文档并添加数据。在这里,文档可以类比关系数据库中的表数据,而数据的格式通常采用JSON格式。为了创建文档并添加数据,我们可以使用POST和PUT请求。需要注意的是,POST请求不是幂等的,而PUT请求是幂等的。在这里,我们使用POST请求来创建文档。
这里我们创建文档的API格式为:IP地址:9200/索引名/_doc。其中,_doc表示创建文档的操作。使用POST请求创建文档时,必须提供一个JSON格式的请求体,否则会导致错误。
创建文档
post http://localhost:9200/index/_doc
{
\"name\":\"张三\",
\"age\": 20
}
-----------------------------------------
{
\"_index\": \"index\",
\"_type\": \"_doc\",
\"_id\": \"Qr5N1YkBmrPK3k6pyF3T\",
\"_version\": 1,
\"result\": \"created\",
\"_shards\": {
\"total\": 2,
\"successful\": 1,
\"failed\": 0
},
\"_seq_no\": 2,
\"_primary_term\": 1
}
上面这种请求方式必须是post,也就是说请求没有指定明确的主键id,没有保证幂等性,所以不能使用put,但是下面这种就可以使用put,因为指定了id。
post/put http://localhost:9200/index/_doc/1
{
\"name\": \"张三\",
\"age\": 20
}
----------------------------------
{
\"_index\": \"index\",
\"_type\": \"_doc\",
\"_id\": \"1\",
\"_version\": 1,
\"result\": \"created\",
\"_shards\": {
\"total\": 2,
\"successful\": 1,
\"failed\": 0
},
\"_seq_no\": 3,
\"_primary_term\": 1
}
查看文档
查询全部文档
GET http://localhost:9200/index/_search
------------------------------------------
{
\"took\": 837,
\"timed_out\": false,
\"_shards\": {
\"total\": 1,
\"successful\": 1,
\"skipped\": 0,
\"failed\": 0
},
\"hits\": {
\"total\": {
\"value\": 2,
\"relation\": \"eq\"
},
\"max_score\": 1.0,
\"hits\": [
{
\"_index\": \"index\",
\"_type\": \"_doc\",
\"_id\": \"1\",
\"_score\": 1.0,
\"_source\": {
\"name\": \"张三\",
\"age\": 20
}
},
{
\"_index\": \"index\",
\"_type\": \"_doc\",
\"_id\": \"2\",
\"_score\": 1.0,
\"_source\": {
\"name\": \"李四\",
\"age\": 20
}
}
]
}
}
根据文档的唯一id查询文档
GET http://localhost:9200/index/_doc/1
-----------------------------
{
\"_index\": \"index\",
\"_type\": \"_doc\",
\"_id\": \"1\",
\"_version\": 1,
\"_seq_no\": 0,
\"_primary_term\": 1,
\"found\": true,
\"_source\": {
\"name\": \"张三\",
\"age\": 20
}
}
修改文档
全局修改
POST http://localhost:9200/index/_doc/1
{
\"name\":\"张大炮\",
\"age\": 40,
\"sex\": \"未知\",
\"address\": \"轮回转世\"
}
-----------------------
{
\"_index\": \"index\",
\"_type\": \"_doc\",
\"_id\": \"1\",
\"_version\": 2,
\"result\": \"updated\", 对一个文档中的id反复post请求,只会是updated
\"_shards\": {
\"total\": 2,
\"successful\": 1,
\"failed\": 0
},
\"_seq_no\": 2,
\"_primary_term\": 1
}
上面这种指挥对这个文档中对应的id全部修改
修改字段
POST http://localhost:9200/index/_update/1
{
\"doc\":{
\"age\": 200
}
}
-------------------------------
{
\"_index\": \"index\",
\"_type\": \"_doc\",
\"_id\": \"1\",
\"_version\": 3,
\"result\": \"updated\",
\"_shards\": {
\"total\": 2,
\"successful\": 1,
\"failed\": 0
},
\"_seq_no\": 3,
\"_primary_term\": 1
}
删除文档
DELETE http://localhost:9200/index/_doc/1
-----------------------------
{
\"_index\": \"index\",
\"_type\": \"_doc\",
\"_id\": \"2\",
\"_version\": 2, 对数据库的操作都会更新版本(前面我已经删除一个了)
\"result\": \"deleted\", 删除一个不存在的文档显示 not_found
\"_shards\": {
\"total\": 2,
\"successful\": 1,
\"failed\": 0
},
\"_seq_no\": 6,
\"_primary_term\": 1
}
条件删除文档
一般删除数据都是根据文档的唯一性标识进行删除,实际操作时,也可以根据条件对多条数据进行删除
POST http://localhost:9200/index/_delete_by_query
{
\"query\":{ 查询
\"match\":{ 精确匹配
\"category\": \"华为\"
}
}
}
--------------------------------
{
\"took\": 7,
\"timed_out\": false, 是否超时
\"total\": 1, 删除后剩余总数
\"deleted\": 1, 删除数量
\"batches\": 1,
\"version_conflicts\": 0,
\"noops\": 0,
\"retries\": {
\"bulk\": 0,
\"search\": 0
},
\"throttled_millis\": 0,
\"requests_per_second\": -1.0,
\"throttled_until_millis\": 0,
\"failures\": []
}
还没有评论呢,快来抢沙发~