Tech Tips

  1. プログラミング
  2. 293 view

Elasticsearch インストール ubuntu

elasticsearch インストール ubuntu

インストール

http://www.elasticsearch.org/overview/elkdownloads/からtar.gzをダウンロードしてインストールする。
$ wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-0.90.7.zip
$ unzip elasticsearch-0.90.7.zip
$ sudo mv elasticsearch-0.90.7 /usr/share
$ sudo ln -s /usr/share/elasticsearch-0.90.7/bin/elasticsearch /usr/bin/elasticsearch
$ sudo vim /etc/init.d/elasticsearch
#! /bin/sh
### BEGIN INIT INFO
# Provides:          elasticsearch
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts elasticsearch
# Description:       Starts elasticsearch using start-stop-daemon
### END INIT INFO
ES_HOME=/usr/share/elasticsearch-0.90.7
ES_MIN_MEM=256m
ES_MAX_MEM=2g
DAEMON=$ES_HOME/bin/elasticsearch
NAME=elasticsearch
DESC=elasticsearch
PID_FILE=/var/run/$NAME.pid
LOG_DIR=/var/log/$NAME
DATA_DIR=/var/lib/$NAME
WORK_DIR=/tmp/$NAME
CONFIG_FILE=$ES_HOME/config/elasticsearch.yml
DAEMON_OPTS="-p $PID_FILE -Des.config=$CONFIG_FILE -Des.path.home=$ES_HOME -Des.path.logs=$LOG_DIR -Des.path.data=$DATA_DIR -Des.path.work=$WORK_DIR"

test -x $DAEMON || exit 0
set -e
case "$1" in
  start)
    echo -n "Starting $DESC: "
    mkdir -p $LOG_DIR $DATA_DIR $WORK_DIR
    if start-stop-daemon --start --pidfile $PID_FILE --startas $DAEMON -- $DAEMON_OPTS
    then
        echo "started."
    else
        echo "failed."
    fi
    ;;
  stop)
    echo -n "Stopping $DESC: "
    if start-stop-daemon --stop --pidfile $PID_FILE
    then
        echo "stopped."
    else
        echo "failed."
    fi
    ;;
  restart|force-reload)
    ${0} stop
    sleep 0.5
    ${0} start
    ;;
  *)
    N=/etc/init.d/$NAME
    echo "Usage: $N {start|stop|restart|force-reload}" >&2
    exit 1
    ;;
esac
exit 0
$ sudo chmod +x /etc/init.d/elasticsearch
$ sudo update-rc.d elasticsearch defaults
$ sudo /etc/init.d/elasticsearch start
Starting elasticsearch: started.

動作チェック

インデックスの作成

curl -XPOST 'http://localhost:9200/test' -d '{
  "settings": {
    "index": {
      "mapping.allow_type_wrapper": true
    }
  }
}'

> {"ok":true,"acknowledged":true}

タイプの作成

curl -XPUT 'http://localhost:9200/test/barley/_mapping' -d '{
    "barley" : {
        "properties" : {
            "place" : {"type" : "string", "index" : "not_analyzed"},
            "ha" : {"type" : "integer"},
            "kg" : {"type" : "integer"},
            "t" : {"type" : "integer"}
        }
    }
}'

> {"ok":true,"acknowledged":true}

テストデータのインサート

curl -X PUT http://localhost:9200/test/barley/1 -d '{
  "barley" : {
     "place":"北海道",
     "ha":1990,
     "kg":337,
     "t":6710}
  }
}'

> {"ok":true,"_index":"test","_type":"barley","_id":"1","_version":1}

検索のテスト

curl -X GET http://localhost:9200/test/barley/_search -d '
{
  "query": {
    "match" : {"place" : "北海道"}
  }
}'

>{
>    "_shards": {
>        "failed": 0,
>        "successful": 5,
>        "total": 5
>    },
>    "hits": {
>        "hits": [
>            {
>                "_id": "1",
>                "_index": "test",
>                "_score": 0.30685282,
>                "_source": {
>                    "barley": {
>                        "ha": 1990,
>                        "kg": 337,
>                        "place": "北海道",
>                        "t": 6710
>                    }
>                },
>                "_type": "barley"
>            }
>        ],
>        "max_score": 0.30685282,
>        "total": 1
>    },
>    "timed_out": false,
>    "took": 114
>}

プラグイン

elasticsearch-analysis-kuromoji

日本語形態素解析エンジンkuromojiを使って全文検索をするためのプラグイン。
$ sudo /usr/share/elasticsearch/bin/plugin -install elasticsearch/elasticsearch-analysis-kuromoji/1.6.0
-> Installing elasticsearch/elasticsearch-analysis-kuromoji/1.6.0...
Trying http://download.elasticsearch.org/elasticsearch/elasticsearch-analysis-kuromoji/elasticsearch-analysis-kuromoji-1.6.0.zip...
Downloading ............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................DONE
Installed elasticsearch/elasticsearch-analysis-kuromoji/1.6.0 into /usr/share/elasticsearch/plugins/analysis-kuromoji

elasticsearch-HQ

