LoginSignup
17
13

More than 5 years have passed since last update.

Elasticsearchのインストール手順と簡単な使い方

Last updated at Posted at 2017-10-12

概要

Elasticsearchのインストール手順と簡単な使い方です。

環境

  • CentOS 7.2
  • elasticsearch 5.6.3-1
  • openjdk version "1.8.0_144"
  • OpenJDK Runtime Environment (build 1.8.0_144-b01)
  • OpenJDK 64-Bit Server VM (build 25.144-b01, mixed mode)

インストール手順

javaのインストール

以下を参考にしてインストール
CentOS7にJava OpenJDK8のインストール

Elasticsearchのインストール

repository追加

$ sudo vi /etc/yum.repos.d/elasticsearch.repo

[elasticsearch-5.x]
name=Elasticsearch repository for 5.x packages
baseurl=https://artifacts.elastic.co/packages/5.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md

インストール

$ sudo yum -y install elasticsearch 

自動起動設定

$ sudo systemctl start elasticsearch
$ sudo systemctl enable elasticsearch 

memoryの設定変更

Elasticsearchは、デフォルトでメモリは2GB必要です。
例えば、AWS EC2のインスタンスタイプt2.microを使用している場合は、メモリは1GBしかありません。
なので、メモリの使用量をもっと少ない設定にする必要があります。
ここでは512MBに変更します。
参考:https://qiita.com/tjinjin/items/7ea3ebd228748f9d5224

$ sudo vi /etc/elasticsearch/jvm.options

# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space

--Xms2g
--Xmx2g
+-Xms512m
+-Xmx512m

設定変更をしたら、elasticsearchを再起動します。

$ sudo systemctl restart elasticsearch

activeになっているか確認してください。

$ sudo systemctl status elasticsearch
  elasticsearch.service - Elasticsearch
   Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2017-10-13 05:34:09 UTC; 6min ago

動作確認

$ curl http://127.0.0.1:9200
{
  "name" : "BEgoRLj",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "mTb6VzKURV2jjjtqiEPhYg",
  "version" : {
    "number" : "5.6.3",
    "build_hash" : "1a2f265",
    "build_date" : "2017-10-06T20:33:39.012Z",
    "build_snapshot" : false,
    "lucene_version" : "6.6.1"
  },
  "tagline" : "You Know, for Search"
}

日本語が扱えるようにする

analysis-kuromojiをインストールします。

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

インストール確認

$ sudo /usr/share/elasticsearch/bin/elasticsearch-plugin list | grep analysis-kuromoji
analysis-kuromoji

簡単な使い方

インデックスを作成

インデックスとは、RDBでいうtableのことです。

$ curl -X PUT "http://127.0.0.1:9200/sample_index"

{"acknowledged":true,"shards_acknowledged":true,"index":"sample_index"}

インデックス一覧確認

RDB: show tables;

$ curl http://127.0.0.1:9200/_aliases?pretty

{
  "sample_index" : {
    "aliases" : { }
  }
}

作成したsample_indexの詳細確認

$ curl http://127.0.0.1:9200/sample_index/_settings?pretty

{
  "sample_index" : {
    "settings" : {
      "index" : {
        "creation_date" : "1507788470720",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "DQV3vNdWQVGOHA2xXDi9lQ",
        "version" : {
          "created" : "5060399"
        },
        "provided_name" : "sample_index"
      }
    }
  }
}

マッピングの作成

マッピングとは、インデックスの構造のことです。
データを投入すれば、自動でマッピングが作成されますが、事前に手動で定義することも可能です。

データを投入する

データを入れてみます。

RDB: insert into sample_index (title, description, amount) values("Sample No.1", "This is a sample data", 20);

$ curl -X PUT "http://127.0.0.1:9200/sample_index/doc01/1" -d '{
    "title" : "Sample No.1",
    "description" : "This is a sample data",
    "amount" : 20
}'

{"_index":"sample_index","_type":"doc01","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"created":true}

マッピングの確認

データ投入によって、マッピングが作成されたことを確認します。

RDB: describe sample_index;

$ curl "http://127.0.0.1:9200/sample_index/_mapping/doc01?pretty"

{
  "sample_index" : {
    "mappings" : {
      "doc01" : {
        "properties" : {
          "amount" : {
            "type" : "long"
          },
          "description" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "title" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          }
        }
      }
    }
  }
}

データを確認

データが投入されたことを確認します。

RDB: select * from sample_index;

$ curl "http://127.0.0.1:9200/sample_index/doc01/1?pretty"

{
  "_index" : "sample_index",
  "_type" : "doc01",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "title" : "Sample No.1",
    "description" : "This is a sample data",
    "amount" : 20
  }
}

データの検索

投入したデータの検索をしてみます。
descriptionに'sample'と入っているレコードを検索します。

RDB: select * from sample_index where description like '%sample%';

$ curl "http://127.0.0.1:9200/sample_index/doc01/_search?q=description:sample&pretty=true"

{
  "took" : 16,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.2824934,
    "hits" : [
      {
        "_index" : "sample_index",
        "_type" : "doc01",
        "_id" : "1",
        "_score" : 0.2824934,
        "_source" : {
          "title" : "Sample No.1",
          "description" : "This is a sample data",
          "amount" : 20
        }
      }
    ]
  }
}

参考

17
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
13