早年,搜索技术是互联网技术的集大成者,涉及自然语言处理(包括分词和模式识别),海量数据处理,分布式,高并发等几个方面。笔者早期曾使用lucene参与老虎地图的搜索模块的开发,现在5年过去了搜索技术更新换代很快,准备重新拾起Lucene,Elastic Search和Solr等现有技术,来对比其异同,同时总结一下技术发展的脉络。
基本准备
- 安装JDK和Elastic自不必多说,目前的版本选择的是JDK 8及Elastic Search 5.5.3。
cd /path/to/you-directory/ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.3.zip unzip elasticsearch-5.5.3.zip cd elasticsearch-5.5.3/ ./bin/elasticsearch # 运行es
- 此时控制台会输出信息,告知你es已经在9200端口正常运行了:
[2018-09-22T18:31:55,132][INFO ][o.e.h.n.Netty4HttpServerTransport] [mH8aEiW] publish_address {127.0.0.1:9200}, bound_addresses {[fe80::1]:9200}, {[::1]:9200}, {127.0.0.1:9200} [2018-09-22T18:31:55,134][INFO ][o.e.n.Node ] [mH8aEiW] started
- 使用浏览器访问localhost:9200,或者使用命令行访问curl localhost:9200,可以见到提示信息
{ "name" : "mH8aEiW", "cluster_name" : "elasticsearch", "cluster_uuid" : "maxykpOgTMG2bUWfRda__Q", "version" : { "number" : "5.5.3", "build_hash" : "9305a5e", "build_date" : "2017-09-07T15:56:59.599Z", "build_snapshot" : false, "lucene_version" : "6.6.0" }, "tagline" : "You Know, for Search" }
基本概念
- Index会索引所有字段。查看当前节点的所有index:
shengl-pro:tmp shengl$ curl -X GET 'http://localhost:9200/_cat/indices?v' health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open weather rF5fi6FPQ-qcJGNbwiLtrQ 5 1 0 0 810b 810
- Index里面的单条记录称为Document(文档),尽量需要使用相同的数据结构。
- Type可以标识Document的分组,在之后的版本中已经废弃了,故而不优先推荐使用。性质不同的数据不应该使用同一个Index来用type标识,type适用于不同的城市(北京的数据和上海的数据)来分组,或者不同的气候(晴天和雨天),他们schema应该相同。
- 查看当前节点的所有分组:
shengl-pro:tmp shengl$ curl 'localhost:9200/_mapping?pretty=true' { "weather" : { "mappings" : { } } }
- 运行elastic search之后,可以通过命令行查看,其使用的web容器为netty.
shengl-pro:elasticsearch-5.5.3-run shengl$ ps -ef | grep netty 501 67290 66245 0 4:57PM ttys001 0:24.07 /usr/bin/java -Xms2g -Xmx2g -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+AlwaysPreTouch -server -Xss1m -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djna.nosys=true -Djdk.io.permissionsUseCanonicalPath=true -Dio.netty.noUnsafe=true -Dio.netty.noKeySetOptimization=true -Dio.netty.recycler.maxCapacityPerThread=0 -Dlog4j.shutdownHookEnabled=false -Dlog4j2.disable.jmx=true -Dlog4j.skipJansi=true -XX:+HeapDumpOnOutOfMemoryError -Des.path.home=/Users/shengl/9-ibm/elasticsearch-5.5.3-run -cp /Users/shengl/9-ibm/elasticsearch-5.5.3-run/lib/* org.elasticsearch.bootstrap.Elasticsearch 501 67376 67136 0 11:27PM ttys003 0:00.01 grep netty
基本结构及配置等说明
文件结构
$ ls -l
-rw-r--r--@ 1 shengl staff 11358 Sep 7 2017 LICENSE.txt
-rw-rw-r--@ 1 shengl staff 194187 Sep 7 2017 NOTICE.txt
-rw-r--r--@ 1 shengl staff 9549 Sep 7 2017 README.textile #有一个简单的使用说明
drwxr-xr-x@ 17 shengl staff 544 Sep 22 18:30 bin #可执行文件的目录,看文件名可以分析基本用途
drwxr-xr-x@ 6 shengl staff 192 Sep 22 18:31 config #配置文件目录
drwxr-xr-x 3 shengl staff 96 Sep 22 18:31 data #数据存储目录
drwxr-xr-x@ 37 shengl staff 1184 Sep 7 2017 lib #依赖库目录
drwxr-xr-x 6 shengl staff 192 Sep 22 18:31 logs #日志目录
drwxr-xr-x@ 13 shengl staff 416 Sep 7 2017 modules #模块
drwxr-xr-x@ 2 shengl staff 64 Sep 7 2017 plugins #第三方插件目录
配置说明
-rw-rw----@ 1 shengl staff 2854 Sep 7 2017 elasticsearch.yml # 基本配置
-rw-rw----@ 1 shengl staff 3064 Sep 7 2017 jvm.options #Jvm参数配置
-rw-rw----@ 1 shengl staff 4456 Sep 7 2017 log4j2.properties #log4j配置
drwxr-xr-x 2 shengl staff 64 Sep 22 18:31 scripts.
配置文件分离
默认目录在$ES_HOME/config, 可以通过ES_PATH_CONF环境变量来指定。即ES_PATH_CONF=/path/to/your/config .bin/elasticsearch
yaml格式的文件可以有两种写法,目前被广泛运用于springboot等其他开源项目的配置中,保持一个统一的配置文件格式很有必要。
path: data: /var/lib/elasticsearch logs: /var/log/elasticsearch
path.data: /var/lib/elasticsearch path.logs: /var/log/elasticsearch前者有更明显的层次结构,后者更不容易出错。 * 引用系统环境变量
node.name: ${HOSTNAME} network.host: ${ES_NETWORK_HOST}
Jvm参数格式说明:
-Xmx2g #无JVM版本限制
8: -Xmx2g #应用指定版本JVM参数
8- : -Xmx2g #应用指定版本及以上的JVM参数
8-9 : -Xmx2g #应用到指定版本之间的JVM参数指定数据目录和日志目录,生产环境下应该与软件分离
path: data: - /mnt/es_data_1 - /mnt/es_data_2 - /mnt/es_data_3 logs: - /var/log/es/
所属集群名称
cluster.name: poi-prod
节点名称,默认为UUID前7字符,可以自定义
node.name: poi-prod-n-1
node.name: ${HOSTNAME} #如果网络架构梳理好了,也可以以主机名作为节点名network.host IP绑定
默认绑定的是[“127.0.0.1”, “[::1]”]回环地址,集群下要服务间通讯,需要绑定一个IPv4或者IPv6地址network.host: 192.168.1.10
http.port: 9200-9300 # 可以指定9200,为区间时会取用第一个可用端口
transport.tcp.port: 9300-9400 # 节点间交互端口,逻辑同上
Discovery Config 节点发现配置
ES中默认采用的节点发现方式是ZEN(基于组播(多播),单播)。应用于生产环境需配置
discovery.zen.ping.unicast.hosts: [“host1”,”host2:port”,”host3[portX-portY]”] 单播模式下,设置具有master资格的节点列表,新加入的节点向这个列表中的节点发送请求来加入集群
discovery.zen.minimum_master_nodes: 1 这个参数控制的是,一个节点需要看到具有master资格的节点的最小数量,然后才能在集群中做操作。官方的推荐值是(N/2)+1,其中N是具有master资格的节点的数量。Jvm heap大小配置, 生产环境一定要配置。
Jvm heap dump path,导出发生异常时的堆信息
-XX:HeapDumpPath=/var/lib/elasticsearch/dump/其他配置
transport.tcp.compress: false 是否压缩tcp传输的数据,默认false http.cors.enabled: true 是否使用http协议对外提供服务,默认true http.max_content_length: 100mb http传输内容的最大容量,默认100mb node.master: true 指定该节点是否可以作为master节点,默认是true。ES集群默认是以第一个节点为master,如果该节点出故障就会重新选举master。 node.data: true 该节点是否存索引数据,默认true。 discover.zen.ping.timeout: 3s 设置集群中自动发现其他节点时ping连接超时时长,默认为3秒。在网络环境较差的情况下,增加这个值,会增加节点等待响应的时间,从一定程度上会减少误判。 discovery.zen.ping.multicast.enabled: false 是否启用多播来发现节点。
Elastic架构说明
TODO
基本操作说明
Index操作
$ curl -X PUT “localhost:9200/weather’
数据操作
新增数据到某个索引
- API通过RESTFUL的PUT方法就区分了 PUT /${indexName}/${indexType}/${indexId}/${op_type}
- id参数可以忽略,会自行分配id
curl -X PUT 'localhost:9200/twitter/user/2/_create' -d ' { "user" : "kimchy", "post_date" : "2009-11-15T14:12:12", "message" : "trying out Elasticsearch" }'
ES 返回
{ "_index": "twitter", "_type": "user", "_id": "1", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "created": true }
Routing
curl -X POST "localhost:9200/twitter/user?routing=kimchy" -H 'Content-Type: application/json' -d' { "user" : "kimchy", "post_date" : "2009-11-15T14:12:12", "message" : "trying out Elasticsearch" } '
GET API
GET /${indexName}/${indexType}/${indexId} # 通过RESTFUL的GET区分了API
_source=false指定不显示源代码,如果仅需要._source中的一个或两个字段,则可以使用_source_include&_source_exclude来指定包含或者不包含某个字段。
curl -X GET "localhost:9200/twitter/user/1" {"_index":"twitter","_type":"user","_id":"1","_version":1,"found":true,"_source": { "user" : "kimchy", "post_date" : "2009-11-15T14:12:12", "message" : "trying out Elasticsearch" }}
中文分词
安装ik分词器,与es使用相同的版本号,安装后重启es即可加载插件。
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.5.3/elasticsearch-analysis-ik-5.5.3.zip
测试分词结果
curl -G ‘localhost:9200/_analyze?analyzer=ik&pretty=true’ –data-urlencode “text=北京市海淀区海淀剧院大门口”
elastic search API详解
数据迁移
方案1 查询并且导出数据
方案2 拷贝ES物理目录/文件
方案3 ES快照数据备份和恢复
方案4 使用logstash导入导出数据
版权声明
本文标题:64-ElasticSearch简单教程
文章作者:盛领
发布时间:2018年09月23日 - 15:29:55
原始链接:http://blog.xiaoyuyu.net/post/6051b4a3.html
许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。
如您有任何商业合作或者授权方面的协商,请给我留言:sunsetxiao@126.com
