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
390
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
「AIで開発し、AIを届ける」をEvalでつなぐ 〜AIネイティブに始めるプロダクト開発の実践〜 / Connecting "Develop with AI, deliver AI" with Eval
rkaga
4
5.1k
その問い、本当に正しいですか?AI時代のエンジニアに必要な哲学と認知科学 / ai-philosophy-cognitive-science
minodriven
8
4.8k
Agentic UI
manfredsteyer
PRO
0
160
PHPで使える日時の表現と、その知り方 #frontend_phpcon_do
o0h
PRO
0
240
軽量Java基盤の設計 DIコンテナに頼らない、長期保守と1秒起動の実現 JJUG CCC 2026 Spring
macha64
0
520
AI時代の仕事技芸論 — ソフトウェア開発で「遊ぶように働く」職人的熟達のすすめ
kuranuki
2
680
Mujeres en SEO Summit 2026 - Greatest Disaster Hits en Web Performance
guaca
0
180
ECSアプリログをFireLensでコスト削減しようとしたけど諦めた話 in Fargate×Node.js
akihisaikeda
2
4.2k
並列実装の現場、2ヶ月間実務でAIを使い倒したAIもPCも私も限界が近い
ming_ayami
0
130
[2026年度第1回ORセミナー] 計画最適化ベンチャーと競技プログラミング人材
terryu16
0
260
3Dシーンの圧縮
fadis
1
770
RTSPクライアントを自作してみた話
simotin13
0
610
Featured
See All Featured
Fireside Chat
paigeccino
42
3.9k
The Curse of the Amulet
leimatthew05
1
13k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
580
WENDY [Excerpt]
tessaabrams
11
38k
How STYLIGHT went responsive
nonsquared
100
6.2k
The Art of Programming - Codeland 2020
erikaheidi
57
14k
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.5k
Ethics towards AI in product and experience design
skipperchong
2
310
Color Theory Basics | Prateek | Gurzu
gurzu
0
360
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
201
75k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
320
The Illustrated Children's Guide to Kubernetes
chrisshort
51
52k
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以上でフィルタ ◦ 適当に並べる
• あってるかは不明…