(自分用メモ)
Mysqlとの違い
| MySQL | ES |
|---|---|
| データベース | index |
| 行(1件のデータ) | document |
| カラムの型 | mapping |
なので「インデックス再構築」はデータベース作り直しってイメージ
データの形
ESはJSON
{
"deal": {
"id": 1,
"name": "TOYOTAへの提案",
"has_materials": true
}
}
検索クエリの書き方
基本形
GET index name/_search
{
"query": {
ここに条件を書く
}
}
query はSQLの WHERE 句
完全一致で検索したいっす (term)
SQLでいうと
WHERE has_materials = true
{
"query": {
"term": { "deal.has_materials": true }
}
}
複数の条件をANDでつなぎたい (= bool + filter)
SQLでいうと
WHERE has_materials = true AND invoice_id = 3
{
"query": {
"bool": {
"filter": [
{ "term": { "deal.has_materials": true } },
{ "term": { "deal.invoice.id": 3 } }
]
}
}
}
bool + filter の配列に条件を並べるとAND
テキストを部分一致で検索したい(= match)
SQLでいうと
WHERE name LIKE '%トヨタ%'
{
"query": {
"match": { "deal.name": "トヨタ" }
}
}
マッピングって何ですの?
MySQLの「カラムの型定義(int, varchar...)」と同じ。
フィールドの型をあらかじめ宣言しておくもの。
{
"deal": {
"id": { "type": "integer" },
"name": { "type": "text" },
"has_materials": { "type": "boolean" }
}
}
mapping変更したら?
インデックス作り直しですわ
Top comments (0)