ElsaticSearch是基于Lucene做了一些封装和增强(我们上手是十分简单)
ElasticSearch是一个分布式全文检索引擎,近乎实时数据、实时检索
ElasticSearch是面向文档
倒排索引
帮助我们快速检索数据 文件落地解决大数据搜索(全文检索)
term index(FST算法)、term dictionary(FST算法)、posting list(RBM算法)
es默认分词器
GET _analyze
{
“analyzer”: “ik_smart”
, “text”: ["*******"]
}
GET _analyze
{
“analyzer”: “ik_max_word”
, “text”: ["********"]
}
编写自定义分词器
IKAnalyzer.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 扩展配置</comment>
<!--用户可以在这里配置自己的扩展字典 -->
<entry key="ext_dict">自定义.dic</entry>
<!--用户可以在这里配置自己的扩展停止词字典-->
<entry key="ext_stopwords"></entry>
<!--用户可以在这里配置远程扩展字典 -->
<!-- <entry key="remote_ext_dict">words_location</entry> -->
<!--用户可以在这里配置远程扩展停止词字典-->
<!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>
ES中的crud
索引增加
PUT /test3/_doc/1
{
"name" : "****",
"age" : 25,
"birth" : "1996-01-01"
}
索引查询
GET /test3
索引修改
1.直接put,但是会丢失字段
2.POST /test3/_doc/1/_update
{
"doc" :{
"name" : "****"
}
}
索引删除
DELETE test
查询文档
GET test3/_doc/_search
{
"query" :{
"match" :{
"name" : "凯"
}
},
"_source": ["name"]
}
使用RESTFUL是ES推荐大家使用的
keyword字段类型不会被分词器解析
高亮查询(可以自定义高亮)
es中默认的评分算法TF/IDF、BM25
es写性能优化
优化方案:
- 客户端: 多线程写,批量写
- ES:在高质量数据建模前提下,主要在refresh,translog和flush之间- 做文章
- 写性能优化 - refresh
- 增大refresh_interval,降低实时性,增大一次refresh处理的文档树,默认是1s,设置为-1直接禁止自动refresh
- 增大index buffer size ,参数 indices.memory.index_buffer_size(静态参数,在elasticsearch.yml),默认10%
- 写性能优化 - translog
- index.translog.durability设置为async(异步模式),index.translog.sync_interval设置需要的大虾片,比如120s,那么translog会120s写一次磁盘
- index.translog.flush_threshold_size默认512mb,translog超过该大小会触发flush
- 写性能优化 - 其他
- 副本设置为0,写完再增加
- 合理设计shard数,并保证shard均匀分配在所有的node上,充分利用node资源
· index.routing.allocation.total_shards_per_node限定每个索引在每个node上可分配的总主副分片数
· 5个node,某索引10个分片,1个副本,如何设置
~ (10 + 10)/5=4
~ 实际要设置5个,防止node下线时,分片迁移失败