Elasticsearch CRUDを試してみる
8198 ワード
Elasticsearch
設定していない場合は???設定するには、AWS EC 2にElasticsearchを取り付けるを参照してください.
Elasticsearch
を使用する場合は、Index
→Type
→Document
の順に入力して使用するのが一般的であり、実習と並行する場合はIndex
→Document
→Type
の方向で学習することが望ましい.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?pretty
prettyは、結果を出力するときに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'
を指定したDocument
のIndex
に追加できます.
次の情報を入力して、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ファイルを入力する
まず、大量のデータを入れると、BULK
、INDEX
とは異なる場合があります.
次のようにファイルのフォーマットを変更してください.// 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 DSL
scoreを変更してクエリーすると、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
Reference
この問題について(Elasticsearch CRUDを試してみる), 我々は、より多くの情報をここで見つけました
https://velog.io/@jwpark06/Elasticsearch-CRUD-해보기
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
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'
を指定したDocument
のIndex
に追加できます.次の情報を入力して、
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ファイルを入力する
まず、大量のデータを入れると、
BULK
、INDEX
とは異なる場合があります.次のようにファイルのフォーマットを変更してください.
// 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の検索
次のコマンドは、sport
Document
の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 DSL
scoreを変更してクエリーすると、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
Reference
この問題について(Elasticsearch CRUDを試してみる), 我々は、より多くの情報をここで見つけました
https://velog.io/@jwpark06/Elasticsearch-CRUD-해보기
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
// soccer_mapping.json
{
"soccer": {
"properties": {
"user": { "type": "text" },
"score": { "type": "long" }
}
}
}
curl -XPUT http://localhost:9200/sport/soccer/_mapping -d '
{
"soccer": {
"properties": {
"user": { "type": "text" },
"score": { "type": "long" }
}
}
}' -H 'Content-Type: application/json'
Reference
この問題について(Elasticsearch CRUDを試してみる), 我々は、より多くの情報をここで見つけました https://velog.io/@jwpark06/Elasticsearch-CRUD-해보기テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol