全文検索のfessは触ったことがありますが、 fessが内部で使用する elasticsearch はありませんでしたので、hands-on
今回のポイントは
参考url
- https://www.elastic.co/guide/en/elasticsearch/reference/current/targz.html
- https://www.server-world.info/query?os=Ubuntu_24.04&p=elasticstack8&f=1
install
$ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.15.2-linux-x86_64.tar.gz $ tar -xzf elasticsearch-8.15.2-linux-x86_64.tar.gz $ cd elasticsearch-8.15.2/
初回起動と、rootパスワード確認
ここで発行されたパスワードは、この後のcurlコマンド実行時に使用します
$ bin/elasticsearch Oct 09, 2024 6:28:03 PM sun.util.locale.provider.LocaleProviderAdapter <clinit> WARNING: COMPAT locale provider will be removed in a future release 【略】 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Elasticsearch security features have been automatically configured! Authentication is enabled and cluster connections are encrypted. Password for the elastic user (reset with `bin/elasticsearch-reset-password -u elastic`): mNXX=JAaEr+gVO5zDQhB ★ 【略】
上記の bin/elasticsearch を起動したまま、以下のcurlコマンドで起動結果確認
$ curl --cacert config/certs/http_ca.crt -u elastic https://localhost:9200
Enter host password for user 'elastic': mNXX=JAaEr+gVO5zDQhB
{
"name" : "a64",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "_1Zg8GmwSdS8pk94jvNoPQ",
"version" : {
"number" : "8.15.2",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "98adf7bf6bb69b66ab95b761c9e5aadb0bb059a3",
"build_date" : "2024-09-19T10:06:03.564235954Z",
"build_snapshot" : false,
"lucene_version" : "9.11.1",
"minimum_wire_compatibility_version" : "7.17.0",
"minimum_index_compatibility_version" : "7.0.0"
},
"tagline" : "You Know, for Search"
}
index作成と作成結果の確認 (今回のindex名は test_index )
index作成
$ curl --cacert config/certs/http_ca.crt -u elastic -X PUT https://localhost:9200/test_index
Enter host password for user 'elastic': mNXX=JAaEr+gVO5zDQhB
{"acknowledged":true,"shards_acknowledged":true,"index":"test_index"}
作成結果の確認
$ curl --cacert config/certs/http_ca.crt -u elastic https://localhost:9200/_aliases?pretty
Enter host password for user 'elastic': mNXX=JAaEr+gVO5zDQhB
{
".security-7" : {
"aliases" : {
".security" : {
"is_hidden" : true
}
}
},
"test_index" : {
"aliases" : { }
}
}
$ curl --cacert config/certs/http_ca.crt -u elastic https://localhost:9200/_settings?pretty
Enter host password for user 'elastic': mNXX=JAaEr+gVO5zDQhB
{
"test_index" : {
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "1",
"provided_name" : "test_index",
"creation_date" : "1728501776101",
"number_of_replicas" : "1",
"uuid" : "274wN-66TZCwly5cWHO6ZQ",
"version" : {
"created" : "8512000"
}
}
}
}
}
検索データの登録による Mapping 作成
Mapping は Index の構造を定義するもので、 データを投入すると自動で Mapping 定義されるが、手動定義することも可能らしい
$ curl --cacert config/certs/http_ca.crt -u elastic \
-H "Content-Type: application/json" -X PUT \
https://localhost: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': mNXX=JAaEr+gVO5zDQhB
{"_index":"test_index",
"_id":"001",
"_version":1,
"result":"created",
"_shards":{"total":2, "successful":1,"failed":0},
"_seq_no":0,
"_primary_term":1}
↑検索データの登録による Mapping 作成 ↓作成結果確認
$ curl --cacert config/certs/http_ca.crt -u elastic \
https://localhost:9200/_mapping/?pretty
Enter host password for user 'elastic': mNXX=JAaEr+gVO5zDQhB
{ "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
}
}
}
}
}
}
}
登録した検索データの確認
$ curl --cacert config/certs/http_ca.crt -u elastic \
https://localhost:9200/test_index/_doc/001?pretty
Enter host password for user 'elastic': mNXX=JAaEr+gVO5zDQhB
{
"_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] 含む)
$ curl --cacert config/certs/http_ca.crt -u elastic \
"https://localhost:9200/test_index/_search?q=description:initial&pretty=true"
Enter host password for user 'elastic': mNXX=JAaEr+gVO5zDQhB
{
"took" : 58,
"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."
}
}
]
}
}