一、 基本概念
1. Node 与 Cluster
1)Elastic 本质上是一个分布式数据库,允许多台服务器协同工作,每台服务器可以运行多个 Elastic 实例
2)单个 Elastic 实例称为一个节点(node)。一组节点构成一个集群(cluster)
2. Index
1)Elastic 会索引所有字段,经过处理后写入一个反向索引(Inverted Index)。查找数据的时候,直接查找该索引。
2)Elastic 数据管理的顶层单位就叫做 Index(索引)。它是单个数据库的同义词。每个 Index (即数据库)的名字必须是小写。
3)查看当前节点的所有 Index : curl -X GET 'http://localhost:9200/_cat/indices?v'
3. Document
1)Index 里面单条的记录称为 Document(文档)。许多条 Document 构成了一个 Index。
2)同一个 Index 里面的 Document,不要求有相同的结构(scheme),但是最好保持相同,这样有利于提高搜索效率
二、新建和删除Index
1. 新建Index curl -X PUT 'localhost:9200/weather'
2. 删除Index curl -X DELETE curl 'localhost:9200/weather'
三、插件
1.安装ik分词器
1)在elasticsearch下的plugins文件夹中创建ik文件夹
2)wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.2.4/elasticsearch-analysis-ik-6.2.4.zip
3)unzip "压缩文件名"
4)重启,可以在日志里看到加载ik插件了(记得解压的那层文件夹去掉,会报错)
5)新建索引 curl -X PUT 'localhost:9200/accouunts'
6)新建索引结构 curl -X PUT 'localhost:9200/accouunts' -H 'Content-Type:application/json' -d 'json格式的数据'
2. chrome插件sense (或者kibana的tool也可以)
1) 找sense插件
2)chrome浏览器安装,记得打开开发者模式,以及保护网站的选项
3.head安装
1)下载地址 https://github.com/mobz/elasticsearch-head,选择download
2)后续真的要学再说
四、数据操作
- 向指定的 /Index/Type 发送 PUT 请求,就可以在 Index 里面新增一条记录。比如,向/accounts/person发送请求,就可以新增一条人员记录
curl -H 'Content-Type:application/json' -X PUT 'localhost:9200/accounts/person/1' -d '{
"user": "张三",
"title": "工程师",
"desc": "数据库管理"
}'
- 向/Index/Type/Id发出 GET 请求,就可以查看这条记录
curl 'localhost:9200/accounts/person/1?pretty=true'
- 删除数据
curl -X DELETE 'localhost:9200/accounts/person/1'
- 使用 GET 方法,直接请求/Index/Type/_search,就会返回所有记录
curl 'localhost:9200/accounts/person/_search'
- 查询语法
curl 'localhost:9200/accounts/person/_search' -d '
{
"query" : { "match" : { "desc" : "管理" }},
"size": 1
}'
- 网页上一套骚操作
1)http://localhost:9200/ elasticsearch开启
2)http://localhost:9200/_cat/indices?v 所有索引
3)http://localhost:9200/tomcat-2018.08.07 索引结构(可以找到type)
4)http://localhost:9200/tomcat-2018.08.07/doc/_search 所有记录(默认10条)
4)http://localhost:9200/tomcat-2018.08.07/doc/id 单条记录
参考网址
注:文章是经过参考其他的文章然后自己整理出来的,有可能是小部分参考,也有可能是大部分参考,但绝对不是直接转载,觉得侵权了我会删,我只是把这个用于自己的笔记,顺便整理下知识的同时,能帮到一部分人。
ps : 有错误的还望各位大佬指正,小弟不胜感激