質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Elasticsearch

Elasticsearchは、クラウド向けに構築された、RESTful な API を提供する分散型のサーチエンジンアプリケーションです。

Q&A

0回答

772閲覧

ElasticsearchにてAggregation結果のkeyごとにドキュメントを持つ出力をする方法はありますでしょうか?

momotaro98

総合スコア19

Elasticsearch

Elasticsearchは、クラウド向けに構築された、RESTful な API を提供する分散型のサーチエンジンアプリケーションです。

0グッド

0クリップ

投稿2019/12/23 09:42

編集2019/12/23 12:02

前提

最新版version7.5のElasticsearchとKibana利用しています。

以下のようなIndexを持っています。
参加者( attendee_id )をキーにした、その参加者がサービス内で拒否したいユーザIDのブラックリスト(assignee_blacklist)を持っています。
(下記テキストはKibana上で動くことを確認しています。)

DELETE /attendees ## Index作成 PUT /attendees { "mappings": { "properties": { "attendee_id": { "type": "keyword" }, "assignee_blacklist": { "type": "keyword" } } } } ## データ挿入 POST _bulk?refresh {"index":{"_index":"attendees", "_id":"1"}} { "attendee_id": "01-00-XXVoiuFdnizJxoJd7pvzZZ", "assignee_blacklist": [ "03-00-5", "03-00-6", "03-00-9" ] } {"index":{"_index":"attendees", "_id":"2"}} { "attendee_id": "01-00-YYVoiuFdnizJxoJd7pvzZZ", "assignee_blacklist": [ "03-00-5" ] } {"index":{"_index":"attendees", "_id":"3"}} { "attendee_id": "01-00-ZZVoiuFdnizJxoJd7pvzZZ", "assignee_blacklist": [] }

やりたいこと & 試していること

やりたいことは、ブラックリストに入っているユーザをごとに集約して、そのユーザをブロックしている参加者(attendee_id)情報を出すことです。

現状では以下のようなクエリを試しているのですが、

GET /attendees/_search { "query": { "match_all": {} }, "aggs": { "blocked_user": { "terms": { "field": "assignee_blacklist" } } } }

以下のような出力が得られます。

{ "took" : 22, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 3, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "attendees", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "attendee_id" : "01-00-XXVoiuFdnizJxoJd7pvzZZ", "assignee_blacklist" : [ "03-00-5", "03-00-6", "03-00-9" ] } }, { "_index" : "attendees", "_type" : "_doc", "_id" : "2", "_score" : 1.0, "_source" : { "attendee_id" : "01-00-YYVoiuFdnizJxoJd7pvzZZ", "assignee_blacklist" : [ "03-00-5" ] } }, { "_index" : "attendees", "_type" : "_doc", "_id" : "3", "_score" : 1.0, "_source" : { "attendee_id" : "01-00-ZZVoiuFdnizJxoJd7pvzZZ", "assignee_blacklist" : [ ] } } ] }, "aggregations" : { "blocked_user" : { "doc_count_error_upper_bound" : 0, "sum_other_doc_count" : 0, "buckets" : [ { "key" : "03-00-5", "doc_count" : 2 }, { "key" : "03-00-6", "doc_count" : 1 }, { "key" : "03-00-9", "doc_count" : 1 } ] } } }

期待すること

期待する(やりたい)結果は以下のようにkeyごとに元々のドキュメント情報があることです。

{ "took" : 55, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 3, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { ここのヒットの中身は既存と同じで良い(合ってもなくても良い) }, "aggregations" : { "blocked_user" : { "doc_count_error_upper_bound" : 0, "sum_other_doc_count" : 0, "buckets" : [ { "key" : "03-00-5", "doc_count" : 2, "hits" : [ { "_index" : "attendees", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "attendee_id" : "01-00-XXVoiuFdnizJxoJd7pvzZZ", "assignee_blacklist" : [ "03-00-5", "03-00-6", "03-00-9" ] }, }, { "_index" : "attendees", "_type" : "_doc", "_id" : "2", "_score" : 1.0, "_source" : { "attendee_id" : "01-00-YYVoiuFdnizJxoJd7pvzZZ", "assignee_blacklist" : [ "03-00-5" ] } } }, { "key" : "03-00-6", "doc_count" : 1 "hits": . 上記と同様にkeyがドキュメントの詳細を持っている . }, { "key" : "03-00-9", "doc_count" : 1, "hits": . 上記と同様にkeyがドキュメントの詳細を持っている . } ] } } }

開発しているサービスでは上記の期待することに出るような結果をそのまま使いたいと思っています。
上記の期待が得られるようなクエリを組むことは可能でしょうか?

また、Apache Solr などの他全文検索ミドルウェアとの対比もしているのですが、Solrならばこういった集約に関して強みがあるなどについても、ご存じであれば教えていただけると大変助かります。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問