码迷,mamicode.com
首页 > 其他好文 > 详细

ElasticSearch集群入门

时间:2016-06-21 07:24:03      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:

一、下载与安装

Elasticsearch 依赖 java,在安装 ES 之前首先要配好 java,这个默认我们的电 脑已经完成。
首先从 elasticsearch官网下载安装包,我们是 linux 系统,下载 tar 包比较方便。当前版本为2.3.3,下载地址:Elasticsearch 2.3.3下载
.下载完成之后解压tar文件:

tar -zxvf elasticsearch-2.3.3.tar.gz 

二、运行ElasticSearch

启动ElasticSearch命令:

./elasticsearch-2.3.3/bin/elasticsearch

如果一切顺利,会收到类似于如下的信息:

[2016-06-18 15:07:50,106][INFO ][node                     ] [Maynard Tiboldt] version[2.3.3], pid[7390], build[218bdf1/2016-05-17T15:40:04Z]
[2016-06-18 15:07:50,106][INFO ][node                     ] [Maynard Tiboldt] initializing ...
[2016-06-18 15:07:50,590][INFO ][plugins                  ] [Maynard Tiboldt] modules [reindex, lang-expression, lang-groovy], plugins [], sites []
[2016-06-18 15:07:50,609][INFO ][env                      ] [Maynard Tiboldt] using [1] data paths, mounts [[/ (/dev/disk1)]], net usable_space [85.6gb], net total_space [111.8gb], spins? [unknown], types [hfs]
[2016-06-18 15:07:50,609][INFO ][env                      ] [Maynard Tiboldt] heap size [990.7mb], compressed ordinary object pointers [true]
[2016-06-18 15:07:50,610][WARN ][env                      ] [Maynard Tiboldt] max file descriptors [10240] for elasticsearch process likely too low, consider increasing to at least [65536]
[2016-06-18 15:07:52,323][INFO ][node                     ] [Maynard Tiboldt] initialized
[2016-06-18 15:07:52,323][INFO ][node                     ] [Maynard Tiboldt] starting ...
[2016-06-18 15:07:52,401][INFO ][transport                ] [Maynard Tiboldt] publish_address {127.0.0.1:9300}, bound_addresses {[fe80::1]:9300}, {[::1]:9300}, {127.0.0.1:9300}
[2016-06-18 15:07:52,406][INFO ][discovery                ] [Maynard Tiboldt] elasticsearch/OawW12ZERyO3CuQrt7SmFQ
[2016-06-18 15:07:55,445][INFO ][cluster.service          ] [Maynard Tiboldt] new_master {Maynard Tiboldt}{OawW12ZERyO3CuQrt7SmFQ}{127.0.0.1}{127.0.0.1:9300}, reason: zen-disco-join(elected_as_master, [0] joins received)
[2016-06-18 15:07:55,456][INFO ][http                     ] [Maynard Tiboldt] publish_address {127.0.0.1:9200}, bound_addresses {[fe80::1]:9200}, {[::1]:9200}, {127.0.0.1:9200}
[2016-06-18 15:07:55,457][INFO ][node                     ] [Maynard Tiboldt] started
[2016-06-18 15:07:55,476][INFO ][gateway                  ] [Maynard Tiboldt] recovered [0] indices into cluster_state

这样就运行起来了一个节点。(可以多开几个终端,运行起来集群名相同的节点都 在一个集群中).
注意一下有 http 标记的那一行,它提供了有关 HTTP 地址(127.0.0.1)和 端口(9200)的信息,通过这个地址和端口我们就可以访问我们的节点了。默认情况下, Elasticsearch 使用 9200 来提供对其 REST API 的访问。
访问http://127.0.0.1:9200/,浏览器会输出如下信息:

{
  "name" : "Obliterator",
  "cluster_name" : "elasticsearch",
  "version" : {
    "number" : "2.3.3",
    "build_hash" : "218bdf10790eef486ff2c41a3df5cfa32dadcfde",
    "build_timestamp" : "2016-05-17T15:40:04Z",
    "build_snapshot" : false,
    "lucene_version" : "5.5.0"
  },
  "tagline" : "You Know, for Search"
}

技术分享

如果想通过服务器ip访问es,打开elasticsearch-2.3.3/config/elasticsearch.yml,找到Network部分:


# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
# network.host: 192.168.0.1
#
# Set a custom port for HTTP:
#
# http.port: 9200
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-network.html>
#

network.host: 192.168.0.1前的注释去掉并修改为network.host: 0.0.0.0
重启es上面返回的信息中 http 部分也会变成真实本机地址,可以通过本季真实ip:9200 访问es。

三、关闭ElasticSearch

