色んな事を書く

シンプルさを極めたエンジニアになりたい

Elasticsearch の index とか doc_value とか

Elasticsearch の Mapping に定義出来る下記の項目について調べたことをまとめます。Elasticsearch の version は 6.8 です。

  • index
  • doc_values
  • fielddata
  • enabled

www.elastic.co

各項目の用途のまとめです。

index doc_values fielddata enabled
用途 検索 集計/ソート/Script 集計/ソート/Script _sourceのみの保存
field 検索 text以外 textのみ objectのみ
保存先 ディスク ディスク ヒープ
キャッシュ ファイルシステムキャッシュ ファイルシステムキャッシュ フィールドデータキャッシュ

index

doc_values

www.elastic.co

検証のために以下の mapping 定義を持つ Index を作成。

{
  "doc_value_index": {
    "mappings": {
      "person": {
        "properties": {
          "age": {
            "type": "long"
          },
          "first_name": {
            "type": "keyword"
          },
          "id": {
            "type": "long"
          },
          "last_name": {
            "type": "keyword",
            "doc_values": false
          }
        }
      }
    }
  }
}

first_namedoc_valuestrue とし、last_namefalse としている。この Index に以下の 4 つのドキュメントを作成。

{
  "id": 1,
  "age": 10,
  "first_name": "山田",
  "last_name": "太郎"
},
{
  "id": 2,
  "age": 10,
  "first_name": "山田",
  "last_name": "花子"
},
{
  "id": 3,
  "age": 10,
  "first_name": "佐藤",
  "last_name": "花子"
},
{
  "id": 4,
  "age": 10,
  "first_name": "佐藤",
  "last_name": "太郎"
}

この状態で last_name, first_name それぞれに Aggregartion を行うリクエストを投げてみる。まずは first_name に対する aggregation

{
  "size": 0,
  "aggs" : {
    "first_name_agges": {
      "terms" : {
        "field" : "first_name"
      }
    }
  }
}

期待通りに集計がなされて、それぞれの名字の件数も正しい。

{
  "aggregations": {
    "first_name_agges": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "佐藤",
          "doc_count": 2
        },
        {
          "key": "山田",
          "doc_count": 2
        }
      ]
    }
  }
}

次に last_name に対する aggregation

{
  "size": 0,
  "aggs" : {
    "last_name_agges": {
      "terms" : {
        "field" : "last_name"
      }
    }
  }
}

すると下記のようなエラーメッセージ(抜粋)が返ってきた。

Can't load fielddata on [last_name] because fielddata is unsupported on fields of type [keyword]. Use doc values instead.

aggregation を行うために fielddata を読み込もうとしたみたい。keyword 型の fieldfielddata をサポートしていないとのこと。だから doc_values を使って aggregation をせねばいかんが、それを無効にしてるから失敗するって事ですね。

元々 doc_valuesfielddata がメモリを使いすぎてしまうため実装されたという前提を考えるとまぁそうなるよねって感じ。

enabled

_source について深ぼる。

www.elastic.co