LoginSignup
5
1

More than 1 year has passed since last update.

Elasticsearchで見る検索エンジンの仕組み(7): マッピング編

Last updated at Posted at 2022-07-05
[前回] Elasticsearchで見る検索エンジンの仕組み(6): Lukeでインデックス操作編

はじめに

前回は、LukeでElasticsearchインデックスを閲覧しました。
今回は、Elasticsearchのマッピングを理解します。

マッピングとは

  • ドキュメント内の各フィールドのデータ構造やデータ型を定義したもの
    • リレーショナルデータベースのテーブル定義に相当
  • 型のみならず、フィールドの検索に必要な解析処理(Analysis)設定も行う

マッピングの種類

  • 動的マッピング(自動)
    • Elasticsearchはスキーマレスで、フィールドの型推論が可能
    • ドキュメントのインデクシング時、フィールド毎に型マッピングを自動設定
  • 静的マッピング(手動)
    • 事前に型マッピングを自前定義する仕組み
    • 自動マッピングされる型や解析処理が検索要件を満たさない場合使用

動的マッピングをやってみる

  • 一冊の本を1つのドキュメントとして登録
    • インデックス: book_index (インデックスは、Elasticsearchクラスターに存在)
PUT book_index/_doc/1
{
  "title": "Elasticsearch Tech",
  "author": "taro momo",
  "tags": [
    "Elasticsearch",
    "Tech"
  ],
  "pub_date": "2022-07-05T07:40:00",
  "price": 1000,
  "tax": 0.1,
  "published": true
}
  • マッピング定義を確認
GET /book_index/_mapping
  • 結果、各フィールドがElasticsearch基本型に自動マッピングされている("type")
{
  "book_index": {
    "mappings": {
      "properties": {
        "author": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "price": {
          "type": "long"
        },
        "pub_date": {
          "type": "date"
        },
        "published": {
          "type": "boolean"
        },
        "tags": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "tax": {
          "type": "float"
        },
        "title": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    }
  }
}

静的マッピングをやってみる

  • インデックスbook_indexを削除
DELETE /book_index
  • 手動で型マッピング定義
    • "tags"フィールドを分割せず、完全一致時のみ検索できるように設定
      • "type": "keyword" <- アナライザーによる単語分割処理を行わない
      • "index": true <- インデクシング対象
PUT /book_index
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text"
      },
      "author": {
        "type": "text"
      },
      "tags": {
        "type": "keyword",
        "index": true
      },
      "price": {
        "type": "long"
      },
      "pub_date": {
        "type": "date"
      },
      "published": {
        "type": "boolean"
      },
      "tax": {
        "type": "float"
      }
    }
  }
}
  • ドキュメントを1件登録
    • フィールド"title"と"tags"に、同じ値"Elasticsearch-Tech"を付与しても、解析処理が異なる
      • "title"フィールドは、アナライザーにより2つの単語に分割しインデクシング
        • "Elasticsearch"
        • "Tech"
      • "tags"フィールドは、分割されず1つの単語としてインデクシング
        • "Elasticsearch-Tech"
PUT book_index/_doc/1
{
  "title": "Elasticsearch-Tech",
  "author": "taro momo",
  "tags": [
    "Elasticsearch-Tech"
  ],
  "pub_date": "2022-07-05T07:40:00",
  "price": 1000,
  "tax": 0.1,
  "published": true
}
  • "title"フィールドを、単語"Elasticsearch"で検索
    • 結果、ドキュメントが1件ヒット
      • "Elasticsearch-Tech"が"Elasticsearch"と"Tech"に分割されるため
GET /book_index/_search
{
  "query": {
    "match": {
      "title": "Elasticsearch"
    }
  }
}
  • "tags"フィールドを、"Elasticsearch"で検索
    • 結果、ヒットせず
      • "Elasticsearch-Tech"が分割されず、1つの単語として扱われるため
GET /book_index/_search
{
  "query": {
    "match": {
      "tags": "Elasticsearch"
    }
  }
}

おわりに

Elasticsearchのマッピングを理解しました。
次回も続きます。お楽しみに。

[次回] Elasticsearchで見る検索エンジンの仕組み(8): Kibana起動エラーのトラブルシューティング
5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1