SlideShare a Scribd company logo
Elasticsearch

基本動作まとめ
@tikeda_meu
目次
• Elasticsearchについて
➢ Index構成
➢ NodeとCluster
➢ Primary shards
➢ Replica shards
• API概要
➢ cat APIs
➢ The REST API
➢ Search APIs
• 格納
➢ 格納の流れ
➢ 格納のrouting
• 検索
➢ 検索の流れ
➢ 検索のrouting
Elasticsearchについて
• スケーラブルなオープンソースフルテキスト検索・分析エンジン
キーワード : RESTful, 分散型検索・分析エンジン
• ソフトバンク
➢トランザクション監視や異常検知
• Shopback
➢スマートなショッピングサイト
• Docker, GitHubなど...
特徴
具体的なサービス
Index構成
index
type
document
document
document
Elasticsearch RDB
document record
field column
type table
index database
field
• Document (doc)
➢ データの実体 (JSON形式)
• Field
➢ データの属性
• Type
➢ documentの型
➢ RDBのtable
• Index(論理的な概念)
➢ documentの集合
https://www.elastic.co/guide/en/elasticsearch/reference/current/_basic_concepts.html
対応表
NodeとCluster
• Node
➢起動したElasticsearchの実体( サーバ)
➢いくつかの種類が存在:
✓ Master node … indexの削除や作成, shardの割当操作を担当
✓ Data node … データを保持し, 検索などの操作を担当
✓ Ingest node … documentを格納する際の前処理を担当
✓ Tribe node … 複数clusterを用いた検索等を担当
• Cluster
➢複数nodeの集まり
https://www.elastic.co/guide/en/elasticsearch/reference/current/_basic_concepts.html
Primary shards
• Primary shards (Shards)
➢indexを細分化した単位(物理的な概念)
• 目的
➢ディスクに乗り切らない大規模なindexを分散させるため
➢検索requestに対するレスポンス性能を向上させるため
index
Primary shard 0
Primary shard 1
Primary shard 2
• 例. node数 = 1, primary shard数 = 3の場合
https://www.elastic.co/guide/en/elasticsearch/reference/current/_basic_concepts.html
Replica shards
• Replica shards (Replicas)
➢primary shardのコピー
✓ 冗長化が可能
• 目的
➢高可用性のため
➢検索スループットのスケールアウトを可能にするため
• 例. node数 = 3, primary shard数 = 3, replica数 = 2 の場合
https://www.elastic.co/guide/en/elasticsearch/reference/current/_basic_concepts.html
API 概要
API 概要
• cat APIs:clusterやnode, indicesに関する情報をテキスト出力
https://www.elastic.co/guide/en/elasticsearch/reference/6.2/cat.html
• Search APIs : documentを検索
https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search.html
• Cluster APIs : clusterやnodeの制御や状態の出力
https://www.elastic.co/guide/en/elasticsearch/reference/6.2/cluster.html
• Indices APIs : indicesやaliases, mapping, index templatesの制御
https://www.elastic.co/guide/en/elasticsearch/reference/6.2/indices.html
• Document APIs : document関連の制御
https://www.elastic.co/guide/en/elasticsearch/reference/6.2/docs.html
cat APIs
• cat API : clusterやnode, indicesに関する情報をテキスト出力
• 例. Master nodeのIDや割り当てられたIPアドレス, ノード名を出力
➢GET /_cat/master?v
• ?以下のオプション
➢v : verbose
➢h : headers指定
➢s : sort
➢format : レスポンスにjson, smile, yaml, cbor形式を選択
➢pretty : 整形
➢bytes : 数値でレスポンス
➢help
&でつなげることで、複数指定できる
https://www.elastic.co/guide/en/elasticsearch/reference/6.2/cat.html
The REST API
• URIで一意に表現可能
➢リクエストの基本形 : <REST Verb> /<Index>/<Type>/<ID>
• 操作のセット
➢GET
✓ 例. GET /_cat/master?v
➢PUT
✓ 例. PUT /customer/_doc/1 {“name”: “Tomoya Ikeda”}
➢DELETE
✓ 例. DELETE /customer
➢UPDATE
✓ 例. POST /customer/_doc/1/_update
{“doc” : {“name”: “Hoge Huga”, “age” : “40”}}
• ステートレス
• 結果はJSON形式で返却 (cat APIのみはテキスト形式で返却)
https://www.elastic.co/guide/en/elasticsearch/reference/6.2/_index_and_query_a_document.html
Search APIs
• Search APIs : query検索を実行して、マッチした検索結果を返すAPI
• Search APIsの中身は以下のようなAPIが存在:
➢ Search Shards API : 検索requestが実行されるindicesとshardsを取得
➢ Multi Search API : いくつかの検索requestをまとめて実行
➢ Count API : queryのマッチ数を取得
➢ Validate API : 実行せずにコストがかかる恐れのあるqueryを検証
➢ Explain API : queryと特定のdocumentのスコアの説明を表示
➢ Profile API : 検索requestの個々の要素が実行されるタイミングを詳述
➢ Field Capabilities API : 複数index間でのfieldの能力を検索
➢ Ranking Evaluation API : ランク付けされた検索結果の質を評価
https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search.html
格納
格納
• POSTによる格納
➢例. POST /customer/_doc –d ‘{“name” : Post Example}’
• PUTによる格納
➢例. PUT /customer/_doc/1 –d ‘{“name” : Put Example}’
• コマンド
例. $ curl –d “Content-Type: application/x-ndjson” –XPUT “http://172.17.0.2:9200/twitter/tweet/1?pretty” --data-
binary “@request.json”
クライアント側のレスポンス
{
“name” : “Tomoya Ikeda”
}
{
“_index” : “customer”,
“_type” : “_doc”,
...
“result” : “created”,
...
}
request.json
https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html
格納の流れ (1/7)
Replica 0
Primary 1
Replica 2
Primary 0
Replica 1
Replica 2
Replica 0
Replica 1
Primary 2
Node 0 Node 1 Node 2
coordinating nodeCluster
例えば Node 0 に対して doc 0 の格納を要求
この時, Node 0 がcoordinating nodeとなる
doc 0
Cluster
Node 0
(coordinating node)
Node 1 Node 2
格納の流れ (2/7)
Primary 0
Replica 1
Replica 2
Node 0 は以下の式でハッシュ値 (デフォルト: DjbHashFunction) を算出
し, documentを配置するprimary shardを選択:
登録先のshard番号 = documentのID % primary shard数
Replica 0
Primary 1
Replica 2
Replica 0
Replica 1
Primary 2
doc 0
Cluster
Node 0
(coordinating node)
Node 1 Node 2
格納の流れ (3/7)
Primary 0
Replica 1
Replica 2
登録するprimary shardが別nodeにある場合, doc 0 を送信
Replica 0
Primary 1
Replica 2
Replica 0
Replica 1
Primary 2doc 0
doc 0
Cluster
Node 0
(coordinating node)
Node 1 Node 2
格納の流れ (4/7)
Primary 0
Replica 1
Replica 2
Node 2がreplica shardsへ格納
request
Replica 0
Primary 1
Replica 2
Replica 0
Replica 1
Primary 2doc 0doc 0doc 0
Cluster
Node 0
(coordinating node)
Node 1 Node 2
格納の流れ (5/7)
Primary 0
Replica 1
Replica 2
Node 0, 1から格納完了のレスポンス
Replica 0
Primary 1
Replica 2
Replica 0
Replica 1
Primary 2doc 0doc 0doc 0
Cluster
Node 0
(coordinating node)
Node 1 Node 2
格納の流れ (6/7)
Primary 0
Replica 1
Replica 2
Coordinating nodeへ格納完了のレスポンスを返却
Replica 0
Primary 1
Replica 2
Replica 0
Replica 1
Primary 2doc 0doc 0doc 0
Cluster
Node 0
(coordinating node)
Node 1 Node 2
格納の流れ (7/7)
Primary 0
Replica 1
Replica 2
格納完了のレスポンスを返却
Replica 0
Primary 1
Replica 2
Replica 0
Replica 1
Primary 2doc 0doc 0doc 0
格納のrouting
• Routing
➢設定した値をIDの代わりとして格納
➢例. ID=1のdocを”user1”という文字列でroutingする時 :
PUT my_index/_doc/1?routing=user1&refresh=true
{
“title” : “This is a document”
}
値を取得する時 :
GET my_index/_search
{
“query” : {
“terms” : {
“_routing” : [ “user1“ ]
}
}
}
https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-routing-field.html
検索
検索
• URIに直接パラメータとして入力
➢例. GET /customer/_search?q=*&sort=age:asc
• requestBodyに記述
➢例. GET /customer/_search
{
“query” : { “match_all” : {} },
“sort” : [
{ “age” : “asc” }
]
}
• twitter と user という2種類のtypeを指定して検索
➢例. GET /twitter/tweet,user/_search?q=user:kimchy
• kimchyとelasticsearchという2種類のindexを指定し, tagつきtweetsを検索
➢例. GET /kimchy,elasticsearch/_search?q=tag:wow
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html
検索の流れ (1/4)
Replica 0
Primary 1
Replica 2
Primary 0
Replica 1
Replica 2
Replica 0
Replica 1
Primary 2
検索requestの受信nodeがcoordinating nodeとなる
Node 0 Node 1 Node 2
coordinating nodeCluster
request
Cluster
Node 0
(coordinating node)
Node 1 Node 2
検索の流れ (2/4)
request
Primary 0
Replica 1
Replica 2
全nodeに対して検索を依頼
Replica 0
Primary 1
Replica 2
Replica 0
Replica 1
Primary 2
Replica 0
Primary 1
Replica 2
Replica 0
Replica 1
Primary 2
Cluster
Node 0
(coordinating node)
Node 1 Node 2
検索の流れ (3/4)
request
Primary 0
Replica 1
Replica 2
検索結果を集約
Node 0
(coordinating node)
Replica 0
Primary 1
Replica 2
Replica 0
Replica 1
Primary 2
Cluster
検索の流れ (4/4)
request
Primary 0
Replica 1
Replica 2
検索結果をJSON形式で返却
Node 1 Node 2
検索のrouting
• Routing
➢対象のdocumentがあるshardに対してのみ検索
✓ 検索request処理時の負荷軽減
➢例. user1, user2からtitleがマッチするqueryのみGET :
GET my_index/_search?routing=user1, user2
{
“query” : {
“match” : {
“title” : “document”
}
}
}
Cluster
Node 0 Node 1 Node 2
request
Primary 0
Replica 1
Replica 2
Replica 0
Primary 1
Replica 2
Replica 0
Replica 1
Primary 2
https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-routing-field.html

