CentOS 7
Sponsored Link

Elastic Stack 6 : Elasticsearch インストール2018/02/12

 
全文検索エンジン Elasticsearch をインストールします。
[1]
[2] Elasticsearch をインストールして起動します。
[root@dlp ~]#
vi /etc/yum.repos.d/elasticsearch.repo
# 新規作成

[elasticsearch-6.x]
name=Elasticsearch repository for 6.x packages
baseurl=https://artifacts.elastic.co/packages/6.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
[root@dlp ~]#
yum -y install elasticsearch
[root@dlp ~]#
systemctl start elasticsearch

[root@dlp ~]#
systemctl enable elasticsearch
# 動作確認

[root@dlp ~]#
curl http://127.0.0.1:9200

{
  "name" : "bV0GuFK",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "JprAV7AzTtSNL0De6BWwEQ",
  "version" : {
    "number" : "6.2.1",
    "build_hash" : "7299dc3",
    "build_date" : "2018-02-07T19:34:26.990113Z",
    "build_snapshot" : false,
    "lucene_version" : "7.2.1",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}
[3] 日本語が扱えるようにプラグインをインストールします。
# analysis-kuromoji インストール

[root@dlp ~]#
/usr/share/elasticsearch/bin/elasticsearch-plugin install analysis-kuromoji

-> Downloading analysis-kuromoji from elastic
[=================================================] 100%
-> Installed analysis-kuromoji
# インストール済みプラグイン表示

[root@dlp ~]#
/usr/share/elasticsearch/bin/elasticsearch-plugin list

analysis-kuromoji
[4] 基本的な使い方です。
まず最初に Index を作成します。RDB のデータベースに相当する概念です。
# Index 一覧を表示 (pretty は JSON形式の表示を見易くするオプション)

[root@dlp ~]#
curl http://127.0.0.1:9200/_aliases?pretty

{ }
# Index 作成

[root@dlp ~]#
curl -X PUT "http://127.0.0.1:9200/test_index"

{"acknowledged":true,"shards_acknowledged":true,"index":"test_index"}
# 確認

[root@dlp ~]#
curl http://127.0.0.1:9200/_aliases?pretty

{
  "test_index" : {
    "aliases" : { }
  }
}
[root@dlp ~]#
curl http://127.0.0.1:9200/test_index/_settings?pretty

{
  "test_index" : {
    "settings" : {
      "index" : {
        "creation_date" : "1518494916064",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "f2UHmZ9US4q4Kw4AtfPEyw",
        "version" : {
          "created" : "6020199"
        },
        "provided_name" : "test_index"
      }
    }
  }
}
[5] Mapping とテストデータの投入を実行します。
Mapping は Index の構造を定義するものです。データを投入すると自動で Mapping 定義されますが、手動で定義することももちろん可能です。
# データ投入

[root@dlp ~]#
curl -H "Content-Type: application/json" \
-X PUT "http://127.0.0.1:9200/test_index/doc01/1" -d '{
    "subject" : "Test Post No.1",
    "description" : "This is the initial post",
    "content" : "This is the test message for using Elasticsearch."
}'

# Mapping 確認

[root@dlp ~]#
curl "http://127.0.0.1:9200/test_index/_mapping/doc01?pretty"

{
  "test_index" : {
    "mappings" : {
      "doc01" : {
        "properties" : {
          "content" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "description" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "subject" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          }
        }
      }
    }
  }
}

# データ確認

[root@dlp ~]#
curl "http://127.0.0.1:9200/test_index/doc01/1?pretty"

{
  "_index" : "test_index",
  "_type" : "doc01",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "subject" : "Test Post No.1",
    "description" : "This is the initial post",
    "content" : "This is the test message for using Elasticsearch."
  }
}

# データ検索

# 例の検索条件は [description] に [initial] 含む

[root@dlp ~]#
curl "http://127.0.0.1:9200/test_index/doc01/_search?q=description:initial&pretty=true"

{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "test_index",
        "_type" : "doc01",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "subject" : "Test Post No.1",
          "description" : "This is the initial post",
          "content" : "This is the test message for using Elasticsearch."
        }
      }
    ]
  }
}
関連コンテンツ