ElasticSearch使用小结
最近有个业务需求,即全文搜索关键字查询列表,因而转向ES的学习,也学习了大半个月了,做个笔记,总结下自己的学习历程。
独自学习一项新技术,总是难免走不少弯路的,在此推荐下ES的基础教程,对,好好学习官网教程就可以了!
1) Elasticsearch: 权威指南
https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html
权威中文教程,对于英文不好的同学,读这个教程可以快速入门。
2)ElasticsearchReference 官网英文教程
https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html
真正想吃透ES还是多看英文官网文档,知识点讲的是最全面的。
ES版本选择
说道ES版本选择,初学ES的时候,查到的大部分资料都是针对2.x版本的,本打算也是用2.x版本,但是读到ES5.x版本新特性说明的时候,果断还是选用5.x版本,因为新版ES性能比2.x版本好太多了,而且本来也想使用spring-data-elasticsearch,但是spring-data不支持5.x版本,而且ES发展势头强劲,半个月前使用的5.5.0版本,现在已经更新到5.5.1。
ES客户端请求方式
1)Java API:创建TransportClient,复杂应用推荐使用
2)Java REST Client:创建RestClient
3)http restful api:使用最原始的http请求访问
目前暂时使用的第三种,原因ES业务需求单一,不需要动态创建、删除索引,上手简单,只需要学习es rest语法就可以了。其实后期可以切换到RestClient,它是持久化http链接(使用httpClient还需要一个http连接池),特点如官方所说:
The low-level client’s features include:
- minimal dependencies
- load balancing across all available nodes
- failover in case of node failures and upon specific response codes
- failed connection penalization (whether a failed node is retrieddepends on how many consecutive times it failed; the more failed attempts thelonger the client will wait before trying that same node again)
- persistent connections
- trace logging of requests and responses
- optional automatic discovery of cluster nodes
ES常用插件
1)head插件
5.5使用教程:http://www.cnblogs.com/xing901022/p/6030296.html
2)ik中文分析器 – 中文分词必备,可以自定义词典
github地址:https://github.com/medcl/elasticsearch-analysis-ik
插件下载地址:https://github.com/medcl/elasticsearch-analysis-ik/releases 博主更新很及时,5.5.1的已经有啦。
3)pinyin分析器
github地址: https://github.com/medcl/elasticsearch-analysis-pinyin
ik和pinyin同一个作者,elastic中文社区创始人。
4)elasticsearch-analysis-lc-pinyin分析器
这一款插件也很不错,但是没有pinyin声势大。支持全拼、首字母、中文混合搜索。后面拼音全文搜索准备测试下效果,目前分析器使用的还是ik和pinyin。
ES集群
ES集群的配置,权威教程讲的很粗糙,当时还花了好几天返回测试,最终发现还是配置文件参数没有吃透。
Minimum Master Nodes
最小主节点数的设置对集群的稳定是非常重要的。该设置对预防脑裂是有帮助的,即一个集群中存在两个master。
这个配置就是告诉Elasticsearch除非有足够可用的master候选节点,否则就不选举master,只有有足够可用的master候选节点才进行选举。
该设置应该始终被配置为有主节点资格的法定节点数,法定节点数:(主节点资格的节点数/2)+1。例如:
1、如果你有10个符合规则的节点数,法定数就是6.
2、如果你有3个候选master,和100个数据节点,法定数就是2,你只要计算那些有主节点资格的节点数就可以了。
3、如果你有2个符合规则的节点数,法定节点数应该是2,但是这意味着如果一个节点狗带了,你的整个集群就不可以用了。设置成1将保证集群的功能,但是就不能防止脑裂了。基于这样的情况,最好的解决就是至少有3个节点。
小集群或本地测试可以不用区分master node,data node,client node。但生产环境为了保证最大的可伸缩性,官方建议不同的类型节点加以区分,默认情况的elasticsearch既是master node,也是data node。关于节点的知识,可参看转载的《Elasticsearch节点类型》。
我目前使用的集群配置:一个Client节点,3个master/data混合节点。使用RestClient可以省去一个Client节点。
创建索引、类型示例
{
"settings":{
"index":{
"number_of_shards":3,
"number_of_replicas":1,
"analysis":{
"analyzer":{
"ik_analyzer":{
"type":"custom",
"tokenizer":"ik_smart"
},
"pinyin_analyzer":{
"tokenizer":"my_pinyin"
}
},
"tokenizer":{
"my_pinyin":{
"type":"pinyin",
"keep_original":true
}
}
}
}
}
}
{
"ProductTour":{
"properties":{
"companyId":{
"type":"integer"
},
"productCode":{
"type":"keyword"
},
"productType":{
"type":"text",
"analyzer":"ik_analyzer",
"fields":{
"pinyin":{
"type":"text",
"analyzer":"pinyin_analyzer"
}
}
},
"gType":{
"type":"keyword"
},
"lineType":{
"type":"keyword"
},
"productState":{
"type":"boolean"
},
"auditState":{
"type":"integer"
},
"productMainTitle":{
"type":"text",
"analyzer":"ik_analyzer",
"fields":{
"pinyin":{
"type":"text",
"analyzer":"pinyin_analyzer"
}
}
},
"productSubTitle":{
"type":"text",
"analyzer":"ik_analyzer",
"fields":{
"pinyin":{
"type":"text",
"analyzer":"pinyin_analyzer"
}
}
},
"supplyProductName":{
"type":"keyword"
},
"productMainPic":{
"type":"keyword"
},
"productPic":{
"type":"keyword"
},
"dpt":{
"type":"keyword"
},
"arr":{
"type":"text",
"analyzer":"ik_analyzer",
"fields":{
"pinyin":{
"type":"text",
"analyzer":"pinyin_analyzer"
}
}
},
"productFeatures":{
"type":"text",
"analyzer":"ik_analyzer",
"fields":{
"pinyin":{
"type":"text",
"analyzer":"pinyin_analyzer"
}
}
},
"tripDay":{
"type":"integer"
},
"tripNight":{
"type":"integer"
},
"advanceDays":{
"type":"integer"
},
"auditResult":{
"type":"keyword"
},
"createTime":{
"type":"date"
}
}
}
}
{
"from":0,
"size":10, // 分页查询
"query":{
"bool":{
"must":[
{
"multi_match":{ // 全文搜索
"query":"1日", // 关键词
"fields":[ // 全文搜索字段
"productType",
"productMainTitle",
"productSubTitle",
"arr",
"productFeatures"
]
}
}
],
"filter":[ // 筛选条件
{
"term":{
"productType":"themt"
}
}
]
}
}
}
如果关键字为字母混合汉字,全文搜索字段换成:
"fields": [
//全文搜索字段"productType.pinyin",
"productMainTitle.pinyin",
"productSubTitle.pinyin",
"arr.pinyin",
"productFeatures.pinyin"
]
使用中遇到的坑:
ElasticSearch使用小结的更多相关文章
- Elasticsearch使用小结之冷热分离
Elasticsearch使用小结之冷热分离 索引迁移 索引setting中的index.routing.allocation.exclude和index.routing.allocation.inc ...
- ElasticSearch 使用小结
写在前面 要做个元数据服务,包括存储和查询.元数据除了一些基本字段外,其他格式是自由的,存储输入为一个JSON形式.比如下面是一个文件对象的元数据: { "name":" ...
- ES使用小结之索引Rollover
Elasticsearch 使用小结之索引Rollover 索引名 一般而言,客户端将数据每天写入一个索引,比如直接写入YYYY-MM-HH格式的索引,那么我们只需要在写入的客户端里面获取时间,然后得 ...
- Elasticsearch alias别名管理小结
Elasticsearch alias别名管理小结 By:授客 QQ:1033553122 建创测试数据 1 创建别名 2 移除别名 3 创建测试数据 4 批量操作 5 例1. 5 例2. 把多个索引 ...
- 面试小结之Elasticsearch篇(转)
最近面试一些公司,被问到的关于Elasticsearch和搜索引擎相关的问题,以及自己总结的回答. Elasticsearch是如何实现Master选举的? Elasticsearch的选主是ZenD ...
- Elasticsearch全文检索实战小结
一.项目概述 这是一个被我称之为“没有枪.没有炮,硬着头皮自己造”的项目.项目是和其它公司合作的三个核心模块开发. 使用ES的目的是: 1).采集数据.网站数据清洗后存入ES: 2).对外提供精确检索 ...
- Elasticsearch学习笔记(七)document小结
一.生成document id 1.自动生成document id 自动生成的id,长度为20个字符,URL安全,base64编码,GUID,分布式系统并行生成时不可能 ...
- 面试小结之Elasticsearch篇
https://www.cnblogs.com/luckcs/articles/7052932.html
- 搜索引擎 ElasticSearch 之 步步为营1 【环境搭建&初识ElasticSearch】
1.下载ElasticSearch a.下载Java环境JDK:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloa ...
随机推荐
- 工具类css框架
@charset "UTF-8"; * { -webkit-box-sizing: border-box; box-sizing: border-box; ...
- C++编程学习(九)this指针&友元函数
mooc西工大魏英老师的课程通道关闭了,难受.现在边看工程代码边重温刷第一遍C++时候的知识点,顺序没有按照大纲的来,想到哪写到哪. this是干啥用的? 简介:在 C++ 中,每一个对象都能通过 t ...
- Python的一些常用知识
1.How to force urllib2 not to use a proxy Here is an example to remove proxy settings for all reques ...
- SpringBoot#自定义配置的封装
_震惊,开局 不可避免的需要弄一些自定义的配置. 要点: 1. 把配置项都写出来,分析层次关系:2. 抽象成bean与bean之间的关系,写出bean对应的类,这时候配置项对应了bean的属性,属性可 ...
- tensorflow学习笔记--dataset使用,创建自己的数据集
数据读入需求 我们在训练模型参数时想要从训练数据集中一次取出一小批数据(比如50条.100条)做梯度下降,不断地分批取出数据直到损失函数基本不再减小并且在训练集上的正确率足够高,取出的n条数据还要是预 ...
- [Machine Learning][BP]The Vectorized Back Propagation Algorithm
Reference: https://www.cs.swarthmore.edu/~meeden/cs81/s10/BackPropDeriv.pdf I spent nearly one hour ...
- c# 类型转换 int.TryParse() 方法
public static bool TryParse(string s, out Int32 result); 如果转换成功则返回true.否则返回false int.TryParse(string ...
- IT日常技能:VMware网络配置
1.0 基本概念 集线器:把一流量为M的端口分为N个端口,每个端口流量为M/N 交换机:把一流量为M的端口分为N个端口,每个端口流量仍为M 路由器:相当于两块网卡,一块连接外网并负责NAT, 另一块负 ...
- CMD手动打jar包
代码: jar -cvfM "jarpage.jar" @fileslist.txt 解析: 将文档(fileslist.txt)中所有路径对应文件打成jar包,取名为:jarpa ...
- 环境变量和文件查找&文件打包与解压缩
环境变量和文件查找 介绍环境变量的作用与用法 及几种搜索文件的方法 学会这些技巧可以高效地使用 Linux 知识点:环境变量的设置 环境变量的修改 环境变量 要解释环境变量,得先明白变量是什么,准确的 ...