More Related Content

PDF
Elasticsearch入門 pyfes 201207
PDF
Elasticsearch Authプラグインでアクセスコントロール
PPTX
Solr 4.0 の主な機能
PDF
Elasticsearchプラグインの作り方
PPT
Mongodb
PDF
はじめてのMongoDB
PPTX
Solr6 の紹介(第18回 Solr勉強会 資料) (2016年6月10日)
PDF
Elasticsearch at CrowdWorks
Elasticsearch入門 pyfes 201207
Elasticsearch Authプラグインでアクセスコントロール
Solr 4.0 の主な機能
Elasticsearchプラグインの作り方
Mongodb
はじめてのMongoDB
Solr6 の紹介(第18回 Solr勉強会 資料) (2016年6月10日)
Elasticsearch at CrowdWorks

What's hot (19)

PPTX
メタプログラミングRuby勉強会#7(fluentプラグイン)
PDF
Elasticsearchで作る形態素解析サーバ
PPTX
HTML5最新動向
PPTX
HTML5&API総まくり
PPTX
Active Directoryデータのプロパティ出力(Output Properties)
PPTX
.NETからActive Directoryデータにアクセス ~ユーザ情報の取得と表示~
PDF
Sc2009autumn s2robot
PDF
コード読経会報告書
PDF
elasticsearchソースコードを読みはじめてみた
PDF
MongoDBざっくり解説
PPTX
FIWAREシステム内の短期履歴の管理
PPTX
BPStudy32 CouchDB 再入門
PDF
ESFluteによるElasticsearchでのO/Rマッパーを用いた開発
PPTX
.NETからActive Directoryにアクセス
PPTX
.NETからActive Directoryデータにアクセス ~プリンタ情報の取得と表示~
PDF
ElasticsearchとTasteプラグインで作るレコメンドシステム
PDF
MongoDB勉強会資料
PDF
PPTX
Ruby on Rails on MySQL チューニング入門
メタプログラミングRuby勉強会#7(fluentプラグイン)
Elasticsearchで作る形態素解析サーバ
HTML5最新動向
HTML5&API総まくり
Active Directoryデータのプロパティ出力(Output Properties)
.NETからActive Directoryデータにアクセス ~ユーザ情報の取得と表示~
Sc2009autumn s2robot
コード読経会報告書
elasticsearchソースコードを読みはじめてみた
MongoDBざっくり解説
FIWAREシステム内の短期履歴の管理
BPStudy32 CouchDB 再入門
ESFluteによるElasticsearchでのO/Rマッパーを用いた開発
.NETからActive Directoryにアクセス
.NETからActive Directoryデータにアクセス ~プリンタ情報の取得と表示~
ElasticsearchとTasteプラグインで作るレコメンドシステム
MongoDB勉強会資料
Ruby on Rails on MySQL チューニング入門
Ad