需要对ES节点进行重新启动或正常关机的时候,有三种方法可以关闭ES:

  1. 在控制台中,使用CTRL+C组合键.
  2. 通过发送TERM信号终止服务器进程.
  3. 使用REST APIcurl -XPOST ‘http://localhost:9200/_shutdown‘.

四、插件安装

4.1安装head 插件

安装命令:

./elasticsearch-2.3.3/bin/plugin install mobz/elasticsearch-head

安装好后,在浏览器输入地址:http://localhost:9200/_plugin/head/ 即可调用 head 插件 查看集群状态、节点信息、做查询等等。

4.2安装IK分词器

(a).首先使用git clone命令下载IK分词器源码

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

也可以直接访问github地址(https://github.com/medcl/elasticsearch-analysis-ik)点击右侧Clone or download按钮,然后Download ZIP直接下载.

(b.)解压下载的elasticsearch-analysis-ik-master.zip.

unzip elasticsearch-analysis-ik-master.zip

(c.)使用maven打包
确保系统已经安装maven,使用mvn -version命令查看是否已经安装maven.如果没有安装,可以根据系统选择安装方法,比如mac OS系统可以使用brew install maven命令完成安装.
进入ik分词器的下载目录,运行命令:

mvn package

打包完成以后可以看到根目录下多出一个target文件夹.
(d.) 配置Ik插件
elasticsearch-2.3.3/plugins/目录下新建名为ik的文件夹.把elasticsearch-analysis-ik-master/target/releases
/elasticsearch-analysis-ik-1.9.3.zip解压,把解压后的所有文件拷贝到elasticsearch-2.3.3/plugins/ik/目录下.
重新启动es,如果配置正确,不会有异常信息输出。
(e.)ik 分词测试
1.首先创建一个索引用于测试:

curl -XPUT localhost:9200/index

2.为索引index创建mapping:

curl -XPOST http://localhost:9200/index/fulltext/_mapping -d‘
{
    "fulltext": {
             "_all": {
            "analyzer": "ik"
        },
        "properties": {
            "content": {
                "type" : "string",
                "boost" : 8.0,
                "term_vector" : "with_positions_offsets",
                "analyzer" : "ik",
                "include_in_all" : true
            }
        }
    }
}

3.测试:

curl ‘http://localhost:9200/index/_analyze?analyzer=ik&pretty=true‘ -d ‘
{
"text":"中国有13亿人口"
}‘

显示结果如下:

{
  "tokens" : [ {
    "token" : "中国",
    "start_offset" : 0,
    "end_offset" : 2,
    "type" : "CN_WORD",
    "position" : 0
  }, {
    "token" : "国有",
    "start_offset" : 1,
    "end_offset" : 3,
    "type" : "CN_WORD",
    "position" : 1
  }, {
    "token" : "13",
    "start_offset" : 3,
    "end_offset" : 5,
    "type" : "ARABIC",
    "position" : 2
  }, {
    "token" : "亿",
    "start_offset" : 5,
    "end_offset" : 6,
    "type" : "CN_WORD",
    "position" : 3
  }, {
    "token" : "人口",
    "start_offset" : 6,
    "end_offset" : 8,
    "type" : "CN_WORD",
    "position" : 4
  } ]
}

五、文档的CRUD

5.1索引、类型、文档、字段

  • 索引是ElasticSearch存放数据的地方,可以理解为关系型数据库中的一个数据库。
  • 类型用于区分同一个索引下不同的数据类型,相当于关系型数据库中的表
  • 文档是ElasticSearch中存储的实体,类比关系型数据库,每个文档相当于数据库表中的一行数据。
  • 文档由字段组成,相当于关系数据库中列的属性,不同的是ES的不同文档可以具有不同的字段集合。
    对比关系型数据库:
Relational DB -> Databases -> Tables -> Rows -> Columns
Elasticsearch -> Indices   -> Types  -> Documents -> Fields

5.2 创建文档

以博客内容管理为例,索引名为blog,类型为article,新加一个文档:

curl -XPUT http://localhost:9200/blog/article/1 -d ‘
{
      "id": "1",
      "title": "New version of Elasticsearch released!",
      "content": "Version 1.0 released today!",
      "priority": 10,
      "tags": ["announce", "elasticsearch", "release"]
}‘

5.3检索文档

http://localhost:9200/blog/article/1?pretty

5.4更新文档

curl -XPOST http://localhost:9200/blog/article/1/_update -d ‘{
  "script": "ctx._source.content = \"new content\""
}‘

5.5删除文档

curl -XDELETE http://localhost:9200/blog/article/1 

ElasticSearch集群入门

标签:

原文地址:http://blog.csdn.net/napoay/article/details/51705902

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!