Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
elasticsearch で作るランキング
Search
iwagami
June 27, 2016
Programming
2.4k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
elasticsearch で作るランキング
iwagami
June 27, 2016
More Decks by iwagami
See All by iwagami
GCP_WORKSHOP_1_GCE
iwag
0
360
Introduction of finagle
iwag
1
170
Introduction of JAVA8
iwag
0
400
cocos2d-jsで雑に作るブラウザゲーム
iwag
0
270
Rewrite in Finagle
iwag
4
1.4k
Chisel RISC-V introduction
iwag
1
3.1k
Other Decks in Programming
See All in Programming
Welcome to the "Parametricity" 🏙️ − Generic だけど Specific な世界 −
guvalif
1
120
そのテスト、説明できますか?~LWテスト戦略FW~のご紹介
nakahara
0
200
トークンをケチるな、設計しろ:GitHub Copilotを賢く使うコンテキスト戦略
ochtum
0
300
【やさしく解説 設計編 #0】DDDのコード、読めるのに分からない人へ
panda728
PRO
1
230
任せる範囲はこう広がった / How the Scope of AI Delegation Has Expanded
nrslib
1
240
なぜ関数型プログラミングで「型」と「証明」が語られるのか #fp_matsuri
kajitack
3
640
初めてのKubernetes 本番運用でハマった話
oku053
0
120
Go1.27で導入されるジェネリクスメソッドでできること
mackee
0
250
エンジニア向け会社紹介/Findy Company Profile
findyinc
6
360k
はてなアカウント基盤 State of the Union
cockscomb
1
1.3k
例外の正しい扱い方 そのエラー try-catchして大丈夫?
jinwatanabe
0
340
技術記事、 専門家としてのプログラマ、 言語化
mizchi
14
7.3k
Featured
See All Featured
Utilizing Notion as your number one productivity tool
mfonobong
4
350
The agentic SEO stack - context over prompts
schlessera
0
840
Measuring & Analyzing Core Web Vitals
bluesmoon
9
880
Optimising Largest Contentful Paint
csswizardry
37
3.8k
The Invisible Side of Design
smashingmag
301
52k
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
760
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
350
30 Presentation Tips
portentint
PRO
1
340
Building Adaptive Systems
keathley
44
3.1k
Designing for Performance
lara
611
70k
Optimizing for Happiness
mojombo
378
71k
Transcript
elasticsearchで ランキングを作ろう iwag 2016/06/27 elasticsearch勉強会 #16
誰よ • iwag • Scala/finagler/elasticsearch
スクリプト • 検索、更新、取得時にスクリプト(groovy 等) でな んでもできる • Scripting • 言語:groovyなど
◦ Ruby っぽい ◦ 他にも lucence script, native(Java) ◦ import して Java の関数呼んだりできる
更新 • insert, update ◦ リンク • ドキュメントの値(ctx._source) を読み書き加工
検索 • スコア計算 ◦ ドキュメントの値を読んでスコアを算出する • スクリプト付きフィルタ • 値の取得(スクリプトフィールド) ◦
ランキング
ランキングとは • 現在時刻から24時間のお気に入り増減数の順 • 1時間ごとに更新
作り方:例 • apache ログを時間で aggregation ◦ 重い… • バッチで1時間ごとに計算 ◦
秘伝の技術になりがち • 1時間ごとの増減値(配列)をドキュメントに持たせ る (今日のお話) •
データ構造 • お気に入り数の配列とその時刻(時)の配列 time_array: [ 160624T16, 160624T15, 160624T10, 160624T08 ...
] fav_array: [160624T16's fav数, 160624T15's fav数, 160624T10's fav数, …, 160624T08's fav数, ... ]
スクリプトと検索クエリ fav_array=doc['fav_array'].values; time_array=doc['time_array'].values; // from, to はパラメータで来ます。 from〜to to =
new DateTime().getMillis; from = to - 24時間前; sum = 0; for (i=0; i<fav_array.length; i++) { if (from < time_array[i] && time_array[i] < to) sum += fav_array[i]; } return sum; { "fuction_score":{ "query":{ "term”: { “category":”ゲーム” }, "functions":{ "script_score":{ "script":"fav_array=doc['fav_array']. values; ... " } } } } }
ヒント
スクリプトめっっっっちゃ重い • フィールド >>> プラグイン(Native) >>>>>>>>>>>>> スクリプト(groovy) • プラグイン ◦
結構簡単に作れる ◦ 更新に再起動がいるので今回は断念
フィルタを使ってスクリプト実行を減らす • デフォルト ◦ 全ドキュメントに対して スクリプトが実行され る→重い • フィルタ ◦
script_scoreにはフィ ルタを付けれる ◦ フィルタされた要素だ けスクリプトを実行す "fuction_score":{ "functions":{ "filter":{ "exists":"fav_array" }, "script_score":{ "script":"fav_array=doc['fav_array'].values; ... " } } }
スクリプト中では絶対 doc を使う • doc は fielddata 、ヒープにキャッシュされる ◦ 設定で制御できるらしいがデフォルトで使っている
• _fields, _source も使えるがインデックスから読ん でくるので遅い ◦ ドキュメントでも使うなって言ってる • ただ doc は制限多い ◦ オブジェクトが使えない、number or string のみ
更新
前提 • クリック時にスクリプト付き(+1) updateが飛ぶ
更新のスクリプト if(ctx._source['fav_array']==null || ctx._source['time_array']) = ... now = new DateTime().getMillis()/3600;
if (ctx._source.time_array[0] == now) { ctx._source.fav_array[0] += 1; } else { ctx._source.time_array.add(0, now); ctx._source.fav_array[0].add(0, 1); }
ヒント
コンフリクトを気にしない • ESはドキュメント単位でのロック • 正直大雑把だけどめちゃくちゃ楽… • コンフリクトした場合→ retry • バズった時大変なことにならないのか?
◦ ESへのメッセージはすべてその前にキューで直列化して いるのでほぼコンフリクトは発生しない ◦ 遅延は発生するが。。。
パフォーマンス • 更新時のスクリプトはパフォーマンスを気にしなく て良い ◦ せいぜい更新1回 • スクリプトの計算よりインデックスへのストアのほ うが重い
古いデータどうするのか? • バッチで48時間以上前のデータは消している • クエリ付き update があるとうれしいなという要望
デバッグどうするのか • スクリプトでloggerが使える ◦ https://www.elastic.co/blog/elasticsearch-logging-secrets • デバッグ用フィールド ◦ スクリプト中で ctx._source.debug
= “....”;
まとめ
まとめ • elasticsearch のスクリプトの話をしました • ランキングの作り方を紹介しました • スクリプトを活用する上でのヒントを紹介しました
おまけ
ホットエントリ(みたいなもの) もつくれます • スクリプト付きフィルタを使う • 例 ◦ 直近1時間のお気入り数が10以上でフィルタ ◦ 適当に並べる
• あってるかは不明…