Similar to Elasticsearchの基本動作まとめ (20)

PDF
Elastic circle ci-co-webinar-20210127
PPTX
はじめてのElasticsearchクラスタ
PDF
Renewed using elasticsearchonaspnet-core5
PDF
Integrating elasticsearch with asp dot net core
PDF
MySQLと組み合わせて始める全文検索プロダクト"elasticsearch"
PDF
Elastic Stackの紹介とOpenStackでの活用事例(Searchlightなど) - OpenStack最新情報セミナー 2016年5月
PDF
ElasticSearch勉強会 第6回
PDF
Learn, build, and scale with elastic - realizing great programming experience...
PDF
実践!Elasticsearch + Sudachi を用いた全文検索エンジン
PDF
モノタロウの1900万商品を検索する Elasticsearch構築運用事例(2022-10-26 第50回Elasticsearch 勉強会発表資料)
PDF
Elastic searchをrailsから使ってみた
PDF
Elastic 7.13-new-features-20210624
PDF
Building asp.net core blazor and elasticsearch elasticsearch using visual stu...
PPTX
Elasticsearch as a Distributed System
KEY
はじめてのCouch db
PDF
Building modernapplicationwithelasiccloud
PDF
20200324 ms open-tech-elastic
PDF
What's New in the Elastic 8.4 Release
PDF
JJUG CCC リクルートの Java に対する取り組み
PDF
aws blackbelt amazon elasticsearch service
Elastic circle ci-co-webinar-20210127
はじめてのElasticsearchクラスタ
Renewed using elasticsearchonaspnet-core5
Integrating elasticsearch with asp dot net core
MySQLと組み合わせて始める全文検索プロダクト"elasticsearch"
Elastic Stackの紹介とOpenStackでの活用事例(Searchlightなど) - OpenStack最新情報セミナー 2016年5月
ElasticSearch勉強会 第6回
Learn, build, and scale with elastic - realizing great programming experience...
実践!Elasticsearch + Sudachi を用いた全文検索エンジン
モノタロウの1900万商品を検索する Elasticsearch構築運用事例(2022-10-26 第50回Elasticsearch 勉強会発表資料)
Elastic searchをrailsから使ってみた
Elastic 7.13-new-features-20210624
Building asp.net core blazor and elasticsearch elasticsearch using visual stu...
Elasticsearch as a Distributed System
はじめてのCouch db
Building modernapplicationwithelasiccloud
20200324 ms open-tech-elastic
What's New in the Elastic 8.4 Release
JJUG CCC リクルートの Java に対する取り組み
aws blackbelt amazon elasticsearch service
Ad

