Amazon Elasticsearch ServiceでElasticsearch 2.3が利用可能になりました

2016.08.14

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

こんにちは、藤本です。

7/27にAmazon Elasticsearch Serviceにバージョン2.3が追加されました。

Amazon Elasticsearch Service now supports Elasticsearch 2.3

Elasticsearch 1.5 ⇛ 2.3

上記サイトにも記載されているように1.5系から2.3系により数多くの機能追加、パフォーマンス改善が含まれています。今回は上記サイトに記載されている3つを簡単にご紹介します。詳細は各バージョンのリリースノートをご確認ください。

パイプラインアグリゲーションの追加

Elasticブログ:Out of this world aggregations

Elasticsearch 2.0からアグリゲーションにパイプラインアグリゲーションが追加されました。

  • Bucketing
  • Metric
  • Pipeline **new**

パイプラインアグリゲーションは他のアグリゲーションの結果(バケット)に対して、アグリゲーションすることができます。従来のMetricでも提供していたAvg/Max/Min/Sumに加えて、移動平均の算出も可能になりました。

例えば、Elasticsearch Reference : Pipeline Aggregationsに記載している以下のようなリクエストを投げることで、3種類のアグリゲーションを活用しています。

{
"aggs" : {
"sales_per_month" : {
"date_histogram" : { // (1)
"field" : "date",
"interval" : "month"
},
"aggs": {
"sales": {
"sum": { // (2)
"field": "price"
}
}
}
},
"max_monthly_sales": {
"max_bucket": { // (3)
"buckets_path": "sales_per_month>sales"
}
}
}
}

(1). date_histogram aggregation (Bucketing) ある日付の単位でデータの固まりを作成します。上記の例ではdateフィールドから1ヶ月単位でデータを分けます。

(2). sum aggregation (Metric) データの合計値を算出します。上記の例では1ヶ月単位のpriceフィールドの合計値を算出します。

(3). max bucket aggregation (Pipeline) データの最大値を抽出します。上記の例では月単位で最も大きい売り上げ値を抽出することができます。

Geo Pointフィールドの改善

Elasticブログ:Supercharging geo_point fields in Elasticsearch 2.2

Lucene 5.3から追加された新しいGeoPointFieldの利用によりgeo_pointフィールドのインデックスサイズの削減、スループットの向上など大幅に改善されました。Elastic社公式ブログのベンチマークで2.1.1から2.2だけでもスループットが約1.5倍、インデックスサイズが半分になったようです。更に2.0へのアップデート時にもgeo_pointフィールドのパフォーマンス改善は行われています。

データ圧縮

Elasticブログ:Store compression in Lucene and Elasticsearch

インデックスデータの圧縮がサポートされました。圧縮タイプはLZ4、もしくはDEFLATEを提供しています。デフォルトでLZ4圧縮が適用され、設定によりDEFLATE圧縮を適用することが出来ます。インデックスサイズが10%〜30%削減されるようです。

filteredqueryの非推奨

新機能ではありませんが、2.0系からQuery DSLに大きな変化がありました。 1.x系はQuery DSLにqueryfilteredがありましたが、2.0からfilteredが非推奨となり、queryに統合されました。

1.x系
{
“filtered” : {
“query”: { query definition },
“filter”: { filter definition }
}
}
2.0以降
{
“bool” : {
“must”: { query definition },
“filter”: { filter definition }
}
}

設定画面

AWSマネジメントコンソールでも、APIでも2.3のAmazon ESドメインを新規作成することが可能です。

※ 現状ではAWSマネジメントコンソールのGUI操作だけによる1.5から2.3へのアップグレードはできません。移行に関しては別途方法があり、AWSのドキュメントに記載されていますので検証してみて別途ご紹介します。

GUIによるドメイン作成

Amazon ESの管理画面に遷移します。 「Create a new domain」からドメイン作成のウィザードに遷移します。

Amazon_Elasticsearch_Service_Management_Console 7

「Elasticsearch version」の選択から1.52.3を選択可能となっています。

Amazon_Elasticsearch_Service_Management_Console 6

その後の手順は変わりありません。

APIによるドメイン作成(AWS CLI)

aws es create-elasticsearch-domainのオプションに--elasticsearch-versionが増えました。1.52.3の指定が可能となりました。

