在EC2实例上安装elasticsearch

安装Java

$ sudo yum install java
$ java -version
java version "1.7.0_79"
OpenJDK Runtime Environment (amzn-2.5.5.1.59.amzn1-x86_64 u79-b14)
OpenJDK 64-Bit Server VM (build 24.79-b02, mixed mode)

安装Elasticsearch核心

请在此处查看最新版本
在这里安装Elasticsearch 1.6.0

$ wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.6.0.tar.gz -O elasticsearch.tar.gz
$ tar -xf elasticsearch.tar.gz
$ sudo mv elasticsearch-* /usr/local/share/elasticsearch
$ sudo chown -R root:root /usr/local/share/elasticsearch/
$ vim elasticsearch/config/elasticsearch.yml

// 下記のコメントアウトを外す
http.port: 9200

安装启动脚本

$ curl -L http://github.com/elasticsearch/elasticsearch-servicewrapper/tarball/master | tar -xz
$ sudo mv *servicewrapper*/service /usr/local/share/elasticsearch/bin/

$ sudo /usr/local/share/elasticsearch/bin/service/elasticsearch install
Detected Linux:
Installing the Elasticsearch daemon..

开动

$ sudo service elasticsearch start
Starting Elasticsearch...
Waiting for Elasticsearch......................
WARNING: Elasticsearch may have failed to start.

错误

$ sudo tail -f /var/log/messages
Jul  1 11:30:06 dev02 elasticsearch[31178]: Unable to write to the configured log file: /usr/local/share/elasticsearch/logs/service.log (No such file or directory)#012  Falling back to the default file in the current working directory: wrapper.log

被告知没有日志写入目标地点

$ sudo mkdir -p /usr/local/share/elasticsearch/logs
$ sudo touch /usr/local/share/elasticsearch/logs/service.log

再次启动

$ sudo service elasticsearch start
Starting Elasticsearch...
Waiting for Elasticsearch......................
WARNING: Elasticsearch may have failed to start.

仍有错误

$ tail -f /usr/local/share/elasticsearch/logs/service.log

STATUS | wrapper  | 2015/07/01 11:38:14 | Launching a JVM...
ERROR  | wrapper  | 2015/07/01 11:38:14 | JVM exited while loading the application.
INFO   | jvm 5    | 2015/07/01 11:38:14 | OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000bad30000, 986513408, 0) failed; error='Cannot allocate memory' (errno=12)
INFO   | jvm 5    | 2015/07/01 11:38:14 | #
INFO   | jvm 5    | 2015/07/01 11:38:14 | # There is insufficient memory for the Java Runtime Environment to continue.
INFO   | jvm 5    | 2015/07/01 11:38:14 | # Native memory allocation (malloc) failed to allocate 986513408 bytes for committing reserved memory.
INFO   | jvm 5    | 2015/07/01 11:38:14 | # An error report file with more information is saved as:
INFO   | jvm 5    | 2015/07/01 11:38:14 | # /tmp/jvm-31983/hs_error.log
FATAL  | wrapper  | 2015/07/01 11:38:15 | There were 5 failed launches in a row, each lasting less than 300 seconds.  Giving up.
FATAL  | wrapper  | 2015/07/01 11:38:15 |   There may be a configuration problem: please check the logs.
STATUS | wrapper  | 2015/07/01 11:38:15 | <-- Wrapper Stopped

无法分配内存
可能是因为服务器不够强大…
尝试降低使用的堆大小

$ sudo vim /usr/local/share/elasticsearch/bin/service/elasticsearch.conf

#set.default.ES_HEAP_SIZE=1024
set.default.ES_HEAP_SIZE=512

重新启动

$ sudo service elasticsearch start
Starting Elasticsearch...
Waiting for Elasticsearch......
running: PID:32081
$ sudo lsof -i -nP | grep 9200
java      32083   root   96u  IPv6 6352906      0t0  TCP *:9200 (LISTEN)

