Elasticsearch 入门,基本概念和操作
基本概念
Node 与 Cluster
Elastic 本质上是一个分布式数据库,允许多台服务器协同工作,每台服务器可以运行多个 Elastic 实例。
单个 Elastic 实例称为一个节点(node)。一组节点构成一个集群(cluster)。
Index ( 对应数据库的表 )
Elastic 会索引所有字段,经过处理后写入一个反向索引(Inverted Index)。查找数据的时候,直接查找该索引。
所以,Elastic 数据管理的顶层单位就叫做 Index(索引)。它是表的同义词。每个 Index 的名字必须是小写。
下面的命令可以查看当前节点的所有 Index。
1 | curl -X GET 'http://localhost:9200/_cat/indices?v' |
ElasticSearch 默认开启9200、9300端口,9200端口供http访问,9300供tcp访问,ElasticSearch通过9300端口通信,
可以直接通过 http://localhost:9200 访问 ElasticSearch,为简化示例,后续都是通过curl方式演示相关操作
Document (对应数据库表的行)
Index 里面单条的记录称为 Document(文档)。许多条 Document 构成了一个 Index。
Document 使用 JSON 格式表示,下面是一个例子。
同一个 Index 里面的 Document,不要求有相同的结构(scheme),但是最好保持相同,这样有利于提高搜索效率。
1 2 3 4 5 | { "user" : "张三" , "title" : "工程师" , "desc" : "数据库管理" } |
Type(分类,elasticsearch 7.0之后弃用)
Document 可以分组,比如weather
这个 Index 里面,可以按城市分组(北京和上海),也可以按气候分组(晴天和雨天)。这种分组就叫做 Type,它是虚拟的逻辑分组,用来过滤 Document。
不同的 Type 应该有相似的结构(schema),举例来说,id
字段不能在这个组是字符串,在另一个组是数值。这是与关系型数据库的表的一个区别。性质完全不同的数据(比如products
和logs
)应该存成两个 Index,而不是一个 Index 里面的两个 Type(虽然可以做到)。
下面的命令可以列出每个 Index 所包含的 Type。
1 | curl 'localhost:9200/_mapping?pretty=true' |
Mapping(类似数据库中的表结构)
查看当前所有mapping
1 | curl 'localhost:9200/_mapping?pretty=true' |
基本操作
查看当前节点的所有Index
curl -X GET 'http://localhost:9200/_cat/indices?v'
列出每个 Index 所包含的 Type
curl 'localhost:9200/_mapping?pretty=true'
新建Index,下面命令,首先新建一个名称为news的 Index,里面有一个名称为newscontent
的 Type。news
有一个字段 content 。content字段都是中文,而且类型都是文本(text),所以需要指定中文分词器,不能使用默认的英文分词器。
创建index同时创建mapping
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | curl -X PUT http: //localhost :9200 /news -H 'Content-Type:application/json' -d' { "mappings" : { "newscontent" : { "properties" : { "content" : { "type" : "text" , "analyzer" : "ik_max_word" , "search_analyzer" : "ik_max_word" } } } } }' |
删除Index
curl -X DELETE 'localhost:9200/news'
新增数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | curl -XPOST http: //localhost :9200 /news/newscontent/1 -H 'Content-Type:application/json' -d' { "content" : "美国留给伊拉克的是个烂摊子吗" } ' curl -XPOST http: //localhost :9200 /news/newscontent/2 -H 'Content-Type:application/json' -d' { "content" : "公安部:各地校车将享最高路权" } ' curl -XPOST http: //localhost :9200 /news/newscontent/3 -H 'Content-Type:application/json' -d' { "content" : "中韩渔警冲突调查:韩警平均每天扣1艘中国渔船" } ' curl -XPOST http: //localhost :9200 /news/newscontent/4 -H 'Content-Type:application/json' -d' { "content" : "中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首" } ' |
查看某ID的数据
curl 'localhost:9200/news/newscontent/1?pretty=true'
返回所有记录
curl -XPOST http://localhost:9200/news/_search
返回搜索记录,并高亮显示 hightlight
1 2 3 4 5 6 7 8 9 10 11 12 | curl -XPOST http: //localhost :9200 /news/_search -H 'Content-Type:application/json' -d' { "query" : { "match" : { "content" : "中国" }}, "highlight" : { "pre_tags" : [ "<red>" ], "post_tags" : [ "</red>" ], "fields" : { "content" : {} } } } ' |
返回结果示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | { "took" : 14, "timed_out" : false , "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 2, "max_score" : 2, "hits" : [ { "_index" : "news" , "_type" : "newscontent" , "_id" : "4" , "_score" : 2, "_source" : { "content" : "中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首" }, "highlight" : { "content" : [ "<red>中国</red>驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首 " ] } }, { "_index" : "news" , "_type" : "newscontent" , "_id" : "3" , "_score" : 2, "_source" : { "content" : "中韩渔警冲突调查:韩警平均每天扣1艘中国渔船" }, "highlight" : { "content" : [ "均每天扣1艘<red>中国</red>渔船 " ] } } ] } } |
参考资料
全文搜索引擎 Elasticsearch 入门教程
作者: 阮一峰
http://www.ruanyifeng.com/blog/2017/08/elasticsearch.html
中文分词 elasticsearch-analysis-ik
https://github.com/medcl/elasticsearch-analysis-ik
CentOS7安装Elasticsearch
腾讯云实验室
https://www.cnblogs.com/gezifeiyang/p/11007727.html
Elasticsearch: 权威指南(官方文档)
https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述