Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
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
380
Introduction of finagle
iwag
1
190
Introduction of JAVA8
iwag
0
410
cocos2d-jsで雑に作るブラウザゲーム
iwag
0
280
Rewrite in Finagle
iwag
4
1.5k
Chisel RISC-V introduction
iwag
1
3.2k
Other Decks in Programming
See All in Programming
Groovy as a human and AI friendly programming language
paulk
0
140
続・コーディングルールの鮮度を保ちたい / keep-fresh-go-internal-conventions-again
handlename
0
150
AI主導のプログラミング学習アプリを作ってみた
econanringo
0
200
あなたの await、OS まで届いてる? strace で覗く JavaScript の非同期
hasutosasaki
1
1.7k
Compiling a real-world CLI tool to native with Static Hermes
tmikov2023
0
140
自作Discord音声botをLambda MicroVMsに移行した話 / Migrating My Discord Voice Bot to Lambda MicroVMs
akihisaikeda
0
190
Performance Optimization With LLMs
xleitix
0
230
AI Augmentation Techniques with Liberty
ivargrimstad
0
160
AI時代のFlutter品質設計 — アプリ品質を守るQuality Layers
omatty198
0
310
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
490
AI時代のIaC入門 〜なぜインフラをコードで管理したいのか?〜 / Introduction to IaC in the AI Era
akihisaikeda
5
1.3k
Snowflake Openflow Connector利用の勘所 / 20261024developersio2026tokyo-snowflake-openflow
kasacchiful
PRO
0
250
Featured
See All Featured
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
380
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
23k
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
230
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2.2k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Java REST API Framework Comparison - PWX 2021
mraible
34
9.8k
WCS-LA-2024
lcolladotor
0
940
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
6
1.2k
The untapped power of vector embeddings
frankvandijk
2
2k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
660
GraphQLとの向き合い方2022年版
quramy
50
15k
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以上でフィルタ ◦ 適当に並べる
• あってるかは不明…