135
110

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 2019-01-30

#Elasticsearchコマンド一覧
よく使うコマンドをメモしておく。

##目次

  1. Elasticsearch関連
  2. インデックス関連
  3. エイリアス関連
  4. ドキュメントタイプ関連
  5. ReIndexAPI
  6. Scroll API ←2020年4月22日追記

##Elasticsearch関連
###起動/再起動

sudo /etc/init.d/elasticsearch start
sudo /etc/init.d/elasticsearch restart

###状態確認

curl -XGET 'localhost:9200/'

##インデックス関連
###一覧

curl -XGET 'localhost:9200/_cat/indices?v'

###設定確認

curl -XGET 'localhost:9200/{indexName}/_settings?pretty'

###マッピング確認

curl -XGET 'localhost:9200/{indexName}/_mapping?pretty'

###削除

curl -XDELETE 'localhost:9200/{indexName}?pretty'

##エイリアス関連
###エイリアス確認

curl -XGET 'localhost:9200/_aliases?pretty'

##ドキュメントタイプ関連
###件数確認

curl -sS -XGET 'localhost:9200/{indexName}/{typeName}/_count?pretty'

###フィールド定義追加

curl -XPUT 'localhost:9200/{indexName}/_mappings/{typeName}?pretty' -d '
{
  "properties" : {
    "field1" : {
      "type" : "long"
    },
    "field2" : {
      "type" : "date"
    }
  }
}'

###データ一括登録(ID指定なし)

curl -XPOST 'localhost:9200/{indexName}/{typeName}/_bulk?pretty' --data-binary @xxx.json
xxx.json
{ "index" : {} }
{"name":"おにぎり","category":"食べ物"}
{ "index" : {} }
{"name":"車","category":"乗り物"}

###データ削除(全件)

curl -XPOST 'localhost:9200/{indexName}/{typeName}/_delete_by_query' --d '
{
  "query":{
    "match_all":{
    }
  }
}'

###データ更新(id指定)

curl -XPOST 'localhost:9200/{indexName}/{typeName}/{_id}/_update' --d '
{
  "doc": { "name": "Jane Doe", "age": 20 }
}'

###データ検索(無条件)

curl -sS -XGET 'localhost:9200/{indexName}/{typeName}/_search?pretty'

###データ検索(From-To指定)

curl -sS -XGET 'localhost:9200/{indexName}/{typeName}/_search?pretty' -d '
{
  "query":{
    "match_all":{
    }
  },
  "from":0,
  "size":400
}'

###データ更新(ID指定)
{typeName}の{_id}のnameを変更し、項目にexpirationを追加する場合

curl -XPOST 'localhost:9200/{indexName}/{typeName}/{_id}/_update' -d '
{
  "doc":{
    "name":"焼きおにぎり","category":"食べ物","expiration":"9999-12-31"
  }
}'

###データ一括更新

curl -XPOST 'localhost:9200/{indexName}/{typeName}/_update_by_query?conflicts=proceed&pretty' -d '
{
  "query":{
    "match_all": { }
  },
  "script":{
    "inline": "ctx._source.field1 = 'hogehoge'"
  }
}'

##ReIndexAPI
###再インデックス

curl -XPOST 'localhost:9200/_reindex?pretty' -d '
{
  "source": {
    "index": "fromIndex"
  },
  "dest": {
    "index": "toIndex"
  }
}'

###再インデックス(リモート)
※reindex.remote.whitelistへ登録の必要あり。(elasticsearch.yml)

curl -XPOST 'localhost:9200/_reindex?pretty' -d '
{
  "source": {
    "remote": {
      "host": "http://otherhost:9200",
      "username": "user",
      "password": "pass"
    },
    "index": "fromIndex"
    }
  },
  "dest": {
    "index": "toIndex"
  }
}

##Scroll API

【Elasticsearch】ScrollAPIを使ってみた ←2020年4月22日追記

135
110
1

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
135
110

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?