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
180
Elasticsearch入門 〜前編〜
社内勉強会でElasticsearchの基礎について話した時の資料です。
Elasticsearchの概要と基本操作について説明しています。
rince
July 21, 2022
Tweet
Share
More Decks by rince
See All by rince
あらゆる商品を扱う商品データベースを再設計した話 / product db re-architecture
rince
9
5k
Railsで海外9ヵ国にサービス展開している話 / Rails i18n
rince
3
1.5k
ActiveSupport::Concern で学ぶRuby
rince
1
280
Railsを6年間やってきたぼくが最近Railsでハマったこと
rince
3
530
プロジェクトをまたいだIssue管理
rince
0
2.2k
そうだ Rack 作ろう。
rince
2
78
Lean Startup
rince
0
150
Yahoo!主催のOpenHackDayJapanに参加してJAXA賞をいただきました
rince
1
98
Other Decks in Technology
See All in Technology
よく聞くけど使ったことないソフトウェアNo.1 KafkaとSnowflake
foursue
4
370
自己改善からチームを動かす! 「セルフエンジニアリングマネージャー」のすゝめ
shoota
6
850
Azureの基本的な権限管理の勉強会
yhana
0
780
Compose Compiler Metricsを使った実践的なコードレビュー
tomorrowkey
1
220
今年のRubyKaigiはProfiler Year🤘
osyoyu
0
190
ExaDB-D dbaascli で出来ること
oracle4engineer
PRO
0
2.1k
ServiceNow Knowledge Learning Rise up
manarobot
0
210
「スニダン」開発組織の構造に込めた意図 ~組織作りはパッションや政治ではない!~
rinchsan
3
570
どうするコスト最適化のトレードオフ
tetsuyaooooo
1
590
Android Target SDK 35 (Android 15) 対応の概要
akkie76
0
110
The AI Revolution Will Not Be Monopolized: Behind the scenes
inesmontani
PRO
0
110
Grafana x PagerDuty Better Together
jacopen
0
150
Featured
See All Featured
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
352
28k
Designing Experiences People Love
moore
136
23k
Designing for Performance
lara
601
67k
Building Your Own Lightsaber
phodgson
99
5.7k
Into the Great Unknown - MozCon
thekraken
10
1k
KATA
mclloyd
15
12k
The Brand Is Dead. Long Live the Brand.
mthomps
49
29k
Rebuilding a faster, lazier Slack
samanthasiow
73
8.2k
A designer walks into a library…
pauljervisheath
200
23k
The Mythical Team-Month
searls
216
42k
Faster Mobile Websites
deanohume
299
30k
Web development in the modern age
philhawksworth
202
10k
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.