ノードやインデックスの状態をWeb画面で見ることができるプラグイン。
http://localhost:9200/_plugin/HQ/
このURLで見ることができる。
$ sudo /usr/share/elasticsearch/bin/plugin -install royrusso/elasticsearch-HQ
-> Installing royrusso/elasticsearch-HQ...
Trying https://github.com/royrusso/elasticsearch-HQ/archive/master.zip...
Downloading .........................................................DONE
Installed royrusso/elasticsearch-HQ into /usr/share/elasticsearch/plugins/HQ
Identified as a _site plugin, moving to _site structure ...

参考

Install

Download Elastic Search from following URL.
http://www.elasticsearch.org/overview/elkdownloads/
$ wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-0.90.7.zip
$ unzip elasticsearch-0.90.7.zip
$ sudo mv elasticsearch-0.90.7 /usr/share
$ sudo ln -s /usr/share/elasticsearch-0.90.7/bin/elasticsearch /usr/bin/elasticsearch
$ sudo vim /etc/init.d/elasticsearch
#! /bin/sh
### BEGIN INIT INFO
# Provides:          elasticsearch
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts elasticsearch
# Description:       Starts elasticsearch using start-stop-daemon
### END INIT INFO
ES_HOME=/usr/share/elasticsearch-0.90.7
ES_MIN_MEM=256m
ES_MAX_MEM=2g
DAEMON=$ES_HOME/bin/elasticsearch
NAME=elasticsearch
DESC=elasticsearch
PID_FILE=/var/run/$NAME.pid
LOG_DIR=/var/log/$NAME
DATA_DIR=/var/lib/$NAME
WORK_DIR=/tmp/$NAME
CONFIG_FILE=$ES_HOME/config/elasticsearch.yml
DAEMON_OPTS="-p $PID_FILE -Des.config=$CONFIG_FILE -Des.path.home=$ES_HOME -Des.path.logs=$LOG_DIR -Des.path.data=$DATA_DIR -Des.path.work=$WORK_DIR"

test -x $DAEMON || exit 0
set -e
case "$1" in
  start)
    echo -n "Starting $DESC: "
    mkdir -p $LOG_DIR $DATA_DIR $WORK_DIR
    if start-stop-daemon --start --pidfile $PID_FILE --startas $DAEMON -- $DAEMON_OPTS
    then
        echo "started."
    else
        echo "failed."
    fi
    ;;
  stop)
    echo -n "Stopping $DESC: "
    if start-stop-daemon --stop --pidfile $PID_FILE
    then
        echo "stopped."
    else
        echo "failed."
    fi
    ;;
  restart|force-reload)
    ${0} stop
    sleep 0.5
    ${0} start
    ;;
  *)
    N=/etc/init.d/$NAME
    echo "Usage: $N {start|stop|restart|force-reload}" >&2
    exit 1
    ;;
esac
exit 0
$ sudo chmod +x /etc/init.d/elasticsearch
$ sudo update-rc.d elasticsearch defaults
$ sudo /etc/init.d/elasticsearch start
Starting elasticsearch: started.

Behavior Check

インデックスの作成

curl -XPOST 'http://localhost:9200/test' -d '{
  "settings": {
    "index": {
      "mapping.allow_type_wrapper": true
    }
  }
}'

> {"ok":true,"acknowledged":true}

タイプの作成

curl -XPUT 'http://localhost:9200/test/barley/_mapping' -d '{
    "barley" : {
        "properties" : {
            "place" : {"type" : "string", "index" : "not_analyzed"},
            "ha" : {"type" : "integer"},
            "kg" : {"type" : "integer"},
            "t" : {"type" : "integer"}
        }
    }
}'

> {"ok":true,"acknowledged":true}

テストデータのインサート

curl -X PUT http://localhost:9200/test/barley/1 -d '{
  "barley" : {
     "place":"北海道",
     "ha":1990,
     "kg":337,
     "t":6710}
  }
}'

> {"ok":true,"_index":"test","_type":"barley","_id":"1","_version":1}

検索のテスト

curl -X GET http://localhost:9200/test/barley/_search -d '
{
  "query": {
    "match" : {"place" : "北海道"}
  }
}'

>{
>    "_shards": {
>        "failed": 0,
>        "successful": 5,
>        "total": 5
>    },
>    "hits": {
>        "hits": [
>            {
>                "_id": "1",
>                "_index": "test",
>                "_score": 0.30685282,
>                "_source": {
>                    "barley": {
>                        "ha": 1990,
>                        "kg": 337,
>                        "place": "北海道",
>                        "t": 6710
>                    }
>                },
>                "_type": "barley"
>            }
>        ],
>        "max_score": 0.30685282,
>        "total": 1
>    },
>    "timed_out": false,
>    "took": 114
>}

プログラミングの最近記事

  1. PubSubClient の便利さと注意点

  2. Java の環境構築方法メモ

  3. PlatformIO IDE for VSCode を使用して VSCode で Ardu…

  4. ROS Docker イメージで発生した GPG error の解消方法

  5. Streamlit で訪れた国を色づけした世界地図を作成できるアプリケーションを作成してみ…

関連記事

PAGE TOP