Elastic search 2.0 安装与基本使用

之前整理官方文档的安装来自1.5.x,目前已发布到2.0正式版,重新整理一下。

安装2.0版本最好使用java8版本。

第一步安装java8

本机UBUBTU12.04,APT方式添加软件源,这个源比较方便能装jdk7、8、9

$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get update
$ sudo apt-get install oracle-java8-installer
$ java -version

官方提供的下载地址。

cd /data
wget https://download.elasticsearch.org/elasticsearch/release/org/elasticsearch/distribution/zip/elasticsearch/2.0.0/elasticsearch-2.0.0.zip
unzip elasticsearch-2.0.0.zip
cd elastic search-2.0.0/bin
ll
-rwxr-xr-x 1 ubuntu ubuntu 5331 Oct 21 13:32 elasticsearch*
-rwxr-xr-x 1 ubuntu ubuntu 2814 Oct 21 08:41 elasticsearch.in.sh*
-rwxr-xr-x 1 ubuntu ubuntu 2847 Oct 21 13:32 plugin*
./elasticsearch #启动( -d 后台启动。默认绑定127.0.0.1端口,更改IP启动参数-Des.network.host=10.0.0.4)
[2015-11-23 18:59:51,225][INFO ][node                     ] [Gorgilla] version[2.0.0], pid[3267], build[de54438/2015-10-22T08:09:48Z]
[2015-11-23 18:59:51,225][INFO ][node                     ] [Gorgilla] initializing ...
[2015-11-23 18:59:51,273][INFO ][plugins                  ] [Gorgilla] loaded [], sites []
[2015-11-23 18:59:51,346][INFO ][env                      ] [Gorgilla] using [1] data paths, mounts [[/data (/dev/vdb)]], net usable_space [18.4gb], net total_space [19.5gb], spins? [possibly], types [ext4]
[2015-11-23 18:59:52,776][INFO ][node                     ] [Gorgilla] initialized
[2015-11-23 18:59:52,776][INFO ][node                     ] [Gorgilla] starting ...
[2015-11-23 18:59:52,836][INFO ][transport                ] [Gorgilla] publish_address {127.0.0.1:9300}, bound_addresses {127.0.0.1:9300}
[2015-11-23 18:59:52,845][INFO ][discovery                ] [Gorgilla] elasticsearch/SHfdJfUeRPK8wf2C417XbQ
[2015-11-23 18:59:55,882][INFO ][cluster.service          ] [Gorgilla] new_master {Gorgilla}{SHfdJfUeRPK8wf2C417XbQ}{127.0.0.1}{127.0.0.1:9300}, reason: zen-disco-join(elected_as_master, [0] joins received)
[2015-11-23 18:59:55,924][INFO ][gateway                  ] [Gorgilla] recovered [0] indices into cluster_state
[2015-11-23 18:59:55,931][INFO ][http                     ] [Gorgilla] publish_address {127.0.0.1:9200}, bound_addresses {127.0.0.1:9200}
[2015-11-23 18:59:55,931][INFO ][node                     ] [Gorgilla] started
#看到以上log表示启动正常。

或者使用APT安装

wget -qO - https://packages.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
echo "deb http://packages.elastic.co/elasticsearch/2.x/debian stable main" | sudo tee -a /etc/apt/sources.list.d/elasticsearch-2.x.list
sudo apt-get update && sudo apt-get install elastic search
sudo update-rc.d elasticsearch defaults 95 10 
sudo /bin/systemctl daemon-reload
sudo /bin/systemctl enable elasticsearch.service
然后使用 service elasticsearch start 启动服务。以下内容将使用自定义安装的方式。apt安装的位置请注意
/usr/share/elasticsearch/plugins #插件目录
/etc/elasticsearch/elasticsearch.yml #主配置文件。

第二步,安装中文分词插件ik

cd /data/elasticsearch-2.0.0/plugins/ik #不存在的文件夹创建

从源码编译、或下载打包好的文件

源码方式:

1.编译

去github下载该项目代码。https://github.com/medcl/elasticsearch-analysis-ik

man package
# 打包,大约15-20分钟

复制并解压 target/releases/elasticsearch-analysis-ik-{version}.zip到/data/elasticsearch-2.0.0/plugins/ik 路径下

2.配置文件:

将解压出来的配置文件放到es的config目录下面的ik文件夹内。

3.重启es服务器。

我也打包了一份es2.0的ik。不想编译的可以直接下载

cd /data/elasticsearch-2.0.0/plugins/ik
wget http://www.gaonengfun.com/attach/elasticsearch-analysis-ik-1.5.0.zip
unzip elasticsearch-analysis-ik-1.5.0.zip

将解压出来的配置文件放到es的config目录下面的ik文件夹内。

最后重启服务器

第三步,测试分词插件。

1、创建索引
curl -XPUT http://127.0.0.1:9200/index

2、创建映射
curl -XPOST http://127.0.0.1:9200/index/fulltext/_mapping -d'
{
    "fulltext": {
             "_all": {
            "analyzer": "ik_max_word",
            "search_analyzer": "ik_max_word",
            "term_vector": "no",
            "store": "false"
        },
        "properties": {
            "content": {
                "type": "string",
                "store": "no",
                "term_vector": "with_positions_offsets",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_max_word",
                "include_in_all": "true",
                "boost": 8
            }
        }
    }
}'
3、测试分词
http://127.0.0.1:9200/index/_analyze?analyzer=ik&text=中国人民自古以来

{
    "tokens": [
        {
            "token": "中国人民",
            "start_offset": 0,
            "end_offset": 4,
            "type": "CN_WORD",
            "position": 0
        },
        {
            "token": "中国人",
            "start_offset": 0,
            "end_offset": 3,
            "type": "CN_WORD",
            "position": 1
        },
        {
            "token": "中国",
            "start_offset": 0,
            "end_offset": 2,
            "type": "CN_WORD",
            "position": 2
        },
        {
            "token": "国人",
            "start_offset": 1,
            "end_offset": 3,
            "type": "CN_WORD",
            "position": 3
        },
        {
            "token": "人民",
            "start_offset": 2,
            "end_offset": 4,
            "type": "CN_WORD",
            "position": 4
        },
        {
            "token": "人",
            "start_offset": 2,
            "end_offset": 3,
            "type": "CN_WORD",
            "position": 5
        },
        {
            "token": "民",
            "start_offset": 3,
            "end_offset": 4,
            "type": "CN_CHAR",
            "position": 6
        },
        {
            "token": "自古以来",
            "start_offset": 4,
            "end_offset": 8,
            "type": "CN_WORD",
            "position": 7
        },
        {
            "token": "自古",
            "start_offset": 4,
            "end_offset": 6,
            "type": "CN_WORD",
            "position": 8
        },
        {
            "token": "以来",
            "start_offset": 6,
            "end_offset": 8,
            "type": "CN_WORD",
            "position": 9
        }
    ]
}

喔,对了。2。0版本已经不推荐在配置文件里加一行配置从而改变默认分词器的配置了。比如类似 index.analysis.analyzer.default.type: ik 这样的配置。

如果想要对索引里的字段使用ik分词器,需要在映射中加上参数 analyzer: ik_max_word。

最后感谢M大神在群里的帮助。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注