1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Elasticsearch の使い方

Last updated at Posted at 2018-10-03

Arch Linux へのインストール方法

sudo pacman -S elasticsearch

サーバーの状態の確認

sudo systemctl status elasticsearch

サーバーの起動

sudo systemctl start elasticsearch

クライアントから、サーバーの確認

$ curl http://127.0.0.1:9200
{
  "name" : "iwata",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "Gvg6GQ6LSJqkqZpaXSNyGA",
  "version" : {
    "number" : "7.10.2",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "unknown",
    "build_date" : "2021-08-25T17:55:24.048595Z",
    "build_snapshot" : false,
    "lucene_version" : "8.7.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

データの投入

go_insert.sh
curl -H "Content-Type: application/json" \
	-X PUT "http://127.0.0.1:9200/test/_doc/1" \
	-d '{
	"user" : "uchida",
	"counter" : 1,
	"tags" : ["red"]
}' | jq .

実行結果

$ ./go_insert.sh 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   209  100   152  100    57     73     27  0:00:02  0:00:02 --:--:--   101
{
  "_index": "test",
  "_type": "_doc",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}

データの値を見る

go_get.sh
curl http://127.0.0.1:9200/test/_doc/1 | jq .

実行結果

$ ./go_get.sh 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   136  100   136    0     0  27200      0 --:--:-- --:--:-- --:--:-- 27200
{
  "_index": "test",
  "_type": "_doc",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "user": "uchida",
    "counter": 1,
    "tags": [
      "red"
    ]
  }
}

データの更新

counter に 4 を加える

go_update.sh
curl -H "Content-Type: application/json" \
	-X POST "http://127.0.0.1:9200/test/_doc/1/_update" \
	-d '{
"script" : {
        "source": "ctx._source.counter += params.count",
        "lang": "painless",
        "params" : {
            "count" : 4
        }
    }
}' | jq .

実行結果

$ ./go_update.sh 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   314  100   152  100   162   1490   1588 --:--:-- --:--:-- --:--:--  3078
{
  "_index": "test",
  "_type": "_doc",
  "_id": "1",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 1,
  "_primary_term": 1
}

値の確認

$ ./go_get.sh 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   123  100   123    0     0  30750      0 --:--:-- --:--:-- --:--:-- 30750
{
  "_index": "test",
  "_type": "_doc",
  "_id": "1",
  "_version": 2,
  "found": true,
  "_source": {
    "user": "uchida",
    "counter": 5,
    "tags": [
      "red"
    ]
  }
}

もう一度、go_update.sh を実行すると counter は 9 になります。

データの削除

go_del.sh
curl -XDELETE 'http://localhost:9200/test'

実行結果

$ ./go_del.sh 
{"acknowledged":true}

ヒープメモリを小さくする方法

1G を 512M に変更
設定を変更してから、Elasticsearch を再起動

//etc/elasticsearch/jvm.options
#-Xms1g
#-Xmx1g
-Xms512m
-Xmx512m
1
3
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
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?