<!doctype html>elasticsrarch
x
ElasticSearch: https://mirrors.huaweicloud.com/elasticsearch/?C=N&O=D
logstash: https://mirrors.huaweicloud.com/logstash/?C=N&O=D
kibana: https://mirrors.huaweicloud.com/kibana/?C=N&O=D
ElasticSearch:搜索!(百度,github,淘宝电商)
库:表
表:在8的版本不用表了
ElasticSeatch安装
声明:JDK1.8,最低要求!ElasticSearch客户端,界面工具!
xxxxxxxxxx
下载的版本是:elasticsearch-7.6.2-windows-x86_64.zip
解压即可以使用了
xxxxxxxxxx
目录:
bin 启动文件
config 配置文件
jvm.options java虚拟机的配置文件
elasticsearch.yml elasticsearch 的配置文件! 默认地址是9200端口,通信地址是9300。
lib 相关的jar包
modules 功能模块
logs 日志
plugins 插件
xxxxxxxxxx
启动:
打开bin 目录点击elasticsearch.bat
打开浏览器:127.0.0.1:9200
安装可视化界面
x
1、下载地址:https://github.com/mobz/elasticsearch-head
2、启动需要先安装Nodejs的环境参考教程:https://www.cnblogs.com/jianguo221/p/11487532.html
3、在elasticsearch-head的目录里面命令行界面 nmp install 安装依赖包
4、启动elasticsearch-head:npm run start
连接elasticsearch-head:127.0.0.1:9100
5、解决9100连接9200的跨域问题
在elasticsearch.yml中配置
http.cors.enabled: true
http.cors.allow-origin: "*"
安装kibana 显示界面
xxxxxxxxxx
解压之后,进入bin目录打开kibana.bat启动kibana
kibana的默认启动端口是5601
127.0.0.1:5601 进入kibanna的界面
xxxxxxxxxx
汉化
kibana自带汉化包
kibana-7.6.2-windows-x86_64kibana-7.6.2-windows-x86_64x-packplugins ranslations ranslationszh-CN.json
修改配置
在kibana.yml中加一行:i18n.locale: "zh-CN"
重启kibana服务
xxxxxxxxxx
数据库 -->索引(index)
表 -->类型(types)
行 --->文档(documents)
字段 --->字段(fields)
IK 分词器
分词:即把一段中文或者别的划分成一个个的关键字,我们在搜索的时候会把自己的信息进行分词,会把数据库中进行分词,然后进行一个匹配操作,默认的中文分词器是将每个字看成一个词。
IK提供了两个分词算法:iK_smart 和ik_max_word 其中ik_smart为最少切片,ik_max_word为最细颗粒划分。
xxxxxxxxxx
1、下载:https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v7.6.2
wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.6.2/elasticsearch-analysis-ik-7.6.2.zip
2、下载完毕之后解压复制到elasticsearch中的plugin目录中建立一个ik的目录
3、查看是否正确加载插件,通过命令elasticsearch-plugin list 出现ik说明插件加载成功
或者http://127.0.0.1:9200/_cat/plugins查看插件是否运行成功
xxxxxxxxxx
###ik_smart模式的分词器
GET _analyze
{
"analyzer": "ik_smart",
"text": "吴志斌"
}
#####ik_max_word模式的分词器
GET _analyze
{
"analyzer": "ik_max_word",
"text": "吴志斌
}
xxxxxxxxxx
自定义自己的分词器
在ik的config里面新建一个my.dic的文档,将文档的名称配置在IKAnalyzer.cfg.xml里面重启即可使用自己的分词器。
以后自己自定义的此就可以在my.dic中进行配置,可以提高搜索速度。
Rest风格说明
基本的Rest命令说明:
method | url | 描述 |
---|---|---|
PUT | /索引名称/类型名称/文档id | 创建文档(指定文档id) |
POST | /索引名称/类型名称 | 创建文档(随机文档id) |
POST | /索引名称/类型名称/文档id/_update | 修改文档 |
DELETE | /索引名称/类型名称/文档id | 删除文档 |
GET | /索引名称/类型名称/文档id | 查询文档通过文档的id |
POST | /索引名称/类型名称/_search | 查询所有的数据 |
常见的类型
-
字符串类型
text keywo
-
数值类型
long, integer, short, byte, double, float, half, scaled, float
-
日期类型
date
-
布尔值类型
boolean
-
二进制类型
binary
-
等
增
xxxxxxxxxx
PUT /test1/type1/2
{
"name": "吴志斌",
"age": 3
}
xxxxxxxxxx
#结果
{
"_index" : "test1",
"_type" : "type1",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
创建索引的规则
xxxxxxxxxx
PUT /test2
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"age": {
"type": "long"
},
"birthday": {
"type": "date"
}
}
}
}
xxxxxxxxxx
###结果
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "test2"
}
可以通过GET请求获取具体的信息
xxxxxxxxxx
GET test2
xxxxxxxxxx
### 结果
{
"test2" : {
"aliases" : { },
"mappings" : {
"properties" : {
"age" : {
"type" : "long"
},
"birthday" : {
"type" : "date"
},
"name" : {
"type" : "text"
}
}
},
"settings" : {
"index" : {
"creation_date" : "1595060159703",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "rN5HjVSvR7Cetm000CnXNA",
"version" : {
"created" : "7060299"
},
"provided_name" : "test2"
}
}
}
}
如果自己的文档的字段没有指定,那么Es就会给我们的默认配置字段类型!
更新
- 通过put 覆盖原来的数据进行更新操作
通过put覆盖进行修改的化,必须数据的格式和前面插入的数据格式是一致的。
xxxxxxxxxx
###插入一条新的数据
PUT /test3/_doc/1
{
"name": "吴志斌",
"age": 13,
"birth": "1993-11-24"
}
###将数据的名称改为吴志斌很帅
PUT /test3/_doc/1
{
"name": "吴志斌很帅",
"age": 13,
"birth": "1993-11-24"
}
#修改后ES中只会存在一条数据,但是查询到的_version会更新,而result的状态由created变为了update
- 同POST 请求后面加_update进行更新
xxxxxxxxxx
POST /test3/_doc/1/_update
{
"doc": {
"name": "法外狂徒张三"
}
}
删
- 直接删除一个库
xxxxxxxxxx
DELETE test1
xxxxxxxxxx
####删除成功的结果
{
"acknowledged" : true
}
通过DELETE命令实现删除,根据请求来判断是删除索引还是删除文档记录!
xxxxxxxxxx
### 删除一个文档
DELETE test2/_doc/2
复杂查询
复杂操作搜索:排序,分页,高亮,模糊查询,精准查询
条件查询
x
_score:分数,匹配度越高,分数越高
query:查询的条件q
match:使用分词器解析,模糊匹配,包含即满足
_source:查询的结果
term:是通过倒排索引指定的词条进行精确查找
两个类型:
xxxxxxxxxx
GET huangshen/user/_search
{
"query": {
"match": {
"name": "吴志斌"
}
}
}
xxxxxxxxxx
##查询的结果
{
"took" : 910,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 2.0794413,
"hits" : [
{
"_index" : "huangshen",
"_type" : "user",
"_id" : "1",
"_score" : 2.0794413,
"_source" : {
"name" : "吴志斌",
"age" : 26,
"desc" : "一顿操作猛如虎,一看工资2500",
"tags" : [
"技术宅",
"温暖",
"直男"
]
}
},
{
"_index" : "huangshen",
"_type" : "user",
"_id" : "5",
"_score" : 1.633847,
"_source" : {
"name" : "吴志斌学Python",
"age" : 26,
"desc" : "帅哥女",
"tags" : [
"广东",
"温暖",
"热爱美女"
]
}
}
]
}
}
结果过滤(只显示设定的字段)
xxxxxxxxxx
GET huangshen/user/_search
{
"query": {
"match": {
"name": "吴志斌大丑比"
}
},
"_source": ["name","desc"]
}
xxxxxxxxxx
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 2.0794413,
"hits" : [
{
"_index" : "huangshen",
"_type" : "user",
"_id" : "1",
"_score" : 2.0794413,
"_source" : {
"name" : "吴志斌",
"desc" : "一顿操作猛如虎,一看工资2500"
}
},
{
"_index" : "huangshen",
"_type" : "user",
"_id" : "5",
"_score" : 1.633847,
"_source" : {
"name" : "吴志斌学Python",
"desc" : "帅哥女"
}
}
]
}
}
排序
xxxxxxxxxx
asc 是升序
desc 是倒序
GET huangshen/user/_search
{
"query": {
"match": {
"name": "吴志斌"
}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
xxxxxxxxxx
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "huangshen",
"_type" : "user",
"_id" : "1",
"_score" : null,
"_source" : {
"name" : "吴志斌",
"age" : 26,
"desc" : "一顿操作猛如虎,一看工资2500",
"tags" : [
"技术宅",
"温暖",
"直男"
]
},
"sort" : [
26
]
},
{
"_index" : "huangshen",
"_type" : "user",
"_id" : "5",
"_score" : null,
"_source" : {
"name" : "吴志斌学Python",
"age" : 26,
"desc" : "帅哥女",
"tags" : [
"广东",
"温暖",
"热爱美女"
]
},
"sort" : [
26
]
},
{
"_index" : "huangshen",
"_type" : "user",
"_id" : "7",
"_score" : null,
"_source" : {
"name" : "吴志斌大帅比",
"age" : 22,
"desc" : "喜欢漂亮妹子",
"tags" : [
"广东",
"温暖",
"美女"
]
},
"sort" : [
22
]
}
]
分页
xxxxxxxxxx
from 从哪里开始
size 返回多少条数据
GET huangshen/user/_search
{
"query": {
"match": {
"name": "吴志斌"
}
},
"sort": [
{
"age": {
"order": "desc"
}
}
],
"from": 0,
"size": 1
}
xxxxxxxxxx
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "huangshen",
"_type" : "user",
"_id" : "1",
"_score" : null,
"_source" : {
"name" : "吴志斌",
"age" : 26,
"desc" : "一顿操作猛如虎,一看工资2500",
"tags" : [
"技术宅",
"温暖",
"直男"
]
},
"sort" : [
26
]
}
]
}
}
布尔值查询
xxxxxxxxxx
bool :布尔查询
多条件查询
x
多条件:几个条件同时满足,才会有结果
精确查询:查询的条件不是模糊匹配,而是精确匹配,通过bool+must构造
must:所有的条件都需要满足
should:多条件只需要一个满足
must_not:不在条件里面
range:条件满足在这个范围内。
filter:对数据进行过滤
x
##1两个条件任意一个成立都有结果
GET huangshen/user/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "吴志斌"
}
},
{
"match": {
"age": 26
}
}
]
}
}
###2两个词任意一个满足都有结果
GET huangshen/user/_search
{
"query": {
"match": {
"tags": "男 温暖"
}
}
}
高亮查询
xxxxxxxxxx
highlight:搜索的结果会高亮显示
xxxxxxxxxx
GET huangshen/user/_search
{
"query": {
"match": {
"name": "吴志斌"
}
},
"highlight": {
"pre_tags": "<p class='key' style='color:red'>",
"post_tags": "</p>",
"fields": {
"name": {}
}
}
}
xxxxxxxxxx
###结果
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 2.31634,
"hits" : [
{
"_index" : "huangshen",
"_type" : "user",
"_id" : "1",
"_score" : 2.31634,
"_source" : {
"name" : "吴志斌",
"age" : 26,
"desc" : "一顿操作猛如虎,一看工资2500",
"tags" : [
"技术宅",
"温暖",
"直男"
]
},
"highlight" : {
"name" : [
"<p class='key' style='color:red'>吴</p><p class='key' style='color:red'>志</p><p class='key' style='color:red'>斌</p>"
]
}
},
{
"_index" : "huangshen",
"_type" : "user",
"_id" : "5",
"_score" : 1.8865037,
"_source" : {
"name" : "吴志斌学Python",
"age" : 26,
"desc" : "帅哥女",
"tags" : [
"广东",
"温暖",
"热爱美女"
]
},
"highlight" : {
"name" : [
"<p class='key' style='color:red'>吴</p><p class='key' style='color:red'>志</p><p class='key' style='color:red'>斌</p>学Python"
]
}
},
{
"_index" : "huangshen",
"_type" : "user",
"_id" : "7",
"_score" : 1.7263287,
"_source" : {
"name" : "吴志斌大帅比",
"age" : 22,
"desc" : "喜欢漂亮妹子",
"tags" : [
"广东",
"温暖",
"美女"
]
},
"highlight" : {
"name" : [
"<p class='key' style='color:red'>吴</p><p class='key' style='color:red'>志</p><p class='key' style='color:red'>斌</p>大帅比"
]
}
}
]
}
}
扩展
通过命令elasticsearch的索引情况
xxxxxxxxxx
GET _cat/indices?v #查看库信息