# 5.0: can't have Terms query within a Filter Aggregation?

**URL:** <https://discuss.elastic.co/t/5-0-cant-have-terms-query-within-a-filter-aggregation/64768>\
**Category:** Elasticsearch\
**Created:** [November 2, 2016, 8:45pm UTC](https://discuss.elastic.co/t/5-0-cant-have-terms-query-within-a-filter-aggregation/64768 "2016-11-02T20:45:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![IntendedEffect](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/intendedeffect/32/12917_2.png) [@IntendedEffect](https://discuss.elastic.co/u/IntendedEffect)\
**Post date:** [November 2, 2016, 8:45pm UTC](https://discuss.elastic.co/t/5-0-cant-have-terms-query-within-a-filter-aggregation/64768/1 "2016-11-02T20:45:26Z")

</div>

I have an application with a social stream feature a la Twitter. Users can "subscribe" to email notifications for people mentioned in posts, but that subscription doesn't affect what appears in a user's stream. So to display the stream, I need to:

1. Fetch the posts users should see

- Figure out which of those posts the user is set to receive emails about, to display a "toggle email notifications" button in the UI.

I don't want to store a list of subscribed users with each post, so instead there's an index of users that includes an array of subscribed-to people. In Elasticsearch 2.x, that let me accomplished the second task by doing a filter aggregation to see which posts, out of the ones due to be displayed, mentioned people that the user was subscribed to email notifications for. Then I'd do a terms aggregation to just get postIDs, which I'd use to to toggle that email subscription button.

Post mapping:

```
"post": {
  "properties": {
      "mentionIDs": {
            "type": "keyword"
       }
  }
}

```

User mapping:

```
"user": {
  "properties": {
     "notifications": {
       "type": "keyword"
     }
  }
}

```

And here's the tail end of my query against "items":

```
"aggs": {
  "itemsNotify": {
    "filter": {
      "terms": {
        "mentionIDs": {
          "index": "users",
          "type": "user",
          "id": "USR|CLIENTID|1234",
          "path": "notifications",
          "routing": "CLIENTID"
        }
      }
    },
    "aggs": {
      "notifyIDs": {
        "terms": {
          "field": "postID"
        }
      }
    }
   }
  }

```

This no longer works in Elasticsearch 5.0, which instead returns the error:

```
{
  "error": {
    "root_cause": [
      {
        "type": "unsupported_operation_exception",
        "reason": "unsupported_operation_exception: query must be rewritten first"
      },
      {
        "type": "unsupported_operation_exception",
        "reason": "query must be rewritten first"
      }
    ],
}

```

I'll admit that I don't understand that error. Is this kind of Terms Query (referencing terms stored in a separate index) within a Filter Aggregation no longer supported? Is there a better fallback strategy for me than just running a second query that takes the original and adds that terms query as a filter?

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [November 3, 2016, 10:33am UTC](https://discuss.elastic.co/t/5-0-cant-have-terms-query-within-a-filter-aggregation/64768/2 "2016-11-03T10:33:42Z")

</div>

Thanks for reporting this issue, I opened [https://github.com/elastic/elasticsearch/issues/21301](https://github.com/elastic/elasticsearch/issues/21301).

---

<div class="post-metadata">

**Author:** ![IntendedEffect](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/intendedeffect/32/12917_2.png) [@IntendedEffect](https://discuss.elastic.co/u/IntendedEffect)\
**Post date:** [November 3, 2016, 3:06pm UTC](https://discuss.elastic.co/t/5-0-cant-have-terms-query-within-a-filter-aggregation/64768/3 "2016-11-03T15:06:44Z")

</div>

Thanks! I've subscribed to the issue.

---

<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:** [July 5, 2017, 10:06pm UTC](https://discuss.elastic.co/t/5-0-cant-have-terms-query-within-a-filter-aggregation/64768/4 "2017-07-05T22:06:53Z")

</div>


