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
190
Elasticsearch入門 〜前編〜
社内勉強会でElasticsearchの基礎について話した時の資料です。
Elasticsearchの概要と基本操作について説明しています。
rince
July 21, 2022
Tweet
Share
More Decks by rince
See All by rince
あらゆる商品を扱う商品データベースを再設計した話 / product db re-architecture
rince
9
5.1k
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
80
Lean Startup
rince
0
160
Yahoo!主催のOpenHackDayJapanに参加してJAXA賞をいただきました
rince
1
100
Other Decks in Technology
See All in Technology
止まらないLinuxシステムを構築する_高信頼性クラスタ入門
koedoyoshida
3
2.3k
ExaDB-D dbaascli で出来ること
oracle4engineer
PRO
0
2.1k
Building Dashboards as a Hobby
egmc
0
420
同じ様なUIをiOS/Android間で合わせるヒントNo.2
fumiyasac0921
1
110
Google Cloud Next '24 Recap(Cloud Run/k8s)
mokocm
0
370
.NET Profiler in 2024.
kkamegawa
2
2.4k
Android Target SDK 35 (Android 15) 対応の概要
akkie76
0
190
データベース03: 関係データモデル
trycycle
0
110
Secrets of a PowerShell "Guru"
guyrleech
1
110
本当のガバクラ基礎
toru_kubota
0
150
kcp: Kubernetes APIs Are All You Need #techfeed_live / TechFeed Experts Night 28th
ytaka23
1
120
【SORACOM UG 東海】あらゆるモノがつながる社会へ、IoT と SORACOM
soracom
PRO
1
160
Featured
See All Featured
Stop Working from a Prison Cell
hatefulcrawdad
267
19k
Testing 201, or: Great Expectations
jmmastey
30
6.4k
Statistics for Hackers
jakevdp
790
220k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
9
1.3k
The Illustrated Children's Guide to Kubernetes
chrisshort
32
47k
ParisWeb 2013: Learning to Love: Crash Course in Emotional UX Design
dotmariusz
104
6.6k
Designing with Data
zakiwarfel
96
4.8k
Teambox: Starting and Learning
jrom
128
8.4k
Infographics Made Easy
chrislema
238
18k
Unsuck your backbone
ammeep
664
57k
A Philosophy of Restraint
colly
197
16k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
226
51k
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.