# aws es create-elasticsearch-domain help
:
SYNOPSIS
create-elasticsearch-domain
--domain-name
[--elasticsearch-version ]
[--elasticsearch-cluster-config ]
[--ebs-options ]
[--access-policies ]
[--snapshot-options ]
[--advanced-options ]
[--cli-input-json ]
[--generate-cli-skeleton]
:
--elasticsearch-version (string)
String of format X.Y to specify version for the Elasticsearch domain
eg. "1.5" or "2.3". For more information, see Creating Elasticsearch
Domains in the Amazon Elasticsearch Service Developer Guide .
:

各種バージョン

Elasticsearch (Lucene)

Elasticsearchは今年の4/26にリリースされた2.3.2なので比較的新しいバージョンが提供されています。(Luceneは5.5.0)

# curl https://search-es23-************.ap-northeast-1.es.amazonaws.com/
{
"name" : "Nighthawk",
"cluster_name" : "341946975188:es23",
"version" : {
"number" : "2.3.2",
"build_hash" : "0944b4bae2d0f7a126e92b6133caf1651ae316cc",
"build_timestamp" : "2016-05-20T07:46:04Z",
"build_snapshot" : false,
"lucene_version" : "5.5.0"
},
"tagline" : "You Know, for Search"
}
(参考)1.5の場合
# curl https://search-es15-************.ap-northeast-1.es.amazonaws.com/
{
"status" : 200,
"name" : "Hawkeye",
"cluster_name" : "341946975188:es15",
"version" : {
"number" : "1.5.2",
"build_hash" : "20085dbc168df96c59c4be65f2999990762dfc6f",
"build_timestamp" : "2016-04-20T15:51:59Z",
"build_snapshot" : false,
"lucene_version" : "4.10.4"
},
"tagline" : "You Know, for Search"
}

Kibana

Kibanaは去年の9月にリリースされた4.1.2と古めなバージョンです。KibanaがElasticsearch 2系のサポートを開始したのが4.2系(もしかしたら4.1.x系でも対応しているかも)からだったはずです。バージョンに「4.1.2-es-2.0」と記載されているので4.1系のまま、2系に対応させたビルドのようです。なぜKibanaをElasticsearch2.3.2のリリースに近いバージョンにしなかったのか気になるところです。4.5.0とか。

Settings_-_Kibana_4 2

(参考)1.5の場合

Settings_-_Kibana_4

導入プラグイン

プラグインはKibana3がElasticsearch2系に対応していないため、抜かれています。

# curl search-es23-*******************.ap-northeast-1.es.amazonaws.com/_cat/plugins
Black Knight analysis-icu 2.3.2 j
Black Knight analysis-kuromoji 2.3.2 j
Black Knight cloud-aws 2.3.2 j
Black Knight elasticsearch-jetty 2.2.0 j
Black Knight kibana 2.3.2 s /_plugin/kibana/
(参考)1.5の場合

気づいた方もいらっしゃるかもしれませんが、既存環境含めていつの間にかmigrationプラグインが追加されています。こちらは先で紹介した移行に役立つプラグインとなりますので別記事でご紹介します。

# curl search-es15-******************.ap-northeast-1.es.amazonaws.com/_cat/plugins
Hawkeye jetty NA j
Hawkeye cloud-aws ${project.version} j
Hawkeye analysis-kuromoji 2.5.1-SNAPSHOT j
Hawkeye analysis-icu 2.5.0 j
Hawkeye kibana NA s /_plugin/kibana/
Hawkeye migration NA s /_plugin/migration/
Hawkeye kibana3 NA s /_plugin/kibana3/

新機能を試す

1.5から2.3にかけて実装された新規APIをいくつか試してみます。

Profile API

Elasticsearch Reference : Profile API

Profile APIはクエリによって実行される内部処理を詳細に出力します。内部処理の解析によりレスポンスが遅いクエリの何がボトルネックなのかを知ることが可能となります。

Profile APIを利用する時はSearch APIのリクエストに"profile": trueを追加するだけです。

