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
280
Elasticsearch入門 〜前編〜
社内勉強会でElasticsearchの基礎について話した時の資料です。
Elasticsearchの概要と基本操作について説明しています。
rince
July 21, 2022
Tweet
Share
More Decks by rince
See All by rince
エンジニアが加速させるプロダクトディスカバリー 〜最速で価値ある機能を見つける方法〜 / product discovery accelerated by engineers
rince
4
510
あらゆる商品を扱う商品データベースを再設計した話 / product db re-architecture
rince
12
6.2k
Railsで海外9ヵ国にサービス展開している話 / Rails i18n
rince
3
1.8k
ActiveSupport::Concern で学ぶRuby
rince
1
310
Railsを6年間やってきたぼくが最近Railsでハマったこと
rince
3
560
プロジェクトをまたいだIssue管理
rince
0
2.2k
そうだ Rack 作ろう。
rince
2
95
Lean Startup
rince
0
180
Yahoo!主催のOpenHackDayJapanに参加してJAXA賞をいただきました
rince
1
130
Other Decks in Technology
See All in Technology
深層学習と古典的画像アルゴリズムを組み合わせた類似画像検索内製化
shutotakahashi
1
290
コンテナサプライチェーンセキュリティ
kyohmizu
1
120
Potential EM 制度を始めた理由、そして2年後にやめた理由 - EMConf JP 2025
hoyo
2
1.5k
プロダクトエンジニア構想を立ち上げ、プロダクト志向な組織への成長を続けている話 / grow into a product-oriented organization
hiro_torii
1
310
OSS構成管理ツールCMDBuildを使ったAWSリソース管理の自動化
satorufunai
0
390
ディスプレイ広告(Yahoo!広告・LINE広告)におけるバックエンド開発
lycorptech_jp
PRO
0
180
Tech Blogを書きやすい環境づくり
lycorptech_jp
PRO
1
270
Iceberg Meetup Japan #1 : Iceberg and Databricks
databricksjapan
0
240
LINE NEWSにおけるバックエンド開発
lycorptech_jp
PRO
0
110
1行のコードから社会課題の解決へ: EMの探究、事業・技術・組織を紡ぐ実践知 / EM Conf 2025
9ma3r
5
1.6k
MIMEと文字コードの闇
hirachan
2
1.4k
偏光画像処理ライブラリを作った話
elerac
1
150
Featured
See All Featured
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.7k
Designing for Performance
lara
604
68k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
7
640
The Art of Programming - Codeland 2020
erikaheidi
53
13k
What's in a price? How to price your products and services
michaelherold
244
12k
Speed Design
sergeychernyshev
27
800
Embracing the Ebb and Flow
colly
84
4.6k
Into the Great Unknown - MozCon
thekraken
35
1.6k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
27
1.9k
Reflections from 52 weeks, 52 projects
jeffersonlam
348
20k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Rails Girls Zürich Keynote
gr2m
94
13k
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.