0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Elasticsearchの基本操作

Last updated at Posted at 2024-12-09

Elasticsearchの基本操作 RESTFUL APIツールを使う

  • curlコマンド
  • GUI(POstman, Kibana, Dev Tools)

基本的なリクエスト例

1.動作確認

起動

elasticsearch

動作確認。elasticsearchのバージョンやクラスタ情報が返ります。

curl http://localhost:9200/

2.インデックス作成

curl -X PUT "http://localhost:9200/my_index"

3.データの追加

作成済みのindexを指定して、json形式の値を送信します。

curl -X POST "http://localhost:9200/my_index/_doc/1" -H "Content-Type: application/json" -d' { "name": "John Doe", "age": 30, "city": "New York" }'

response

{"_index":"my_index","_type":"_doc","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}% 

4.データの検索

curl -X GET "http://localhost:9200/my_index/_search?q=name:John"

response

{"took":509,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":0.2876821,"hits":[{"_index":"my_index","_type":"_doc","_id":"1","_score":0.2876821,"_source": { "name": "John Doe", "age": 30, "city": "New York" }}]}}% 

5. データの更新

curl -X POST "http://localhost:9200/my_index/_update/1" -H "Content-Type: application/json" -d' { "doc": { "age": 31 } }' 

response

{"_index":"my_index","_type":"_doc","_id":"1","_version":2,"result":"updated","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":1,"_primary_term":1}% 

Kibana

1. kibana起動

Kibanaを利用すると、Elasticsearchに対する操作や可視化が簡単になります。

kibanaを起動

kibana

kibanaにアクセスする。
http://localhost:5601

2. Dev Toolsでクエリ送信

Kibanaの「Dev Tools」で先ほど同様にElasticsearch APIを実行してみます。

GET /my_index/_search
{
  "query": {
    "match": { "name": "John" }
  }
}

kibanaのDev ToolsでAPI実行します。右上の緑ボタンをポチッと押します。200-OK出たら成功。

スクリーンショット 2024-12-09 11.08.56.png

複数インデックスを取得

値の追加

値の追加

POST /my_index2/_doc/
{
  "properties":{
    "channel_id": 4,
    "content": "トルトル"
  }
}

値の追加

POST /my_index3/_doc/
{
  "properties":{
    "channel_id": 1,
    "title": "赤毛のあん"
  }
}

値の追加

POST /my_index3/_doc/
{
  "properties":{
    "channel_id": 1,
    "title": "Tete"
  }
}

複数インデックスを指定して取得

GET /my_index2,my_index3/_search
{
  "query":{
    "bool":{
      "should": [
        { "wildcard": { "properties.title": "*" } },
        { "wildcard": { "properties.content": "*" } }
      ]
    }
  }
}

output

{
  "took" : 6,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my_index2",
        "_type" : "_doc",
        "_id" : "fZVhsJMBYYz9d7HPfLZH",
        "_score" : 1.0,
        "_source" : {
          "properties" : {
            "channel_id" : 4,
            "content" : "トルトル"
          }
        }
      },
      {
        "_index" : "my_index3",
        "_type" : "_doc",
        "_id" : "fpVjsJMBYYz9d7HPs7aQ",
        "_score" : 1.0,
        "_source" : {
          "properties" : {
            "channel_id" : 1,
            "title" : "赤毛のあん"
          }
        }
      },
      {
        "_index" : "my_index3",
        "_type" : "_doc",
        "_id" : "f5VksJMBYYz9d7HPl7br",
        "_score" : 1.0,
        "_source" : {
          "properties" : {
            "channel_id" : 1,
            "title" : "Tete"
          }
        }
      }
    ]
  }
}

その他

全てを取り出す

GET /_search
{
    "query": {
        "match_all": {}
    }
}

インデックスで絞り込み

GET /my_index/_search
{
    "query": {
        "match_all": {}
    }
}

参考

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?