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
300
Elasticsearch入門 〜前編〜
社内勉強会でElasticsearchの基礎について話した時の資料です。
Elasticsearchの概要と基本操作について説明しています。
rince
July 21, 2022
Tweet
Share
More Decks by rince
See All by rince
エンジニアが加速させるプロダクトディスカバリー 〜最速で価値ある機能を見つける方法〜 / product discovery accelerated by engineers
rince
3
800
あらゆる商品を扱う商品データベースを再設計した話 / product db re-architecture
rince
11
6.3k
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
190
Yahoo!主催のOpenHackDayJapanに参加してJAXA賞をいただきました
rince
0
150
Other Decks in Technology
See All in Technology
從開發到架構設計的可觀測性實踐
philipz
0
120
AIのための オンボーディングドキュメントを整備する - hirotea
hirotea
9
2.3k
テストを実施する前に考えるべきテストの話 / Thinking About Testing Before You Test
nihonbuson
PRO
14
2.1k
DevOpsDays Taipei 2025 -- Creating Awesome Change in SmartNews!
martin_lover
0
160
技術書典18結果報告
mutsumix
2
180
MCP Clientを活用するための設計と実装上の工夫
yudai00
1
810
Contract One Dev Group 紹介資料
sansan33
PRO
0
6k
【5分でわかる】セーフィー エンジニア向け会社紹介
safie_recruit
0
25k
ソフトウェアは捨てやすく作ろう/Let's make software easy to discard
sanogemaru
10
5.8k
Babylon.jsでゲームを作ってみよう
limes2018
0
100
令和トラベルQAのAI活用
seigaitakahiro
0
520
Introduction to Bill One Development Engineer
sansan33
PRO
0
240
Featured
See All Featured
How STYLIGHT went responsive
nonsquared
100
5.6k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
31
1.2k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
160
15k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
12k
Agile that works and the tools we love
rasmusluckow
329
21k
Building Flexible Design Systems
yeseniaperezcruz
329
39k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
30
2.4k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Building Applications with DynamoDB
mza
95
6.4k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
32
5.8k
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.