Elasticsearch CRUDを試してみる


Elasticsearch設定していない場合は???
設定するには、AWS EC 2にElasticsearchを取り付けるを参照してください.Elasticsearchを使用する場合は、IndexTypeDocumentの順に入力して使用するのが一般的であり、実習と並行する場合はIndexDocumentTypeの方向で学習することが望ましい.

1.Indexで遊ぶ


1)索引の作成

curl -XPUT http://[Elasticsearch Server IP]:9200/[INDEX NAME]?pretty上記のコマンドを使用して、INDEXを作成できます.
次のコマンドを入力して、sportという名前のINDEXを作成します.curl -XPUT http://localhost:9200/sport?pretty

2)クエリー索引

curl -XGET http://[Elasticsearch Server IP]:9200/[INDEX NAME]?pretty上記のコマンドを使用して、INDEXを問い合わせることができます.
次のコマンドを入力して、sportという名前のINDEXを検索してください.curl -XGET http://localhost:9200/sport?prettyprettyは、結果を出力するときにjson形式の結果をより美しくするオプションです.

3)索引の削除

curl -XDELETE http://[Elasticsearch Server IP]:9200/[INDEX NAME] INDEXを削除するには、上記のコマンドを使用します.
次のコマンドを入力して、sportという名前のINDEXを削除します.curl -XDELETE http://localhost:9200/sport

2.Documentをする


1)Documentの作成


(1)直接入力データ


DocumentおよびIndexが作成されていない場合でも、Typeの追加時にDocumentおよびIndexを指定できます.Type上記のコマンドを使用して、curl -XPOST http://[Elasticsearch Server IP]:9200/[INDEX NAME]/[TYPE NAME]/[ID]/ -d [JSON DATA] -H 'Content-Type: application/json'を指定したDocumentIndexに追加できます.
次の情報を入力して、Typeを直接生成します.Document

(2)JSONファイル入力

// single_data.json (데이터가 하나만 든 json 파일)
{"user": "messi", "score": 62}
curl -XPOST http://localhost:9200/sport/soccer/1/ -d '{"user": "jwpark", "score": 15}' -H 'Content-Type: application/json'Q)同じコマンドで複数のデータを実行するjsonファイルはどうなりますか?
// multi_data.json (여러개의 json 데이터가 들어있는 파일)
{"user": "park", "score": 12}
{"user": "kim", "score": 24}
{"user": "choi", "score": 36}
curl -XPOST http://localhost:9200/sport/soccer/2 -d @single_data.json -H 'Content-Type: application/json'
はい、エラーが発生しました.解析に失敗した可能性があります.
一度に大量のJSONデータを入力する方法をcurl -XPOST http://localhost:9200/sport/soccer/3?pretty -d @multi_data.json -H 'Content-Type: application/json'と呼ぶ.

(3)BULKでJSONファイルを入力する


まず、大量のデータを入れると、BULKINDEXとは異なる場合があります.
次のようにファイルのフォーマットを変更してください.
// formated_multi_data.json (여러개의 json 데이터가 각 index, type이 지정된채로 들어있는 파일)
{"index": { "_index": "sport", "_type": "soccer", "_id": 3 } }
{"user": "park", "score": 12}
{"index": { "_index": "sport", "_type": "soccer", "_id": 4 } }
{"user": "kim", "score": 24}
{"index": { "_index": "sport", "_type": "soccer", "_id": 5 } }
{"user": "choi", "score": 36}
TYPE上記のコマンドでフォーマットされたmulti data.jsonファイルの大量のJSONデータを一度にcurl -XPOST http://localhost:9200/_bulk?pretty --data-binary @formated_multi_data.json -H 'Content-Type: application/json'に配置できます.

では、これまでに追加されたElasticsearchを見てみましょう.

2)Documentの検索


次のコマンドは、sportDocumentのsoccerINDEXの完全TYPEをクエリーするために使用されます.Document確認すると、これまでに入っていた5つのcurl -XGET http://localhost:9200/sport/soccer/_search?prettyが正常であることがわかります.
Documentには3つの検索方法があります.

(1)URI方式


検索は、HTTP GET要求を使用してKey:Valueの形式で検索条件のパラメータを追加することによって行われる.
では、検索条件を追加し、クエリーscoreは24です.Document

(2)要求主体方式


コマンド本文にJSON形式の検索条件を追加することで検索を行います.curl -XGET http://localhost:9200/sport/soccer/_search?q=score:24メソッドには多くのオプションがあります.リンクを参照してください.
curl -XGET http://localhost:9200/sport/soccer/_search -d '
{"query":
  {"term":
    {"score": 24}
  }
}' -H 'Content-Type: application/json'

(3)Query DSL方式


JSON構造に基づいて提供します.複数のクエリーを組み合わせたり、クエリーの結果を再検索したりするなど、強力な検索ができます.Request bodyの詳細と使用方法については、リンクを参照してください.

3)Documentの修正

curl -XPOST http://localhost:9200/sport/soccer/1/_update -d '
{
  "score": 100
}' -H 'Content-Type: application/json'
Query DSLscoreを変更してクエリーすると、scoreが100に正しく変更されたことを確認できます.

4)Documentの削除

Document検索curl -XDELETE http://localhost:9200/sport/soccer/1を削除して検索しないと、Documentと表示されます.

3.遊びのタイプ

found: false 6.xバージョン以降ではマルチタイプはサポートされていませんElasticsearch : Index = 1:1Type上のElasticsearchはRDBMS上のSchemaと似ている.
入力するType(データ)のタイプDocumentを定義します.
これらのTypeで日付形式のpropertyを定義した場合、TypeまたはKibanaを接続すると、グラフデータは時間が経つにつれて表示されやすくなります.

1)作成タイプ


(1)JSONファイルの使用

// soccer_mapping.json
{
  "soccer": {
    "properties": {
      "user": { "type": "text" },
      "score": { "type": "long" }
    }
  }
}
既存のGrafanaタイプに従って入れる必要があります.
ここにいるよtype=integerに設定するとparse exceptionが発生し、Documentに正常にマッピングできません.Type上記のコマンドを使用して、マッピングをサッカーcurl -XPUT http://localhost:9200/sport/soccer/_mapping -d @soccer_mapping.json -H 'Content-Type: application/json'に追加します.
次に、ceサッカーTypeに正常に設定されているかどうかを確認します.Typeタイプは正常に設定されています.

(2)直接データ作成


soccer_mapping.jsonファイルの内容を入力します.
curl -XPUT http://localhost:9200/sport/soccer/_mapping -d '
{
  "soccer": {
    "properties": {
      "user": { "type": "text" },
      "score": { "type": "long" }
    }
  }
}' -H 'Content-Type: application/json'

参考資料


https://www.inflearn.com/course/elk-%EC%8A%A4%ED%83%9D-%EB%8D%B0%EC%9D%B4%ED%84%B0-%EB%B6%84%EC%84%9D/dashboard
https://yhmane.tistory.com/166