Elastic Stack 9 : Elasticsearch インストール2025/08/08 |
|
全文検索エンジン Elasticsearch をインストールします。 |
|
| [1] | Elasticsearch をインストールして起動します。 Java のインストールは不要です。Elasticsearch に統合されています。 |
[root@dlp ~]# cat > /etc/yum.repos.d/elasticsearch.repo <<EOF
[elasticsearch-9.x]
name=Elasticsearch repository for 9.x packages
baseurl=https://artifacts.elastic.co/packages/9.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
EOF
[root@dlp ~]#
dnf -y install elasticsearch ..... ..... --------------------------- Security autoconfiguration information ------------------------------ Authentication and authorization are enabled. TLS for the transport and HTTP layers is enabled and configured. The generated password for the elastic built-in superuser is : FkAK-+pw-jJ_liPxalZd If this node should join an existing cluster, you can reconfigure this with '/usr/share/elasticsearch/bin/elasticsearch-reconfigure-node --enrollment-token <token-here>' after creating an enrollment token on your existing cluster. You can complete the following actions at any time: Reset the password of the elastic built-in superuser with '/usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic'. Generate an enrollment token for Kibana instances with '/usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s kibana'. Generate an enrollment token for Elasticsearch nodes with '/usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s node'. ------------------------------------------------------------------------------------------------- ### NOT starting on installation, please execute the following statements to configure elasticsearch service to start automatically using systemd sudo systemctl daemon-reload sudo systemctl enable elasticsearch.service ### You can start elasticsearch service by executing sudo systemctl start elasticsearch.service /usr/lib/tmpfiles.d/elasticsearch.conf:1: Line references path below legacy directory /var/run/, updating /var/run/elasticsearch → /run/elasticsearch; please update the tmpfiles.d/ drop-in file accordingly. Installed: elasticsearch-9.1.1-1.x86_64 Complete!
[root@dlp ~]#
systemctl enable --now elasticsearch
# 動作確認 # パスワードはインストール中に表示されたパスワードで応答 [root@dlp ~]# curl -u elastic --cacert /etc/elasticsearch/certs/http_ca.crt https://127.0.0.1:9200
Enter host password for user 'elastic':
{
"name" : "dlp.srv.world",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "oI1WSy2sSD2HYcmmEx2sbQ",
"version" : {
"number" : "9.1.1",
"build_flavor" : "default",
"build_type" : "rpm",
"build_hash" : "5e94055934defa56e454868b7783b2a3b683785e",
"build_date" : "2025-08-05T01:07:31.959947279Z",
"build_snapshot" : false,
"lucene_version" : "10.2.2",
"minimum_wire_compatibility_version" : "8.19.0",
"minimum_index_compatibility_version" : "8.0.0"
},
"tagline" : "You Know, for Search"
}
|
| [2] |
Elasticsearch を他ホストからも利用する場合は、クラスターの設定を参照ください。 |
| [3] | 日本語が扱えるようにプラグインをインストールします。 |
|
# analysis-kuromoji インストール [root@dlp ~]# /usr/share/elasticsearch/bin/elasticsearch-plugin install analysis-kuromoji -> Installing analysis-kuromoji -> Downloading analysis-kuromoji from elastic [=================================================] 100% -> Installed analysis-kuromoji -> Please restart Elasticsearch to activate any plugins installed # インストール済みプラグイン表示 [root@dlp ~]# /usr/share/elasticsearch/bin/elasticsearch-plugin list analysis-kuromoji |
| [4] | 基本的な使い方です。 まず最初に Index を作成します。RDB のデータベースに相当する概念です。 |
|
# Index 一覧を表示 (pretty は JSON 形式の表示を見易くするオプション) [root@dlp ~]# curl -u elastic --cacert /etc/elasticsearch/certs/http_ca.crt https://127.0.0.1:9200/_aliases?pretty
Enter host password for user 'elastic':
{
".security-7" : {
"aliases" : {
".security" : {
"is_hidden" : true
}
}
}
}
# Index 作成 [root@dlp ~]# curl -u elastic --cacert /etc/elasticsearch/certs/http_ca.crt -X PUT "https://127.0.0.1:9200/test_index" Enter host password for user 'elastic': {"acknowledged":true,"shards_acknowledged":true,"index":"test_index"} # 確認 [root@dlp ~]# curl -u elastic --cacert /etc/elasticsearch/certs/http_ca.crt https://127.0.0.1:9200/_aliases?pretty
Enter host password for user 'elastic':
{
".security-7" : {
"aliases" : {
".security" : {
"is_hidden" : true
}
}
},
"test_index" : {
"aliases" : { }
}
}
[root@dlp ~]# curl -u elastic --cacert /etc/elasticsearch/certs/http_ca.crt https://127.0.0.1:9200/test_index/_settings?pretty
Enter host password for user 'elastic':
{
"test_index" : {
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "1",
"provided_name" : "test_index",
"creation_date" : "1754609895813",
"number_of_replicas" : "1",
"uuid" : "4Hl4VKaGTRuiwBVCCTijPQ",
"version" : {
"created" : "9033000"
}
}
}
}
}
|
| [5] | Mapping とテストデータの投入を実行します。 Mapping は Index の構造を定義するものです。データを投入すると自動で Mapping 定義されますが、手動で定義することももちろん可能です。 |
|
# データ投入
[root@dlp ~]# curl -u elastic --cacert /etc/elasticsearch/certs/http_ca.crt \
-H "Content-Type: application/json" \
-X PUT "https://127.0.0.1:9200/test_index/_doc/001" -d '{
"subject" : "Test Post No.1",
"description" : "This is the initial post",
"content" : "This is the test message for using Elasticsearch."
}'
Enter host password for user 'elastic':
{"_index":"test_index","_id":"001","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}
# Mapping 確認 [root@dlp ~]# curl -u elastic --cacert /etc/elasticsearch/certs/http_ca.crt "https://127.0.0.1:9200/test_index/_mapping/?pretty"
Enter host password for user 'elastic':
{
"test_index" : {
"mappings" : {
"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 -u elastic --cacert /etc/elasticsearch/certs/http_ca.crt "https://127.0.0.1:9200/test_index/_doc/001?pretty"
Enter host password for user 'elastic':
{
"_index" : "test_index",
"_id" : "001",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 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 -u elastic --cacert /etc/elasticsearch/certs/http_ca.crt "https://127.0.0.1:9200/test_index/_search?q=description:initial&pretty=true"
Enter host password for user 'elastic':
{
"took" : 56,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "test_index",
"_id" : "001",
"_score" : 0.2876821,
"_source" : {
"subject" : "Test Post No.1",
"description" : "This is the initial post",
"content" : "This is the test message for using Elasticsearch."
}
}
]
}
}
|
| Sponsored Link |
|
|