# curl -XGET "http://search-es23-***********.ap-northeast-1.es.amazonaws.com/logstash-2015.05.20/_search?pretty" -d'
{
"profile": true,
"query": {
"bool": {
"must": [
{"match": {"response": "200"}}
],
"filter": {
"match": {"geo.src": "US"}
}
}
}
}'
{
"took": 23,
:
"hits": {
"total": 398,
:
},
"profile": {
"shards": [
{
"id": "[CDa6WCYjTjujddssKbE-jQ][logstash-2015.05.20][4]",
"searches": [
{
"query": [
{
"query_type": "BooleanQuery",
"lucene": "+response:200 #geo.src:us",
"time": "0.9419740000ms",
"breakdown": {
"score": 38638,
"create_weight": 176391,
"next_doc": 153608,
"match": 0,
"build_scorer": 194738,
"advance": 0
},
"children": [
{
"query_type": "TermQuery",
"lucene": "response:200",
"time": "0.2178780000ms",
:
},
{
"query_type": "TermQuery",
"lucene": "geo.src:us",
"time": "0.1607210000ms",
:
}
]
}
],
"rewrite_time": 46895,
"collector": [
{
"name": "SimpleTopScoreDocCollector",
"reason": "search_top_hits",
"time": "0.06943100000ms"
}
]
}
]
:

結果は1000行を超えたので省略しています。出力結果のJSONにprofileが追加されています。内容としては、シャード別にQueryの処理経緯が出力されています。Luceneに発行されたAPI、Lucene Queryの内容、処理にかかった時間が出力されます。

Update By Query API

Elasticsearch Reference : Update By Query API

Query結果に対してUpdate処理を行うことができます。今まではQueryで取得したドキュメントをUpdateするというアプリケーション側で実装する必要がありましたが、Update By QueryによりElasticsearch APIを発行するだけで可能となりました。

ただし、Amazon Elasticsearch Serviceではダイナミックスクリプトをサポートされておらず、スクリプトファイルの配置も不可なため、スクリプトによるアップデートができません。(何か方法があればコメントなどで教えてください)

Update By Queryには追加したMapping設定の反映といった利用方法もあります。

# curl -XGET "http://search-es23-****************.ap-northeast-1.es.amazonaws.com/bank/account/_mapping"
{
"bank": {
"mappings": {
"account": {
"properties": {
:
"lastname": {
"type": "string"
}
}
}
}
}
}

### not_analyzedフィールドの追加
# curl -XPUT "http://search-es23-***************.ap-northeast-1.es.amazonaws.com/bank/account/_mapping" -d'
{
"account": {
"properties": {
:
"lastname": {
"type": "string",
"fields": {
"noanalyze": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}'
{
"acknowledged": true
}

### この時点ではマッピングの更新だけでは検索結果に追加されない
# curl -XGET "http://search-es23-***************.ap-northeast-1.es.amazonaws.com/bank/_search" -d'
{
"query": {
"match": {
"lastname.noanalyze": "Harding"
}
}
}'
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}

### Update By Query APIによりインデックスが更新される
# curl -XPOST "http://search-es23-**************.ap-northeast-1.es.amazonaws.com/bank/_update_by_query" -d'
{
"query": {
"term": {
"_type": "account"
}
}
}'
{
"took": 353,
"timed_out": false,
"total": 1000,
"updated": 1000,
"batches": 10,
"version_conflicts": 0,
"noops": 0,
"retries": 0,
"failures": []
}

### Update By Queryにより検索結果に反映される
# curl -XGET "http://search-es23-***************.ap-northeast-1.es.amazonaws.com/bank/_search" -d'
{
"query": {
"match": {
"lastname.noanalyze": "Harding"
}
}
}'
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 5.5900564,
"hits": [
{
"_index": "bank",
"_type": "account",
"_id": "44",
"_score": 5.5900564,
"_source": {
"account_number": 44,
"balance": 34487,
"firstname": "Aurelia",
"lastname": "Harding",
"age": 37,
"gender": "M",
"address": "502 Baycliff Terrace",
"employer": "Orbalix",
"email": "aureliaharding@orbalix.com",
"city": "Yardville",
"state": "DE"
}
}
]
}
}

制限事項

1.5系から2.3系により提供されるAPIも増えましたが、Amazon ESのサポートAPIに変更はありませんでした。Profile APIやUpdate Query APIなどドキュメントやインデックスに実行するAPI利用可能ですが、Reindex APIは利用できません。

# curl -XPOST "http://search-es23-*********************.ap-northeast-1.es.amazonaws.com/_reindex" -d'
{
"source": {
"index": "index"
},
"dest": {
"index": "newindex"
}
}'
{"Message":"Your request: '/_reindex' is not allowed."}

まとめ

いかがでしたでしょうか?

今回のリリースは待望のリリースとなった方は多いのではないでしょうか。今回は主に新規機能をご紹介しましたが、1系から2系にかけて多くのパフォーマンス改善が実施されています。アップデートは簡単ではないかもしれませんが、2系にアップデートすることをオススメします。

参考URL