9200端口已被激活并处于监听状态。

我会随意输入一些测试数据。

$ curl -XPUT http://localhost:9200/mytest/test/1 -d '
{
    "title" : "hogehoge",
    "text"  : "fugafuga"
}'
{"_index":"mytest","_type":"test","_id":"1","_version":2,"created":false}[

进行搜索

$ curl -XGET 'localhost:9200/mytest/test/1?pretty=true'
{
  "_index" : "mytest",
  "_type" : "test",
  "_id" : "1",
  "_version" : 2,
  "found" : true,
  "_source":
{
    "title" : "hogehoge",
    "text"  : "fugafuga"
}
}

好的

安装kuromoji插件

为了进行日本语形态素解析的插件
版本信息在这里
由于elasticsearch是1.6.0,所以需要安装kuromoji-2.6.0。

$ sudo /usr/local/share/elasticsearch/bin/plugin --install elasticsearch/elasticsearch-analysis-kuromoji/2.6.0

$ sudo service elasticsearch restart
Stopping Elasticsearch...
Stopped Elasticsearch.
Starting Elasticsearch...
Waiting for Elasticsearch......
running: PID:32331

对于kuromoji的操作进行确认。

$ curl -XPUT 'http://localhost:9200/kuromoji_test/' -d'
{
    "index":{
        "analysis":{
            "tokenizer" : {
                "kuromoji" : {
                    "type" : "kuromoji_tokenizer"
                }
            },
            "analyzer" : {
                "analyzer" : {
                    "type" : "custom",
                        "tokenizer" : "kuromoji"
                }
            }
        }
    }
}'
{"acknowledged":true}

将字符串传递到kuromoji_test

$ curl -XPOST 'http://localhost:9200/kuromoji_test/_analyze?analyzer=analyzer&petty' -d '私はビールが大好きです'
{
    "tokens":[
        {
            "token":"私",
            "start_offset":0,
            "end_offset":1,
            "type":"word",
            "position":1
        },
        {
            "token":"は",
            "start_offset":1,
            "end_offset":2,
            "type":"word",
            "position":2
        },{
            "token":"ビール",
            "start_offset":2,
            "end_offset":5,
            "type":"word",
            "position":3
        },{
            "token":"が",
            "start_offset":5,
            "end_offset":6,
            "type":"word",
            "position":4
        },{
            "token":"大好き",
            "start_offset":6,
            "end_offset":9,
            "type":"word",
            "position":5
        },{
            "token":"です",
            "start_offset":9,
            "end_offset":11,
            "type":"word",
            "position":6
        }
    ]
}

文字列经过了分词解析并返回了结果。

接下来,将日语添加到索引中。

$ curl -XPUT http://localhost:9200/mytest/test/1 -d '
{
    "title" : "テストだよ",
    "text"  : "イケてるサービス作ろうze!"
}'
{"_index":"mytest","_type":"test","_id":"1","_version":3,"created":false}

$ curl -XPUT http://localhost:9200/mytest/test/2 -d '
{
    "title" : "テストだよ2",
    "text"  : "ビールうましうまし"
}'
{"_index":"mytest","_type":"test","_id":"2","_version":1,"created":true}

搜索

$ curl -XGET http://localhost:9200/mytest/test/_search -d '
{
    "query": {
        "match":{"text":"イケてる"}
    }
}'

{
    "took":137,
    "timed_out":false,
    "_shards":{
        "total":5,
        "successful":5,
        "failed":0
    },
    "hits":{
        "total":1,
        "max_score":0.16608897,
        "hits":[{
            "_index":"mytest",
            "_type":"test",
            "_id":"1",
            "_score":0.16608897,
            "_source": {
                "title" : "テストだよ",
                "text"  : "イケてるサービス作ろうze!"
            }
        }]
    }
}

我在网上成功进行了日语搜索,没有遇到任何问题。

广告
将在 10 秒后关闭
bannerAds