以最简单的方式尝试Elasticsearch

我想做的事情 (Wǒ zuò de

    Elasticsearchをシングルノードで設定し、外部からデータを追加・検索できるようにする

验证环境

    VagrantでホスティングしたUbuntu Server 22.04 LTS

安装

    基本的に公式ドキュメントの手順に従っている
$ wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg
$ sudo apt update
$ sudo apt install apt-transport-https -y
$ echo "deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-8.x.list
$ sudo apt update
$ sudo apt install elasticsearch -y

设定

    • ※ 変更点のみ抜粋

 

    • シングルノードで動作するよう設定

 

    • リモートからHTTP(TCPポート9200番)でのアクセスを受け付けるよう設定

 

    認証やHTTPS通信をオフにするよう設定(検証を簡単にするため)
$ sudo vim /etc/elasticsearch/elasticsearch.yml
# cluster.initial_master_nodes: ["ubuntu2204.localdomain"]
discovery.type: single-node
http.host: 0.0.0.0
http.port: 9200
xpack.security.enabled: false
    • UFWを有効化する設定

 

    TCPポート9200番への外部からのアクセスを許可する設定
$ sudo ufw status
Status: inactive

$ sudo ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup

$ sudo ufw allow 9200/tcp
Rule added
Rule added (v6)

$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
9200/tcp                   ALLOW       Anywhere                  
9200/tcp (v6)              ALLOW       Anywhere (v6)

启动

$ sudo systemctl start elasticsearch

$ sudo systemctl status elasticsearch
● elasticsearch.service - Elasticsearch
     Loaded: loaded (/lib/systemd/system/elasticsearch.service; disabled; vendor preset: enabled)
     Active: active (running) since Mon 2023-12-11 00:53:44 UTC; 45s ago
       Docs: https://www.elastic.co
   Main PID: 3703 (java)
      Tasks: 74 (limit: 2237)
     Memory: 1.3G
        CPU: 1min 10.987s
     CGroup: /system.slice/elasticsearch.service
             ├─3703 /usr/share/elasticsearch/jdk/bin/java -Xms4m -Xmx64m -XX:+UseSerialGC -Dcli.name=server -Dcli.script=/usr/share/elasticsearch/bin/elasticsearch -Dcli.libs=lib/tools/server-cli -Des.path.home=/usr/>
             ├─3761 /usr/share/elasticsearch/jdk/bin/java -Des.networkaddress.cache.ttl=60 -Des.networkaddress.cache.negative.ttl=10 -Djava.security.manager=allow -XX:+AlwaysPreTouch -Xss1m -Djava.awt.headless=true ->
             └─3781 /usr/share/elasticsearch/modules/x-pack-ml/platform/linux-x86_64/bin/controller

Dec 11 00:53:09 ubuntu2204.localdomain systemd[1]: Starting Elasticsearch...
Dec 11 00:53:13 ubuntu2204.localdomain systemd-entrypoint[3703]: Dec 11, 2023 12:53:13 AM sun.util.locale.provider.LocaleProviderAdapter <clinit>
Dec 11 00:53:13 ubuntu2204.localdomain systemd-entrypoint[3703]: WARNING: COMPAT locale provider will be removed in a future release
Dec 11 00:53:44 ubuntu2204.localdomain systemd[1]: Started Elasticsearch.

$ ss -nlp | grep 9200
tcp   LISTEN 0      4096                                            *:9200                   *:* 

确认动作

# ヘルスチェック
$ curl -X GET "192.168.56.10:9200/_cluster/health?pretty"
{
  "cluster_name" : "elasticsearch",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 1,
  "number_of_data_nodes" : 1,
  "active_primary_shards" : 0,
  "active_shards" : 0,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 0,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0,
  "task_max_waiting_in_queue_millis" : 0,
  "active_shards_percent_as_number" : 100.0
}

# messagesインデックスを作成
$ curl -X PUT "192.168.56.10:9200/messages?pretty"
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "messages"
}

# データを作成
$ curl -X POST "192.168.56.10:9200/messages/_doc/1/?pretty" -H "Content-Type: application/json" -d '{"message": "hello, world"}'
{
  "_index" : "messages",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

# データを確認
$ curl -X GET "192.168.56.10:9200/messages/_doc/1?pretty" 
{
  "_index" : "messages",
  "_id" : "1",
  "_version" : 3,
  "_seq_no" : 2,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "message" : "hello, world"
  }
}
广告
将在 10 秒后关闭
bannerAds