# サジェスト結果に対してAggregationをかけたい

**URL:** <https://discuss.elastic.co/t/aggregation/157269>\
**Category:** Elasticsearch\
**Created:** [November 19, 2018, 5:50am UTC](https://discuss.elastic.co/t/aggregation/157269 "2018-11-19T05:50:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![aya2453](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@aya2453](https://discuss.elastic.co/u/aya2453)\
**Post date:** [November 19, 2018, 5:50am UTC](https://discuss.elastic.co/t/aggregation/157269/1 "2018-11-19T05:50:30Z")

</div>

お世話になっております。Elasticsearch初心者のためよくわかっていない部分も多いのですが、以下の内容を実現する方法に関して相談させてください

◾やりたいこと  
user情報を管理するindexに定義したtagsフィールドに対してサジェスト検索を行い、そのタグ名と設定しているユーザーのカウントをとる

以下試した結果になります  
◾データ構造  
GET [http://localhost:9200/index1/type/1?pretty](http://localhost:9200/index1/type/1?pretty)

```
{
"_index": "index1",
"_type": "type",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
    "user_id":1,
    "tags": [
        "オレンジ",
        "りんご",
        "ぶどう",
    ]
}

```

}

GET [http://localhost:9200/index2/type/2?pretty](http://localhost:9200/index2/type/2?pretty)

```
{
"_index": "index2",
"_type": "type",
"_id": "2",
"_version": 1,
"found": true,
"_source": {
    "user_id":1,
    "tags": [
        "なし",
        "りんご"
    ]
}

```

}

◾mapping定義

```
"tags": {
  "type": "text",
  "fields": {
    "raw": {
      "type": "keyword"
    },
    "suggest": {
      "type": "completion"
    }
  }
}

```

◾リクエスト

POST [http://localhost:9200/\_all/\_search?pretty](http://localhost:9200/_all/_search?pretty)

```
{
  "_source": false,
  "suggest": {
      "tags_suggest" : {
          "prefix" : "り",
          "completion" : {
              "field" : "tags.suggest"
        }
    }
}

```

}

◾レスポンス

```
{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 2,
    "successful": 2,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": 0,
    "hits": []
  },
  "suggest": {
    "tags_suggest": [
        {
            "text": "り",
            "offset": 0,
            "length": 1,
            "options": [
                {
                    "text": "りんご",
                    "_index": "index1",
                    "_type": "type",
                    "_id": "1",
                    "_score": 1
                },
               {
                    "text": "りんご",
                    "_index": "index2",
                    "_type": "type",
                    "_id": "2",
                    "_score": 1
                }
            ]
        }
    ]
}

```

}

これをアグリゲーションで

```
"aggregations": {
    "keywords": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
            {
                "key": "りんご",
                "doc_count": 2
            }
        ]
    }
}

```

のように返ってくるようにしたいのですが、やり方がわからず悩んでいます。  
そもそもこのやり方ではできないという話であれば別の方法を教えていただきたいです。

```
{
"_source": false,
"suggest": {
    "tags_suggest" : {
        "prefix" : "り",
        "completion" : {
            "field" : "tags.suggest"
        }
    }
},
"aggs" : {
    "tags" : {
        "terms" : { "field" : "tags.keyword" }
    }
}

```

}

これで取れるのか試したのですが、ダメでした。  
初歩的な質問で申し訳ないのですが、ご教授いただければと思います。

以上、よろしくおねがいいたします。

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 17, 2018, 5:50am UTC](https://discuss.elastic.co/t/aggregation/157269/2 "2018-12-17T05:50:37Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
