LoginSignup
26
21

More than 5 years have passed since last update.

よく使うElasticSearchのクエリ(ドキュメント操作)

Last updated at Posted at 2017-10-28

ElasticSearchは、マニュアル(英語)がしっかりしてるので、そっちを参考にしてください。
マニュアル(本家):https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html

とりあえず、理解しやすく、MySQLのSQL文と比較しながら、まとめます。

環境

  • CentOS 7.1
  • ElasticSearch 5.6.2
    ※ElasticSearch 6.xでは、curlに「 -H 'Content-Type: application/json'」を付ける必要があります。

目次

ドキュメント(レコード)操作

■ _idを指定したときの登録方法

elasticsearchクエリ
curl -XPUT http://localhost:9200/hoge_index/hoge_type/2 -d '{
 "field_01": "hoge1",
 "field_02": "hoge2",
 "field_03": "hoge3"
}'

_id=2がデータの有無をチェックして、
データがなければMySQLでいうINSERT、データがあればMySQLでいうUPDATEを実行してくれる。

MySQL(データがない場合)
INSERT INTO `hoge_index` . `hoge_type` (`_id`, `field_01`, `field_02`, `field_03`) 
VALUES (2, 'hoge1', 'hoge2', 'hoge3')
MySQL(データがある場合)
UPDATE `hoge_index` . `hoge_type` SET 
  `field_01` = 'hoge1', 
  `field_02` = 'hoge2', 
  `field_03` = 'hoge3'
WHERE `_id` = 2

■ _idを指定しない登録方法

MySQLでいうところのauto_incrementのこと。

elasticsearch
curl -XPOST http://localhost:9200/hoge_index/hoge_type/ -d '{
 "field_01": "hoge1",
 "field_02": "hoge2",
 "field_03": "hoge3"
}'
MySQL
INSERT INTO `hoge_index` . `hoge_type` (`field_01`, `field_02`, `field_03`) 
VALUES ('hoge1', 'hoge2', 'hoge3')

■ 一部のカラムを更新する方法

elasticsearch
curl -XPOST http://localhost:9200/hoge_index/hoge_type/2/_update -d '{
  "doc": {
    "field_01": "hoge1_upd"
  }
}'
MySQL
UPDATE `hoge_index` . `hoge_type` SET 
  `field_01` = 'hoge1_upd'
WHERE `_id` = 2

■ 条件を指定してデータを一括更新

※タイプは「hoge_type」とする。

elasticsearch
curl -XPOST 'http://localhost:9200/hoge_index/_update_by_query?conflicts=proceed&pretty' -d '{
  "query": {
    "term": {
      "field_03": {
        "value": "hoge3"
      }
    }
  },
  "script": {
    "inline": "ctx._source.field_01 = \"Hoge hoge\""
  }
}'
MySQL
UPDATE `hoge_index` . `hoge_type` SET
  `field_01` = 'Hoge hoge'
WHERE 
  `field_03` = 'hoge3'

■ 別のカラムの値に一括更新

※タイプは「hoge_type」とする。

elasticsearch
curl -XPOST 'http://localhost:9200/hoge_index/_update_by_query?conflicts=proceed&pretty' -d '{
  "query": {
    "term": {
      "field_03": {
        "value": "hoge3"
      }
    }
  },
  "script": {
    "inline": "ctx._source.field_02 = ctx._source.field_01"
  }
}'
MySQL
UPDATE `hoge_index` . `hoge_type` SET
  `field_02` = `field_01`
WHERE 
  `field_03` = 'hoge3'

■ 条件を指定しないでデータを一括更新

※タイプは「hoge_type」とする。

elasticsearch
curl -XPOST 'http://localhost:9200/hoge_index/_update_by_query?conflicts=proceed&pretty' -d '{
  "query": {
    "match_all": { }
  },
  "script": {
    "inline": "ctx._source.field_01 = \"Hoge\"; ctx._source.field_02 = \"\";"
  }
}'
MySQL
UPDATE `hoge_index` . `hoge_type` SET
  `field_01` = 'Hoge', 
  `field_02` = ''

■ 条件を指定してデータを一括削除

※タイプは「hoge_type」とする。

elasticsearch
curl -XPOST 'http://localhost:9200/hoge_index/_delete_by_query?conflicts=proceed&pretty' -d '{
  "query": {
    "term": {
      "field_03": {
        "value": "hoge3"
      }
    }
  }
}'
MySQL
DELETE FROM `hoge_index` . `hoge_type`
WHERE 
  `field_03` = 'hoge3'

■ 条件を指定しないでデータを一括更新

※タイプは「hoge_type」とする。

elasticsearch
curl -XPOST 'http://localhost:9200/hoge_index/_delete_by_query?conflicts=proceed&pretty' -d '{
  "query": {
    "match_all": { }
  }
}'
MySQL
DELETE FROM `hoge_index` . `hoge_type`

or 

TRUNCATE `hoge_index` . `hoge_type`

参考

26
21
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
26
21