最近有个业务需求,即全文搜索关键字查询列表,于是转向ES的学习,也学习了大半个月了,作个笔记,总结下本身的学习历程。css
独自学习一项新技术,老是不免走很多弯路的,在此推荐下ES的基础教程,对,好好学习官网教程就能够了!html
1) Elasticsearch: 权威指南node
https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.htmlgit
权威中文教程,对于英文很差的同窗,读这个教程能够快速入门。github
2)ElasticsearchReference 官网英文教程spring
https://www.elastic.co/guide/en/elasticsearch/reference/current/index.htmlapi
真正想吃透ES仍是多看英文官网文档,知识点讲的是最全面的。restful
说道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。markdown
1)Java API:建立TransportClient,复杂应用推荐使用app
2)Java REST Client:建立RestClient
3)http restful api:使用最原始的http请求访问
目前暂时使用的第三种,缘由ES业务需求单一,不须要动态建立、删除索引,上手简单,只须要学习es rest语法就能够了。其实后期能够切换到RestClient,它是持久化http连接(使用httpClient还须要一个http链接池),特色如官方所说:
The low-level client’s features include:
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集群的配置,权威教程讲的很粗糙,当时还花了好几天返回测试,最终发现仍是配置文件参数没有吃透。
Minimum Master Nodes
最小主节点数的设置对集群的稳定是很是重要的。该设置对预防脑裂是有帮助的,即一个集群中存在两个master。
这个配置就是告诉Elasticsearch除非有足够可用的master候选节点,不然就不选举master,只有有足够可用的master候选节点才进行选举。
该设置应该始终被配置为有主节点资格的法定节点数,法定节点数:(主节点资格的节点数/2)+1。例如:
一、若是你有10个符合规则的节点数,法定数就是6.
二、若是你有3个候选master,和100个数据节点,法定数就是2,你只要计算那些有主节点资格的节点数就能够了。
三、若是你有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" ]
使用中遇到的坑: