elasticsearch2.2.0安装

概念

名称
说明
cluster  代表一个集群,集群中有多个节点,其中有一个为主节点,这个主节点是可以通过选举产生的,主从节点是对于集群内部来说的。es的一个概念就是去中心化,字面上理解就是无中心节点,这是对于集群外部来说的,因为从外部来看es集群,在逻辑上是个整体,你与任何一个节点的通信和与整个es集群通信是等价的。
shards 代表索引分片,es可以把一个完整的索引分成多个分片,这样的好处是可以把一个大的索引拆分成多个,分布到不同的节点上。构成分布式搜索。分片的数量只能在索引创建前指定,并且索引创建后不能更改。
replicas  代表索引副本,es可以设置多个索引的副本,副本的作用一是提高系统的容错性,当个某个节点某个分片损坏或丢失时可以从副本中恢复。二是提高es的查询效率,es会自动对搜索请求进行负载均衡。
recovery 

代表数据恢复或叫数据重新分布,es在有节点加入或退出时会根据机器的负载对索引分片进行重新分配,挂掉的节点重新启动时也会进行数据恢复。

river 

代表es的一个数据源,也是其它存储方式(如:数据库)同步数据到es的一个方法。它是以插件方式存在的一个es服务,通过读取river中的数据并把它索引到es中,官方的river有couchDB的,RabbitMQ的,Twitter的,Wikipedia的。

gateway  代表es索引的持久化存储方式,es默认是先把索引存放到内存中,当内存满了时再持久化到硬盘。当这个es集群关闭再重新启动时就会从gateway中读取索引数据。es支持多种类型的gateway,有本地文件系统(默认),分布式文件系统,Hadoop的HDFS和amazon的s3云存储服务。
discovery.zen 代表es的自动发现节点机制,es是一个基于p2p的系统,它先通过广播寻找存在的节点,再通过多播协议来进行节点之间的通信,同时也支持点对点的交互。
Transport 代表es内部节点或集群与客户端的交互方式,默认内部是使用tcp协议进行交互,同时它支持http协议(json格式)、thrift、servlet、memcached、zeroMQ等的传输协议(通过插件方式集成)。
index 逻辑单位,可以理解为关系型数据库中的数据库单元
type 一个index可以包含多个type,可以理解为关系型数据库中的表单元

安装

环境

mkdir ~/app                           #es的bin目录
mkdir ~/data/es/logs/ -p        #es的log目录
mkdir ~/data/es/data/ -p       #es的data目录
需要端口:
9200、9300 
配置java运行时

安装ES

1. 下载最新包并解压到~/app/,这里的配置是elasticsearch-2.2.0
2. 配置 vi ~/app/elasticsearch-2.2.0/config/elasticsearch.yml 
path.data: /home/elasticsearch/data/es/data
path.logs: /home/elasticsearch/data/es/logs
cluster.name: uc-edu
bootstrap.mlockall: true
discovery.zen.minimum_master_nodes: 1
http.port: 9200
network.host: 0.0.0.0
discovery.zen.ping.unicast.hosts: ["10.20.33.78"]           #"10.20.33.78"是你master主机


其它配置说明

 

安装插件

安装中文分词插件

#官网https://github.com/medcl/elasticsearch-analysis-ik
a. 下载https://codeload.github.com/medcl/elasticsearch-analysis-ik/zip/master
b. 解压并进入elasticsearch-analysis-ik-master
c. 打包 mvn package
d. 服务器上创建~/app/elasticsearch-2.2.0/plugins/ik
e. 复制elasticsearch-analysis-ik-master\target\releases\elasticsearch-analysis-ik-1.8.0.zip 到 ~/app/elasticsearch-2.2.0/plugins/ik
f. unzip elasticsearch-analysis-ik-1.8.0.zip

安装数据库importer

cd ~/app/
wget http://xbib.org/repository/org/xbib/elasticsearch/importer/elasticsearch-jdbc/2.2.0.0/elasticsearch-jdbc-2.2.0.0-dist.zip
unzip  elasticsearch-jdbc-2.2.0.0-dist.zip
rm  elasticsearch-jdbc-2.2.0.0-dist.zip

启动

启动

~/app/elasticsearch-2.2.0/bin/elasticsearch -d

初始化数据库

初始化devindex的solution表

curl -XPUT http://localhost:9200/devindex
curl -XPUT 'http://localhost:9200/devindex/solution/_mapping'-d '
{
    "solution": {
        "_all": {
            "analyzer":"ik_max_word",
            "search_analyzer":"ik_max_word",
            "term_vector":"no",
            "store":"false"
        },
        "properties": {
            "docid": {
                "type":"string",
                "store":"yes"
            },
            "btype": {
                 "type":"string",
                 "index":"not_analyzed"
            },
            "title": {
                "type":"string",
                "store":"yes",
                "term_vector":"with_positions_offsets",
                "analyzer":"ik_max_word",
                "search_analyzer":"ik_max_word",
                "include_in_all":"true",
                "boost": 8
            },
            "create_time": {
                "type":"date",
                "store":"yes",
                "format":"dateOptionalTime"
            },
            "update_time": {
                "type":"date",
                "store":"yes",
                "format":"dateOptionalTime"
             },           
             "difficulty": {
                 "type":"double"
             },           
             "frequency": {
                 "type":"integer"
             },           
             "hotness": {
                 "type":"string"
              },           
             "status": {
                 "type":"integer",
                 "index":"not_analyzed"
            },
            "source": {
                "type":"string",
                "store":"yes",
                "term_vector":"with_positions_offsets",
                "analyzer":"ik_max_word",
                "search_analyzer":"ik_max_word",
                "include_in_all":"true",
                "boost": 8
            }
        }
    }
}'

分词方式有ik_smart和ik_max_word。ik_max_word 会将文本做最细粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,中华人民,中华,华人,人民共和国,人民,人,民,共和国,共和,和,国国,国歌”,会穷尽各种可能的组合;而 ik_smart 会做最粗粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,国歌”

导入数据库数据

cd ~/app/elasticsearch-jdbc-2.2.0.0/bin

配置脚本solution.sh

#!/bin/sh
DIR="$( cd "$(dirname"${BASH_SOURCE[0]}" )" && pwd )"
bin=${DIR}/../bin
lib=${DIR}/../lib
echo'
{
    "type": "jdbc",
    "jdbc": {
        "elasticsearch": {
             "cluster": "uc-edu",
             "host": "localhost",
             "port": 9300
        },
        "url": "jdbc:mysql://127.0.0.1:3306/test",
        "user": "test",
        "password": "test",
        "sql": "select id as _id,type as btype,source,create_time,update_time,difficulty,frequency,hotness,status,title from solution",
        "index": "devindex",
        "type": "solution",
        "index_settings": {
            "index": {
                "number_of_shards": 1
            }
        }
    }
}
' | java \
    -cp"${lib}/*" \
    -Dlog4j.configurationFile=${bin}/log4j2.xml \
    org.xbib.tools.Runner \
    org.xbib.tools.JDBCImporter

执行 sh solution.sh

查询

简单查询

curl -XPOST http://localhost:9200/devindex/solution/_search -d'
{
    "query": { "match": { "title": "一艘轮船和一艘快艇沿相同路线从甲港出发到乙港,行驶过程随时间变化的图象如图所示,下列结论错误的是"}},
    "size": 5,
    "from": 0,
    "highlight": {
        "pre_tags": ["<tag1>","<tag2>"],
        "post_tags": ["</tag1>","</tag2>"],
        "fields": {
            "title": {}
        }
    }
}
'

组合查询

支持should,must,must_not的组合,每种组合还支持多个字段的数组

查询方法包括:term,wildcard,prefix,range,fuzzy,query_string,text,missing

curl -XPOST http://localhost:9200/devindex/solution/_search -d'
{
"query": {
    "bool": {
        "must":[
            {"match": { "source":"广州"}},
            {"term": { "status":"-1"}},           
            {"match": { "title":"下列结论错误的是"}}
        ],
        "should":[
              {"term": { "ptype":"选择题"}},
            {"term": { "ptype":"填空题"}}
        ],
        "must_not": {"match": { "source":"深圳"}} 
    }
  },
    "size": 15,
    "from": 0,
    "highlight": {
        "pre_tags": ["<tag1>","<tag2>"],
        "post_tags": ["</tag1>","</tag2>"],
        "fields": {
            "title": {}
        }
    },
     "sort": [{ "difficulty": { "order":"desc"} }, { "hotness": { "order":"desc"} }]
}
'

相关文档

https://www.elastic.co/downloads/elasticsearch

https://www.elastic.co/guide/en/elasticsearch/reference/current/docs.html

https://github.com/jprante/elasticsearch-jdbc

https://github.com/medcl/elasticsearch-analysis-ik

 

其它配置说明

1. 集群名称,默认为elasticsearch:

cluster.name: elasticsearch

2. 节点名称,es启动时会自动创建节点名称,但你也可进行配置:

node.name: "Franz Kafka"

3. 是否作为主节点,每个节点都可以被配置成为主节点,默认值为true:

node.master: true

4. 是否存储数据,即存储索引片段,默认值为true:

node.data: true

master和data同时配置会产生一些奇异的效果:

1) 当master为false,而data为true时,会对该节点产生严重负荷;

2) 当master为true,而data为false时,该节点作为一个协调者;

3) 当master为false,data也为false时,该节点就变成了一个负载均衡器。

你可以通过连接http://localhost:9200/_cluster/health或者http://localhost:9200/_cluster/nodes,或者使用插件http://github.com/lukas-vlcek/bigdesk或http://mobz.github.com/elasticsearch-head来查看集群状态。

5. 每个节点都可以定义一些与之关联的通用属性,用于后期集群进行碎片分配时的过滤:

