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
rince
July 21, 2022
Technology
0
310
Elasticsearch入門 〜前編〜
社内勉強会でElasticsearchの基礎について話した時の資料です。
Elasticsearchの概要と基本操作について説明しています。
rince
July 21, 2022
Tweet
Share
More Decks by rince
See All by rince
エンジニアが加速させるプロダクトディスカバリー 〜最速で価値ある機能を見つける方法〜 / product discovery accelerated by engineers
rince
3
840
あらゆる商品を扱う商品データベースを再設計した話 / product db re-architecture
rince
11
6.4k
Railsで海外9ヵ国にサービス展開している話 / Rails i18n
rince
2
1.9k
ActiveSupport::Concern で学ぶRuby
rince
1
320
Railsを6年間やってきたぼくが最近Railsでハマったこと
rince
3
570
プロジェクトをまたいだIssue管理
rince
0
2.3k
そうだ Rack 作ろう。
rince
2
110
Lean Startup
rince
0
200
Yahoo!主催のOpenHackDayJapanに参加してJAXA賞をいただきました
rince
0
150
Other Decks in Technology
See All in Technology
GitHub Copilot の概要
tomokusaba
1
150
プロダクトエンジニアリング組織への歩み、その現在地 / Our journey to becoming a product engineering organization
hiro_torii
0
140
AI専用のリンターを作る #yumemi_patch
bengo4com
3
1.9k
生成AIで小説を書くためにプロンプトの制約や原則について学ぶ / prompt-engineering-for-ai-fiction
nwiizo
4
3.4k
生成AI時代の開発組織・技術・プロセス 〜 ログラスの挑戦と考察 〜
itohiro73
1
370
MySQL5.6から8.4へ 戦いの記録
kyoshidaxx
1
300
タイミーのデータモデリング事例と今後のチャレンジ
ttccddtoki
4
1.3k
製造業からパッケージ製品まで、あらゆる領域をカバー!生成AIを利用したテストシナリオ生成 / 20250627 Suguru Ishii
shift_evolve
PRO
1
160
Github Copilot エージェントモードで試してみた
ochtum
0
130
250627 関西Ruby会議08 前夜祭 RejectKaigi「DJ on Ruby Ver.0.1」
msykd
PRO
2
370
Tech-Verse 2025 Keynote
lycorptech_jp
PRO
0
1.3k
KubeCon + CloudNativeCon Japan 2025 Recap by CA
ponkio_o
PRO
0
250
Featured
See All Featured
Into the Great Unknown - MozCon
thekraken
39
1.9k
It's Worth the Effort
3n
185
28k
Music & Morning Musume
bryan
46
6.6k
Being A Developer After 40
akosma
90
590k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
15
1.5k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
2.9k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.9k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
281
13k
Typedesign – Prime Four
hannesfritz
42
2.7k
Statistics for Hackers
jakevdp
799
220k
Raft: Consensus for Rubyists
vanstee
140
7k
Transcript
Elasticsearch 入門 〜前編〜 2022/04/07 @Professional Fun Lunch
本日の GOAL Elasticsearch を使った開発を行うための最低限の知識を身に付ける Elasticsearch の仕組みを理解する Elasticsearch の基本操作を理解する mybest で
Elasticsearch がどのように使われているかを知る
Agenda 1. Elasticsearch とは 2. Elasticsearch の用語と概念 3. Elasticsearch の基本操作
4. Elasticsearch の検索 5. mybest における Elasticsearch の利用例
1. Elasticsearch とは
Elasticsearch とは Apache Lucene をベースとした Java で書かれた全文検索ソフトウェア ドキュメントを格納(インデックス)することでさまざまな検索や分析を行うことができる オランダに拠点を置く Elastic
社によって開発されている Kibana, Logstash, Beats などの関連ソフトウェアと組み合わせて高機能な分析システムが簡単に構築できる
Elasticsearch の特徴 高速 すべてのデータに対してほぼリアルタイムでの分析、検索が可能 スケーラブル 扱うデータ量、リクエスト量に応じてスケールし分散処理が可能 高可用性 標準で分散配置型の構成をとり障害時に自動復旧が可能 開発者フレンドリー シンプルな
REST API によるアクセス
Elasticsearch のユースケース Web サイトの検索機能 ログやイベントデータの保存・分析 機械学習でリアルタイムにモデルを作成 セキュリティ分析 地理情報を利用しての空間情報管理、分析 遺伝子データの分析
2. Elasticsearch の用語と概念
Elasticsearch の用語と概念 Elasticsearch RDB index ドキュメントを格納する場所 database document type ドキュメントの構成
table mapping 各フィールドのデータ構造やデータ型 schema document 格納される 1 つの文章の単位 record field ドキュメント内の key と value の組 column Elasticsearch の論理的な概念
Elasticsearch の用語と概念
3. Elasticsearch の基本操作
Elasticsearch の基本操作 REST API による HTTP リクエストで操作する ドキュメントは JSON 形式で登録する
API エンドポイントに対して HTTP メソッド(GET, POST, PUT, DELETE )を用いて CRUD の操作を行う ex: https://< サーバ名>:9200/< インデックス名>/\_doc/< ドキュメント ID> curl で簡単に試せる ex: curl http://localhost:9200/product_index_development/_count ` ` ` `
ドキュメントの登録(Create ) POST my_index/_doc/ { "user_name": "rince", "date": "2022-04-07T10:00:00", "message":
"Hello, Elasticsearch!" } { "_type": "_doc", "_seq_no": 0, "_shards": { "successful": 1, "failed": 0, "total": 2 }, "_index": "my_index", "_version": 1, "_primary_term": 1, "result": "created", "_id": "QwuSAYABU1vj78nr9orF" }
ドキュメントの登録(Create ) PUT でドキュメント ID を指定して登録することも可能 PUT my_index/_doc/1 { "user_name":
"rince", "date": "2022-04-07T10:00:00", "message": "Hello, Elasticsearch!" } { "_type": "_doc", "_seq_no": 1, "_shards": { "successful": 1, "failed": 0, "total": 2 }, "_index": "my_index", "_version": 1, "_primary_term": 1, "result": "created", "_id": "1" }
ドキュメントの取得(Read ) GET my_index/_doc/1 { "_type": "_doc", "_seq_no": 1, "_index":
"my_index", "_source": { "date": "2022-04-07T10:00:00", "message": "Hello, Elasticsearch!", "user_name": "rince" }, "_version": 1, "_primary_term": 1, "found": true, "_id": "1" }
ドキュメントの取得(Read ) 登録したドキュメント部分のみを取得したい時は _source を使う GET my_index/_source/1 { "date": "2022-04-07T10:00:00",
"message": "Hello, Elasticsearch!", "user_name": "rince" }
ドキュメントの更新(Update ) PUT my_index/_doc/1 { "user_name": "rince", "date": "2022-04-07T10:30:00", "message":
"Hi, Elasticsearch!" } { "_type": "_doc", "_seq_no": 2, "_shards": { "successful": 1, "failed": 0, "total": 2 }, "_index": "my_index", "_version": 2, "_primary_term": 1, "result": "updated", "_id": "1" }
ドキュメントの削除(Delete ) DELETE my_index/_doc/1 { "_type": "_doc", "_seq_no": 3, "_shards":
{ "successful": 1, "failed": 0, "total": 2 }, "_index": "my_index", "_version": 3, "_primary_term": 1, "result": "deleted", "_id": "1" }
マッピング定義の確認 GET my_index/_mapping { "my_index": { "mappings": { "properties": {
"date": { "type": "date" }, "message": { "type": "text" }, "user_name": { "type": "text" } } } } }
4. Elasticsearch の検索
Elasticsearch の検索 match クエリでフィールド名とキーワードを指定して全文検索が行える GET my_index/_search { "query": { "match":
{ "message": "Elasticsearch" } } }
Elasticsearch の検索 { "hits": { "hits": [ { "_score": 0.2876821,
"_type": "_doc", "_id": "1", "_source": { "date": "2022-04-07T10:30:00", "message": "Hi, Elasticsearch!", "user_name": "rince" }, "_index": "my_index" } ], "total": { "relation": "eq", "value": 1 }, "max_score": 0.2876821 } //... }
基本クエリ - Term レベルクエリ 種別 説明 term クエリ 完全一致検索を行う terms
クエリ キーワードを複数指定して完全一致検索を行う range クエリ 数値型や日付型の値の範囲検索を行う 指定した検索キーワードに完全一致したフィールドを探すときに使用する { "query": { "terms": { "prefecture": ["Tokyo", "Kanagawa", "Chiba", "Saitama"] } } }
複合クエリ - Bool クエリ 種別 説明 must クエリ AND 条件
should クエリ OR 条件 must_not クエリ NOT 条件 filter クエリ AND 条件(スコアは無視) 基本クエリを複数組み合わせて検索を行うための記法 { "query": { "bool": { "must": [ { "match": { "message": "Elasticsearch" } }, { "match": { "user_name": "rince" } } ] }
5. mybest における Elasticsearch の利用例
ex. 抱っこひもの絞り込み { "size": 200, "query": { "bool": { "filter":
[ { "terms": { "_id": [113891, 113886, 113881, 113888, 113890, 113887, ...] } }, { "nested": { "path": "specs", "query": { "bool": { "filter": [ { "bool": { "should": [{ "range": { "specs.31": { "lt": 501 } } }] } }, { "terms": { "specs.86": [21681, 21683] } } ] }
Fin.