[WIP] Elasticsearchインストール


ここに書いてある通り
[Elasticsearch Reference [1.5] » Setup » Repositories](https://www.elastic.co/guide/en/elasticsearch/reference/current/setup-repositories.html)

### 構築環境

```
$ cat /etc/redhat-release ; uname -a
CentOS release 6.6 (Final)
Linux moon 2.6.32-504.16.2.el6.x86_64 #1 SMP Wed Apr 22 06:48:29 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
```

### javaをインストール

* OracleのJDKをインストールする 

[Java SE Development Kit 7 Downloads](http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html)
上記から最新版をダウンロード
2015/06/04時点では[Java SE Development Kit 7u79]が最新

```text:cmd
# sudo rpm -ihv jdk-7u79-linux-x64.rpm
```

```text:確認
$ java -version
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)
```

### Centos系(yumから)

* レポジトリの取得(ver 1.5)

```text:/etc/yum.repos.d/elasticsearch-1.5.repo
[elasticsearch-1.5]
name=Elasticsearch repository for 1.5.x packages
baseurl=http://packages.elastic.co/elasticsearch/1.5/centos
gpgcheck=1
gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch
enabled=1
```

* インストール

```text:cmd
$ sudo yum install elasticsearch
```



* 起動

```text:cmd
$ sudo service elasticsearch start
```

* 起動確認

```text:cmd
curl -X GET http://localhost:9200/
```

```text:結果
$ curl -X GET http://localhost:9200/
{
  "status" : 200,
  "name" : "Right-Winger",
  "cluster_name" : "elasticsearch",
  "version" : {
    "number" : "1.5.2",
    "build_hash" : "62ff9868b4c8a0c45860bebb259e21980778ab1c",
    "build_timestamp" : "2015-04-27T09:21:06Z",
    "build_snapshot" : false,
    "lucene_version" : "4.10.4"
  },
  "tagline" : "You Know, for Search"
}
```

* 上記正常に起動出来る事が分かったのでサーバ起動時に自動起動設定

```
$ sudo chkconfig elasticsearch on
$ sudo chkconfig elasticsearch --list
```

### 挙動の確認

* データを登録

```
# curl -XPUT http://localhost:9200/mytest/test/1 -d '{ "title":"memo", "text":"ほげほげ" }'

{"_index":"mytest","_type":"test","_id":"1","_version":1,"created":true}
```

* データの確認

```
$ curl -XGET http://localhost:9200/mytest/test/_search -d '{ "query": { "match": { "title":"memo" } } }'

{"took":2,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}
```

* データの削除

```
$ curl -XDELETE http://localhost:9200/mytest/test/1


{"found":false,"_index":"mytest","_type":"test","_id":"1","_version":1}
```

### Elasticsearchの日本語対応(kuromoji)のインストール

* [elastic/elasticsearch-analysis-kuromoji](https://github.com/elastic/elasticsearch-analysis-kuromoji)

2015/06/04時点でes1.5x系はelasticsearch-analysis-kuromoji/2.5.0が対応している

```
$ sudo bin/plugin install elasticsearch/elasticsearch-analysis-kuromoji/2.5.0
```

* 設定の反映のために、esの再起動

```
$ sudo service elasticsearch restart
```

### 挙動の確認(kuromoji-anaralzer経由)

* データの投入

```
$ curl -XPUT http://localhost:9200/kuromoji_sample -d '{ "index": { "analysis": { "tokenizer": { "kuromoji_user_dict" : { "type":"kuromoji_tokenizer" } }, "analyzer": { "analyzer": { "type":"custom", "tokenizer": "kuromoji_user_dict" } } } } }'

{"acknowledged":true}
```

* この新しく作成したインデックスに対して日本語文字列を POST して、形態素解析が有効になっているかどうかを確認 

```
$ curl -XPOST 'http://localhost:9200/kuromoji_sample/_analyze?analyzer=analyzer&pretty' -d '月島もんじゃ'
{
  "tokens" : [ {
    "token" : "月島",
    "start_offset" : 0,
    "end_offset" : 2,
    "type" : "word",
    "position" : 1
  }, {
    "token" : "もんじゃ",
    "start_offset" : 2,
    "end_offset" : 6,
    "type" : "word",
    "position" : 2
  } ]
}
```

* kuromoji を使ったインデックスを ElasticSearch のデフォルトアナライザとして指定し、ElasticSearch を再起動します

```
$ sudo echo "index.analysis.analyzer.default.type: custom"                  >> /etc/elasticsearch/elasticsearch.yml
$ sudo echo "index.analysis.analyzer.default.tokenizer: kuromoji_user_dict" >> /etc/elasticsearch/elasticsearch.yml
$ sudo service elasticsearch restart
```

* 改めて、データの投入

```
$ curl -XPUT http://localhost:9200/kuromoji_sample/test/1 -d '{ "title":"メモ １", "text":"今夜の夕食が楽しみ" }'
$ curl -XPUT http://localhost:9200/kuromoji_sample/test/1 -d '{ "title":"メモ １", "text":"来週の刃牙が楽しみ" }'
{"_index":"kuromoji_sample","_type":"test","_id":"1","_version":2,"created":false}
```

* データの検索

```
$ curl -XGET http://localhost:9200/kuromoji_sample/test/_search -d '{"query":{"match":{"text":"刃牙"}}}'

{"took":12,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":0.16273327,"hits":[{"_index":"kuromoji_sample","_type":"test","_id":"1","_score":0.16273327,"_source":{ "title":"メモ１", "text":"来週の刃牙が楽しみ" }}]}}
```

参考URL
http://dotnsf.blog.jp/archives/1005213909.html







### その他

* Elastic Headの導入
 
[couchbaselabs/elasticsearch-transport-couchbase](https://github.com/couchbaselabs/elasticsearch-transport-couchbase)

ブラウザで見れる用にElastic Headを入れてみる

```
$ sudo service elasticsearch stop
$ sudo bin/plugin -install mobz/elasticsearch-head
$ sudo service elasticsearch start
```

上記が正常にインストール出来た後は、ブラウザよりアクセスしてみる。

```
http://(サーバーのIPアドレス):9200/_plugin/head/
```

![es-head.png](https://qiita-image-store.s3.amazonaws.com/0/25582/d3bcbb50-6748-3ded-678f-d5cafc6841e8.png)