node.rack: rack314

6. 默认情况下,多个节点可以在同一个安装路径启动,如果你想让你的es只启动一个节点,可以进行如下设置:

node.max_local_storage_nodes: 1

7. 设置一个索引的碎片数量,默认值为5:

index.number_of_shards: 5

8. 设置一个索引可被复制的数量,默认值为1:

index.number_of_replicas: 1

当你想要禁用公布式时,你可以进行如下设置:

index.number_of_shards: 1

index.number_of_replicas: 0

这两个属性的设置直接影响集群中索引和搜索操作的执行。假设你有足够的机器来持有碎片和复制品,那么可以按如下规则设置这两个值:

1) 拥有更多的碎片可以提升索引执行能力,并允许通过机器分发一个大型的索引;

2) 拥有更多的复制器能够提升搜索执行能力以及集群能力。

对于一个索引来说,number_of_shards只能设置一次,而number_of_replicas可以使用索引更新设置API在任何时候被增加或者减少。

ElasticSearch关注加载均衡、迁移、从节点聚集结果等等。可以尝试多种设计来完成这些功能。

可以连接http://localhost:9200/A/_status来检测索引的状态。

9. 配置文件所在的位置,即elasticsearch.yml和logging.yml所在的位置:

path.conf: /path/to/conf

10. 分配给当前节点的索引数据所在的位置:

path.data: /path/to/data

可以可选择的包含一个以上的位置,使得数据在文件级别跨越位置,这样在创建时就有更多的自由路径,如:

path.data: /path/to/data1,/path/to/data2

11. 临时文件位置:

path.work: /path/to/work

12. 日志文件所在位置:

path.logs: /path/to/logs

13. 插件安装位置:

path.plugins: /path/to/plugins

14. 插件托管位置,若列表中的某一个插件未安装,则节点无法启动:

plugin.mandatory: mapper-attachments,lang-groovy

15. JVM开始交换时,ElasticSearch表现并不好:你需要保障JVM不进行交换,可以将bootstrap.mlockall设置为true禁止交换:

bootstrap.mlockall: true

请确保ES_MIN_MEM和ES_MAX_MEM的值是一样的,并且能够为ElasticSearch分配足够的内在,并为系统操作保留足够的内存。

16. 默认情况下,ElasticSearch使用0.0.0.0地址,并为http传输开启9200-9300端口,为节点到节点的通信开启9300-9400端口,也可以自行设置IP地址:

network.bind_host: 192.168.0.1

17. publish_host设置其他节点连接此节点的地址,如果不设置的话,则自动获取,publish_host的地址必须为真实地址:

network.publish_host: 192.168.0.1

18. bind_host和publish_host可以一起设置:

network.host: 192.168.0.1

19. 可以定制该节点与其他节点交互的端口:

transport.tcp.port: 9300

20. 节点间交互时,可以设置是否压缩,转为为不压缩:

transport.tcp.compress: true

21. 可以为Http传输监听定制端口:

http.port: 9200

22. 设置内容的最大长度:

http.max_content_length: 100mb

23. 禁止HTTP

http.enabled: false

24. 网关允许在所有集群重启后持有集群状态,集群状态的变更都会被保存下来,当第一次启用集群时,可以从网关中读取到状态,默认网关类型(也是推荐的)是local:

gateway.type: local

25. 允许在N个节点启动后恢复过程:

gateway.recover_after_nodes: 1

26. 设置初始化恢复过程的超时时间:

gateway.recover_after_time: 5m

27. 设置该集群中可存在的节点上限:

gateway.expected_nodes: 2

28. 设置一个节点的并发数量,有两种情况,一种是在初始复苏过程中:

cluster.routing.allocation.node_initial_primaries_recoveries: 4

另一种是在添加、删除节点及调整时:

cluster.routing.allocation.node_concurrent_recoveries: 2

29. 设置复苏时的吞吐量,默认情况下是无限的:

indices.recovery.max_size_per_sec: 0

30. 设置从对等节点恢复片段时打开的流的数量上限:

indices.recovery.concurrent_streams: 5

31. 设置一个集群中主节点的数量,当多于三个节点时,该值可在2-4之间:

discovery.zen.minimum_master_nodes: 1

32. 设置ping其他节点时的超时时间,网络比较慢时可将该值设大:

discovery.zen.ping.timeout: 3s

http://elasticsearch.org/guide/reference/modules/discovery/zen.html上有更多关于discovery的设置。

33. 禁止当前节点发现多个集群节点,默认值为true:

discovery.zen.ping.multicast.enabled: false

34. 设置新节点被启动时能够发现的主节点列表(主要用于不同网段机器连接):

discovery.zen.ping.unicast.hosts: ["host1", "host2:port", "host3[portX-portY]"]

35.设置是否可以通过正则或者_all删除或者关闭索引

action.destructive_requires_name 默认false 允许 可设置true不允许

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值