一:elasticsearch: Docker方式安装
# 1.获取镜像
docker pull elasticsearch:7.14.0
2.运行es
docker run -d -e ES_JAVA_POTS="-Xms256m -Xmx256m" -e "discovery.type=single-node" -p 9200:9200 -p 9300:9300 --name es elasticsearch:7.14.0
//映射对应的分词器
docker run -v /root/es/ik-7.14.0:/usr/share/elasticsearch/plugins/ik-7.14.0 -d -e ES_JAVA_POTS="-Xms256m -Xmx256m" -e "discovery.type=single-node" -p 9200:9200 -p 9300:9300 --name es elasticsearch:7.14.0
//问题解决:
https://blog.csdn.net/qq_43308337/article/details/90069371
sysctl -w vm.max_map_count=262144
sysctl -a|grep vm.max_map_count
在 /etc/sysctl.conf文件最后添加一行
vm.max_map_count=262144
source sysctl.conf
docker logs -f es
3.访问ES
curl http://localhost:9200/
二:Docker方式安装kibana
1.获取镜像
docker pull kibana:7.14.0
#2.运行 kibana
docker run -d --name kibana -p 5601:5601 kibana:7.14.0
# 3.进入客器连接到ES.重启k1bana容器,访问 htt0://10.15 8.3:5581
# 4.基于数据卷加战配置文件方式运行
docker run -d --name kibana -p 5601:5601 -v /root/config:/usr/share/kibana/config kibana:7.14.0
三:elasticsearch 索引
# 1 查询索引 -v 格式化显示
GET /_cat/indices?v
# 2 创建索引
PUT /products
PUT /orders
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
}
}
# 3 删除索引
DELETE /products
四:elasticsearch 映射
映射 类型:
字符串类型:keyword text
数字类型:integer long
小数类型:float double
布尔类型:boolean
日期类型:date
// 创建映射类型
PUT /products
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"properties": {
"id":{
"type":"integer"
},
"title":{
"type":"keyword"
},
"price":{
"type":"double"
},
"created_at":{
"type":"date"
},
"description":{
"type":"text"
}
}
}
}
五:elasticsearch 文档
# 1 添加文档 手动 指定文档id
POST /products/_doc/2
{
"title":"iphon13",
"price":13.4,
"created_at":"2022-04-24",
"description":"很好用"
}
# 2 添加文档 自动创建文档id
POST /products/_doc/
{
"title":"iphon12",
"price":13.4,
"created_at":"2022-04-24",
"description":"很好用"
}
# 3文档查询:
GET /products/_doc/5
# 4 删除文档
DELETE /products/_doc/1
# 5 更新文档 这种方式:删除、原始文档 从新开始添加
PUT /products/_doc/4QyYWoAB69iYBBWP7pcr
{
"title":"小浣熊"
}
# 6 更新文档 这种方式:基于指定字段更新 是先把老的查出来 再更新对应的字段
POST /products/_doc/4gyfWoAB69iYBBWPm5f1/_update
{
"doc" :{
"title":"iphone15"
}
}
# 7 文档批量操作
# 批量添加:
POST /products/_doc/_bulk
{"index":{"_id":4}}
{"title":"小浣熊1","price":13.4,"created_at":"2022-04-24","description":"很好用"}
{"index":{"_id":5}}
{"title":"小浣熊2","price":13.4,"created_at":"2022-04-24","description":"很好用"}
# 8 批量添加 更新 删除
POST /products/_doc/_bulk
{"index":{"_id":6}}
{"title":"小浣熊1","price":13.4,"created_at":"2022-04-24","description":"很好用"}
{"update":{"_id":5}}
{"doc":{"title":"小鱼肚父" }}
{"delete":{"_id":4}}
六:高级查询query DSL :
语法:
get /索引名/_doc/_search {json 格式请求体数据}
get /索引名/_search {json 格式请求体数据}
# 1 query DSL 语法 查询所有 match_all
GET /products/_doc/_search
{
"query":{
"match_all":{}
}
}
# 2 query DSL 语法 关键词查询 (term)
# keyword 类型 搜索 要搜索全部内容,不分词
# text 类型 默认 es 标准分词器 中文单子分词 英文 单词分词
GET /products/_search
{
"query": {
"term": {
"title": {
"value": "小鱼肚父"
}
}
}
}
#3 query DSL 语法 范围查询 (range)
GET /products/_search
{
"query": {
"range": {
"price": {
"gte": 0,
"lte": 20
}
}
}
}
# 4 query DSL 语法 前缀查询 (prefix)
GET /products/_search
{
"query": {
"prefix": {
"title": {
"value": "小浣"
}
}
}
}
#5 query DSL 语法 通配符查询 (wildcard)?用来匹配一个任意字符 * 用来匹配多个任意字符
GET /products/_search
{
"query": {
"wildcard": {
"title": {
"value": "小*"
}
}
}
}
#6 query DSL 语法 多id查询
GET /products/_search
{
"query": {
"ids": {
"values": ["5","6"]
}
}
}
#7 query DSL 语法 模糊查询 (fuzzy)
# 最大模糊错误 必须在0-2 之间
# 搜索关键词长度为2 不允许存在模糊
# 搜索关键词长度为3-5 允许1次模糊
# 搜索关键词长度大于5 不允最大2模糊
GET /products/_search
{
"query": {
"fuzzy": {
"title": "小浣熊"
}
}
}
# 8布尔查询[bool]
# bool关键字:用来组合多个条件实现复杂查询
# must:相当于&&同时成立
# should:相当于成立一个就行
#must_not:相当于!不能满足任何一个
GET /products/_search
{
"query": {
"bool": {
"must_not": [
{
"ids":{
"values": [1]
}
},
{
"term": {
"title": {
"value": "小浣熊"
}
}
}
]
}
}
}
# 9 多字段查询 (multi_match)
GET /products/_search
{
"query": {
"multi_match": {
"query": "很好用",
"fields": ["title","description"]
}
}
}
# 10 默认字段分词查询 (query_string)
GET /products/_search
{
"query": {
"query_string": {
"default_field": "description",
"query": "很"
}
}
}
七:elasticsearch 分页查询
# 1 返回指定条数(size)
GET /products/_search
{
"query": {
"query_string": {
"default_field": "description",
"query": "很"
}
},
"highlight": {
"pre_tags": ["<span style='color:red;'> "],
"post_tags":["/span>"],
"fields": {
"*":{}
}
},
"size": 2
}
# 2 分页查询(form)
GET /products/_search
{
"query": {
"query_string": {
"default_field": "description",
"query": "很"
}
},
"highlight": {
"pre_tags": ["<span style='color:red;'> "],
"post_tags":["/span>"],
"fields": {
"*":{}
}
},
"from": 2,
"size": 2
}
# 3 指定字段排序(sort)
GET /products/_search
{
"query": {
"query_string": {
"default_field": "description",
"query": "很"
}
},
"highlight": {
"pre_tags": ["<span style='color:red;'> "],
"post_tags":["/span>"],
"fields": {
"*":{}
}
},
"from": 0,
"size": 7,
"sort": [
{
"price": {
"order": "asc"
}
}
]
}
# 4 返回指定字段(_souce)
GET /products/_search
{
"query": {
"query_string": {
"default_field": "description",
"query": "很"
}
},
"highlight": {
"pre_tags": ["<span style='color:red;'> "],
"post_tags":["/span>"],
"fields": {
"*":{}
}
},
"from": 0,
"size": 7,
"sort": [
{
"price": {
"order": "asc"
}
}
],
"_source": ["title","price"]
}
八:elasticsearch 索引原理
倒排索引
(Inverted Index)也叫反向索引,有反向索引必有正向索引。
通俗地来讲,正向索引是通过key找value,反向索引则是通过value找key。ES底层在检索时底层使用的就是倒排索引。
索引模型
现有索引和映射如下:
{
"products”:{
"mappings":{
"properties":{
"description":{"type":"text"},
"price":{"type":"float"},
"title":{"type" : "keyword"}
}
}
}
}
九:elasticsearch 分词器
Analysis和Analyzer
Analysis:文本分析是把全文本转换一系列单词(term/token)的过程,也叫分词
(Analyzer)。Analysis是通过Analyzer来实现的。分词就是将文档通过Analyzer分成一个一个的 Term(关键词查询),每一个Term都指向包含这个Term的文档"。
Analyzer组成
●注意:在ES中默认使用标准分词器:StandardAnalyzer特点:中文单字分词 英文单词分词
我是中国人this is good man-->analyzer->我 是 中 国 人 this is good man
分析器(analyzer)都由三种构件组成的:character filters , tokenizers , token filters.
character filter 字符过滤器:
。在一段文本进行分词之前,先进行预处理,比如说最常见的就是,过滤html标签
(<span>hello<span>…> hello),&-->and (I&you--> I and you)
tokenizers 分词器:
英文分词可以根据空格将单词分开,中文分词比较复杂,可以采用机器学习算法来分词。
Token filters Token过滤器:
将切分的单词进行加工。大小写转换(例将“Quick”转为小写),去掉停用词(例如停用词像“a”、“and”、“the”等等),加入同义词(例如同义词像“jump”和“leap”。
注意:
三者顺序:Character Filters…>Tokenizer-->Token Filter
三者个数:Character Filters(0个或多个)+Tokenizer+TokenFilters(0个或多个)
----------------------------------------------------------------------------------------------------------
内置分词器
standard Analyzer 默认分词器,英文按单词分词,并小写处理 中文单字分词
simple Analyzer 按照单词分词 英文统一转小写 去掉符号,中文按照空格分词。
stop Analyzer 小写处理,停用词过滤(the,a,is)
whitespace Analyzer 按照空格切分,不转小写 不去掉标点符号
keyword Analyzer 不分词,直接将输入当作输出
内置分词器测试
1 标准分词器:
# 标准分词器: 默认standard
PUT /tests
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"properties": {
"description":{
"type":"text",
"analyzer": "standard"
}
}
}
}
PUT /tests
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"properties": {
"description":{
"type":"text",
"analyzer": "whitespace"
}
}
}
}
中文分词器:
在ES中支持中文分词器非常多如smartCNIK等,推荐的就是IK分词器。
安装IK
开源分词器lk的github: https://github.com/medcl/elasticsearch-analysis-ik
注意:IK分词器的版本要你安装ES的版本一致
注意:Docker容器运行ES安装插件目录为 /usr/share/elasticsearch/plugins
# 1.下载对应版本
.[es0linux~] wget
https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.14.0/elasticsearch-analysis-ik-7.14.0.zip
# 2.解压
-[es@linux~1S unzip elasticsearch-analysis-ik-6.2.4.zip #先使用yuminstall -yunzip
#3.移动到es安装目录的plugins目录中
[es0linux~]$ 1s elasticsearch-6.2.4/plugins/
[esPlinux~1s mv elasticsearch elasticsearch-6.2.4/plugins/
[es@linux~]$ 1s elasticsearch-6.2.4/plugins/ elasticsearch
[eselinux~]S 1s elasticsearch-6.2.4/plugins/elasticsearch/
commons-codec-1.9.jar config httpclient-4.5.2.1arplugin-descriptor.properties
commong-logging-1.2.jar elasticsearch-analynis-1k-6.2.4.jar httpcore-4.4.4.1ar 自动 2.0x
# ik 2种
# ik_smart :会做最粗粒度的拆分
# ik_max_word :将文本做最细粒度的拆分
POST /_analyze
{
"analyzer": "ik_max_word",
"text": "中华人民共和国国歌"
}
PUT /tests
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"properties": {
"description":{
"type":"text",
"analyzer": "ik_max_word"
}
}
}
}
=========================================
扩展词、停用词配置
IK支持自定义 扩展词典 和 停用词典
扩展词典:就是有些词并不是关键词,但是也希望被ES用来作为检索的关键词,可以将这些
词加入扩展词典。
停用词典:就是有些词是关键词,但是出于业务场景不想使用这些关键词被检索到,可以将这些词放入停用词典。
定义扩展词典和停用词典可以修改IK分词器中config目录中 IKAnalyzercfg.xml 这个文件。
1.修改vim IKAnalyzer.cfg.xml
<?xmlversion="1.0"encoding=“UTF-8"?>
<!DOCTYPE properties SYSTEM"http://java.sun.com/dtd/properties.dtd"><properties>
<comment>IK Analyzer 扩展配置</comment><!--用户可以在这里配置自己的扩展字典 -->
<entry key="ext_dict">ext dict.dic</entry><!--用户可以在这里配置自己的扩展停止词字典-->
<entry key="ext_stopwords">ext_stopword.dic</entry></properties>
===================
过滤查询:
过滤查询
<filter query>,其实准确来说,ES中的查询操作分为2种:查询(query)和过滤(filter)。
查询即是之前提到的query查询,它(查询)默认会计算每个返回文档的得分,然后根据得分排序。
而过滤(filter)只会筛选出符合的文档,并不计算得分,而且它可以缓存文档。所以,单从性能考虑,过滤比查询更快。换句话说过滤适合在大范围筛选数据,而查询则适合精确匹配数据,一般应用时,,应先使用过滤操作过谑数据,,然后使用查询匹配数据。
使用:
GET /ems/emp/_search
"query":("bool":{"must*:[
("match_al1":{}} //查询条件],
"filter":{....} //过滤条件
)
·注意:
在执行filter和query时先执行filter在执行query
Elasticsearch会自动缓存经常使用的过滤器,以加快性能
常见过滤类型有:
term 匹配字段
terms 多个字段
ranage 范围过滤
exists 存在字段的过滤
dis 等filter。
十:springboot 整合:
整合应用
1 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
2 配置客户端
@Configuration
public class RestClientConfig extends AbstractElasticsearchConfiguration{
@Override
@Bean
public RestHighLevelClient elasticsearchClient(){
final ClientConfiguration clientConfiguration=ClientConfiguration.builder()
.connectedTo(17216.9110:9208")
.build();
return RestClients.create(clientConfiguration).rest();
}
}
-------
客户端对象
ElasticsearchOperations
RestHighLevelClient推荐
ElasticsearchOperations
特点:始终使用面向对象方式操作ES
索引:用来存放相似文档集合
映射:用来决定放入文档的每个字段以什么样方式录入到ES中字段类型分词器.
文档:可以被索引最小单元ison数据格式
注:以上为es 初步探索内容