Elasticsearchの基本動作まとめ

  • 2. 目次 • Elasticsearchについて ➢ Index構成 ➢ NodeとCluster ➢ Primary shards ➢ Replica shards • API概要 ➢ cat APIs ➢ The REST API ➢ Search APIs • 格納 ➢ 格納の流れ ➢ 格納のrouting • 検索 ➢ 検索の流れ ➢ 検索のrouting
  • 3. Elasticsearchについて • スケーラブルなオープンソースフルテキスト検索・分析エンジン キーワード : RESTful, 分散型検索・分析エンジン • ソフトバンク ➢トランザクション監視や異常検知 • Shopback ➢スマートなショッピングサイト • Docker, GitHubなど... 特徴 具体的なサービス
  • 4. Index構成 index type document document document Elasticsearch RDB document record field column type table index database field • Document (doc) ➢ データの実体 (JSON形式) • Field ➢ データの属性 • Type ➢ documentの型 ➢ RDBのtable • Index(論理的な概念) ➢ documentの集合 https://www.elastic.co/guide/en/elasticsearch/reference/current/_basic_concepts.html 対応表
  • 5. NodeとCluster • Node ➢起動したElasticsearchの実体( サーバ) ➢いくつかの種類が存在: ✓ Master node … indexの削除や作成, shardの割当操作を担当 ✓ Data node … データを保持し, 検索などの操作を担当 ✓ Ingest node … documentを格納する際の前処理を担当 ✓ Tribe node … 複数clusterを用いた検索等を担当 • Cluster ➢複数nodeの集まり https://www.elastic.co/guide/en/elasticsearch/reference/current/_basic_concepts.html
  • 6. Primary shards • Primary shards (Shards) ➢indexを細分化した単位(物理的な概念) • 目的 ➢ディスクに乗り切らない大規模なindexを分散させるため ➢検索requestに対するレスポンス性能を向上させるため index Primary shard 0 Primary shard 1 Primary shard 2 • 例. node数 = 1, primary shard数 = 3の場合 https://www.elastic.co/guide/en/elasticsearch/reference/current/_basic_concepts.html
  • 7. Replica shards • Replica shards (Replicas) ➢primary shardのコピー ✓ 冗長化が可能 • 目的 ➢高可用性のため ➢検索スループットのスケールアウトを可能にするため • 例. node数 = 3, primary shard数 = 3, replica数 = 2 の場合 https://www.elastic.co/guide/en/elasticsearch/reference/current/_basic_concepts.html
  • 9. API 概要 • cat APIs:clusterやnode, indicesに関する情報をテキスト出力 https://www.elastic.co/guide/en/elasticsearch/reference/6.2/cat.html • Search APIs : documentを検索 https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search.html • Cluster APIs : clusterやnodeの制御や状態の出力 https://www.elastic.co/guide/en/elasticsearch/reference/6.2/cluster.html • Indices APIs : indicesやaliases, mapping, index templatesの制御 https://www.elastic.co/guide/en/elasticsearch/reference/6.2/indices.html • Document APIs : document関連の制御 https://www.elastic.co/guide/en/elasticsearch/reference/6.2/docs.html
  • 10. cat APIs • cat API : clusterやnode, indicesに関する情報をテキスト出力 • 例. Master nodeのIDや割り当てられたIPアドレス, ノード名を出力 ➢GET /_cat/master?v • ?以下のオプション ➢v : verbose ➢h : headers指定 ➢s : sort ➢format : レスポンスにjson, smile, yaml, cbor形式を選択 ➢pretty : 整形 ➢bytes : 数値でレスポンス ➢help &でつなげることで、複数指定できる https://www.elastic.co/guide/en/elasticsearch/reference/6.2/cat.html
  • 11. The REST API • URIで一意に表現可能 ➢リクエストの基本形 : <REST Verb> /<Index>/<Type>/<ID> • 操作のセット ➢GET ✓ 例. GET /_cat/master?v ➢PUT ✓ 例. PUT /customer/_doc/1 {“name”: “Tomoya Ikeda”} ➢DELETE ✓ 例. DELETE /customer ➢UPDATE ✓ 例. POST /customer/_doc/1/_update {“doc” : {“name”: “Hoge Huga”, “age” : “40”}} • ステートレス • 結果はJSON形式で返却 (cat APIのみはテキスト形式で返却) https://www.elastic.co/guide/en/elasticsearch/reference/6.2/_index_and_query_a_document.html
  • 12. Search APIs • Search APIs : query検索を実行して、マッチした検索結果を返すAPI • Search APIsの中身は以下のようなAPIが存在: ➢ Search Shards API : 検索requestが実行されるindicesとshardsを取得 ➢ Multi Search API : いくつかの検索requestをまとめて実行 ➢ Count API : queryのマッチ数を取得 ➢ Validate API : 実行せずにコストがかかる恐れのあるqueryを検証 ➢ Explain API : queryと特定のdocumentのスコアの説明を表示 ➢ Profile API : 検索requestの個々の要素が実行されるタイミングを詳述 ➢ Field Capabilities API : 複数index間でのfieldの能力を検索 ➢ Ranking Evaluation API : ランク付けされた検索結果の質を評価 https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search.html
  • 14. 格納 • POSTによる格納 ➢例. POST /customer/_doc –d ‘{“name” : Post Example}’ • PUTによる格納 ➢例. PUT /customer/_doc/1 –d ‘{“name” : Put Example}’ • コマンド 例. $ curl –d “Content-Type: application/x-ndjson” –XPUT “http://172.17.0.2:9200/twitter/tweet/1?pretty” --data- binary “@request.json” クライアント側のレスポンス { “name” : “Tomoya Ikeda” } { “_index” : “customer”, “_type” : “_doc”, ... “result” : “created”, ... } request.json https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html
  • 15. 格納の流れ (1/7) Replica 0 Primary 1 Replica 2 Primary 0 Replica 1 Replica 2 Replica 0 Replica 1 Primary 2 Node 0 Node 1 Node 2 coordinating nodeCluster 例えば Node 0 に対して doc 0 の格納を要求 この時, Node 0 がcoordinating nodeとなる doc 0
  • 16. Cluster Node 0 (coordinating node) Node 1 Node 2 格納の流れ (2/7) Primary 0 Replica 1 Replica 2 Node 0 は以下の式でハッシュ値 (デフォルト: DjbHashFunction) を算出 し, documentを配置するprimary shardを選択: 登録先のshard番号 = documentのID % primary shard数 Replica 0 Primary 1 Replica 2 Replica 0 Replica 1 Primary 2 doc 0
  • 17. Cluster Node 0 (coordinating node) Node 1 Node 2 格納の流れ (3/7) Primary 0 Replica 1 Replica 2 登録するprimary shardが別nodeにある場合, doc 0 を送信 Replica 0 Primary 1 Replica 2 Replica 0 Replica 1 Primary 2doc 0 doc 0
  • 18. Cluster Node 0 (coordinating node) Node 1 Node 2 格納の流れ (4/7) Primary 0 Replica 1 Replica 2 Node 2がreplica shardsへ格納 request Replica 0 Primary 1 Replica 2 Replica 0 Replica 1 Primary 2doc 0doc 0doc 0
  • 19. Cluster Node 0 (coordinating node) Node 1 Node 2 格納の流れ (5/7) Primary 0 Replica 1 Replica 2 Node 0, 1から格納完了のレスポンス Replica 0 Primary 1 Replica 2 Replica 0 Replica 1 Primary 2doc 0doc 0doc 0
  • 20. Cluster Node 0 (coordinating node) Node 1 Node 2 格納の流れ (6/7) Primary 0 Replica 1 Replica 2 Coordinating nodeへ格納完了のレスポンスを返却 Replica 0 Primary 1 Replica 2 Replica 0 Replica 1 Primary 2doc 0doc 0doc 0
  • 21. Cluster Node 0 (coordinating node) Node 1 Node 2 格納の流れ (7/7) Primary 0 Replica 1 Replica 2 格納完了のレスポンスを返却 Replica 0 Primary 1 Replica 2 Replica 0 Replica 1 Primary 2doc 0doc 0doc 0
  • 22. 格納のrouting • Routing ➢設定した値をIDの代わりとして格納 ➢例. ID=1のdocを”user1”という文字列でroutingする時 : PUT my_index/_doc/1?routing=user1&refresh=true { “title” : “This is a document” } 値を取得する時 : GET my_index/_search { “query” : { “terms” : { “_routing” : [ “user1“ ] } } } https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-routing-field.html
  • 24. 検索 • URIに直接パラメータとして入力 ➢例. GET /customer/_search?q=*&sort=age:asc • requestBodyに記述 ➢例. GET /customer/_search { “query” : { “match_all” : {} }, “sort” : [ { “age” : “asc” } ] } • twitter と user という2種類のtypeを指定して検索 ➢例. GET /twitter/tweet,user/_search?q=user:kimchy • kimchyとelasticsearchという2種類のindexを指定し, tagつきtweetsを検索 ➢例. GET /kimchy,elasticsearch/_search?q=tag:wow https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html
  • 25. 検索の流れ (1/4) Replica 0 Primary 1 Replica 2 Primary 0 Replica 1 Replica 2 Replica 0 Replica 1 Primary 2 検索requestの受信nodeがcoordinating nodeとなる Node 0 Node 1 Node 2 coordinating nodeCluster request
  • 26. Cluster Node 0 (coordinating node) Node 1 Node 2 検索の流れ (2/4) request Primary 0 Replica 1 Replica 2 全nodeに対して検索を依頼 Replica 0 Primary 1 Replica 2 Replica 0 Replica 1 Primary 2
  • 27. Replica 0 Primary 1 Replica 2 Replica 0 Replica 1 Primary 2 Cluster Node 0 (coordinating node) Node 1 Node 2 検索の流れ (3/4) request Primary 0 Replica 1 Replica 2 検索結果を集約
  • 28. Node 0 (coordinating node) Replica 0 Primary 1 Replica 2 Replica 0 Replica 1 Primary 2 Cluster 検索の流れ (4/4) request Primary 0 Replica 1 Replica 2 検索結果をJSON形式で返却 Node 1 Node 2
  • 29. 検索のrouting • Routing ➢対象のdocumentがあるshardに対してのみ検索 ✓ 検索request処理時の負荷軽減 ➢例. user1, user2からtitleがマッチするqueryのみGET : GET my_index/_search?routing=user1, user2 { “query” : { “match” : { “title” : “document” } } } Cluster Node 0 Node 1 Node 2 request Primary 0 Replica 1 Replica 2 Replica 0 Primary 1 Replica 2 Replica 0 Replica 1 Primary 2 https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-routing-field.html