Elasticsearch
リレーショナルデータベースと弾性検索の違い
サーチ…
前書き
これは、関係背景から来て、弾性探索を学びたい読者のためのものです。このトピックでは、リレーショナルデータベースが適切なオプションではないユースケースを示します。
用語の相違
| リレーショナルデータベース | 弾性検索 |
|---|---|
| データベース | インデックス |
| 表 | タイプ |
| 行/レコード | 資料 |
| 列名 | フィールド |
上の表は、リレーショナルデータベースとelasticsearchの基本的な要素を概略的に示しています。
セットアップ
リレーショナルデータベースの構造に従うことを検討する:
create databse test;
use test;
create table product;
create table product (name varchar, id int PRIMARY KEY);
insert into product (id,name) VALUES (1,'Shirt');
insert into product (id,name) VALUES (2,'Red Shirt');
select * from product;
name | id
----------+----
Shirt | 1
Red Shirt | 2
エラスティックサーチ等価:
POST test/product
{
"id" : 1,
"name" : "Shirt"
}
POST test/product
{
"id" : 2,
"name" : "Red Shirt"
}
GET test/product/_search
"hits": [
{ ==============
"_index": "test", ===> index |
"_type": "product", ===>type |
"_id": "AVzglFomaus3G2tXc6sB", |
"_score": 1, |
"_source": { |===> document
"id": 2, ===>field |
"name": "Red Shirt" ===>field |
} |
}, ==============
{
"_index": "test",
"_type": "product",
"_id": "AVzglD12aus3G2tXc6sA",
"_score": 1,
"_source": {
"id": 1,
"name": "Shirt"
}
}
]
リレーショナルデータベースが適切でない用途
検索の本質はその順序にある。誰もが、最適な結果が上に表示されるように検索結果を表示することを望んでいます。リレーショナルデータベースにはこのような機能はありません。一方、Elasticsearchは、デフォルトで関連性に基づいて結果を表示します。
セットアップ
前の例で使用したのと同じです。
問題文
ユーザーが
shirtsを探したいと思っていて、red色のシャツに興味があるとします。その場合、redとshirtsキーワードを含む結果が上に表示されます。その後、他のシャツの結果が表示されます。リレーショナルデータベースクエリを使用したソリューション
select * from product where name like '%Red%' or name like '%Shirt%'。出力
name | id -----------+---- Shirt | 1 Red Shirt | 2弾性検索ソリューション
POST test/product/_search { "query": { "match": { "name": "Red Shirt" } } }出力
"hits": [ { "_index": "test", "_type": "product", "_id": "AVzglFomaus3G2tXc6sB", "_score": 1.2422675, ===> Notice this "_source": { "id": 2, "name": "Red Shirt" } }, { "_index": "test", "_type": "product", "_id": "AVzglD12aus3G2tXc6sA", "_score": 0.25427115, ===> Notice this "_source": { "id": 1, "name": "Shirt" } } ]結論
上で見てきたように、Relational Databaseはランダムな順序で結果を返しましたが、Elasticsearchは関連性に基づいて計算された
_scoreで結果を返します。
検索文字列を入力している間、間違ってしまう傾向があります。ユーザーが間違った検索パラメータを入力する場合があります。リレーショナルデータベースはこのようなケースを処理しません。救助のための弾圧の探索。
セットアップ
前の例で使用したのと同じです。
問題文
仮定ユーザーがを検索したい
shirtsが、彼は間違った単語に入りshrt誤ってを。ユーザーはまだシャツの結果を見ることを期待しています 。リレーショナルデータベースクエリを使用したソリューション
select * from product where name like '%shrt%'出力
No results found弾性検索ソリューション
POST /test/product/_search { "query": { "match": { "name": { "query": "shrt", "fuzziness": 2, "prefix_length": 0 } } } }出力
"hits": [ { "_index": "test", "_type": "product", "_id": "AVzglD12aus3G2tXc6sA", "_score": 1, "_source": { "id": 1, "name": "Shirt" } }, { "_index": "test", "_type": "product", "_id": "AVzglFomaus3G2tXc6sB", "_score": 0.8784157, "_source": { "id": 2, "name": "Red Shirt" } } ]結論
上記のように、リレーショナルデータベースは誤った単語の検索結果を返しませんでしたが、Elasticsearchは特別な
fuzzyクエリを使用して結果を返します。