Elasticsearch のクエリで利用する、データ構造をまとめました。
index | doc_values | fielddata | global_ordinals | |
---|---|---|---|---|
用途 | 検索 | sort/集計/script | sort/集計/script | パフォーマンス向上 |
フィールド | ほとんど全て | text 以外ほぼ全て | text | text, keyword など |
格納場所 | ストレージ | ストレージ | ヒープメモリ | ヒープメモリ |
キャッシュ | ファイルシステムキャッシュ | ファイルシステムキャッシュ | フィールドデータキャッシュ | フィールドデータキャッシュ |
デフォルト | 有効 | 有効 | 無効 | 有効※ |
これらの [用途] で使わないフィールドは、無効にすることでリソース消費を削減できます。
Elasticsearch & OpenSearch の使い方 | ||||
---|---|---|---|---|
学習ロードマップ | |||||
---|---|---|---|---|---|
データ構造の種類
- index
- doc_values
- fielddata
- global_ordinals
analyzer の後に indexe を作成します。
PUT index
{
"mappings": {
"properties": {
"test_field": {
"type": "text",
"index": false
}
}
}
}
index を無効にすると、インデックスが作成されず、検索できないことを確かめます。
PUT index/_doc/1
{
"test_field":"Elasticsearch"
}
GET index/_search
{
"query": {
"match": {
"test_field": "Elasticsearch"
}
}
}
"type": "query_shard_exception",
"reason": "failed (中略) since it is not indexed.",
インデックスが無いと、検索できないことを確認できました。
doc_values を無効にすると、集計できないことを確かめます。
PUT doc_values
{
"mappings": {
"properties": {
"num": {
"type": "integer",
"doc_values": false
}
}
}
}
POST doc_values/_bulk
{ "index": { "_id": "1" } }
{ "num":1 }
{ "index": {"_id": "2" } }
{ "num":2 }
{ "index": { "_id": "3" } }
{ "num":3 }
GET doc_values/_search
{
"size": 0,
"aggs": {
"result": {
"avg": {
"field":"num"
}
}
}
}
"reason": "(中略) Use doc values instead."
doc_values が無効なので、集計できなくなりました。
元々は全てのフィールドタイプで fielddata を利用していたが、メモリを食いすぎるため、ストレージ上にデータを置く doc_values が開発された経緯があるようです。(詳しくはこちら)
fielddata を有効にすることで、text フィールドでも集計可能になります。
PUT fielddata
{
"mappings": {
"properties": {
"str": {
"type": "text",
"fielddata": true
}
}
}
}
POST fielddata/_bulk
{ "index": { "_id": "1" } }
{ "str":"Elasticsearch is full search engine" }
{ "index": {"_id": "2" } }
{ "str":"Elasticsearch search a doc" }
{ "index": { "_id": "3" } }
{ "str":"Elasticsearch aggrigation terms" }
GET fielddata/_search?size=0
{
"aggs": {
"tags": {
"terms": {
"field": "str",
"min_doc_count": 2
}
}
}
}
"buckets": [
{
"key": "elasticsearch",
"doc_count": 3
},
{
"key": "search",
"doc_count": 2
}
fielddata を有効にすることで、text フィールドでも集計できました。
Elasticsearch は、doc values の代わりに、コンパクトな序数をヒープメモリに置き、パフォーマンスを向上させます。
Doc ID | doc values | 序数 |
---|---|---|
1 | Elasticsearch | 1 |
2 | OpenSearch | 2 |
3 | Elasticsearch | 1 |
カーディナリが高いほど序数と用語のマッピングが増えるので、メモリ消費が増える
つまり、true にすると検索が早くなります。(refresh は遅くなります)
PUT ordinals
{
"mappings": {
"properties": {
"str": {
"type": "keyword",
"eager_global_ordinals": true
}
}
}
}
関連記事
Elasticsearch & OpenSearch の使い方 | ||||
---|---|---|---|---|
学習ロードマップ | |||||
---|---|---|---|---|---|
参考サイト
Elasticsearch: Understanding the Terms Aggregation | Mitchell Pottratz
Anyone who has worked extensively around Elasticsearch is familiar with terms aggregations. Conceptually, it’s very simp...
0