# Stored filters using sequence numbers

**URL:** <https://discuss.elastic.co/t/stored-filters-using-sequence-numbers/97816>\
**Category:** Elasticsearch\
**Created:** [August 21, 2017, 7:03pm UTC](https://discuss.elastic.co/t/stored-filters-using-sequence-numbers/97816 "2017-08-21T19:03:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ljcollins25](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ljcollins25/32/21347_2.png) [@ljcollins25](https://discuss.elastic.co/u/ljcollins25)\
**Post date:** [August 21, 2017, 7:03pm UTC](https://discuss.elastic.co/t/stored-filters-using-sequence-numbers/97816/1 "2017-08-21T19:03:40Z")

</div>

I am working on a set of changes to ElasticSearch which allow storing a filter for documents matching a query using sequence numbers. I would like to get feedback on my approach.

This is implemented with these changes:

1. Add special query field type and \_stored\_filter meta field whose content is a query.
2. When a document with \_stored\_filter\_query is encountered another field is added which stores the sorted sequence numbers for the documents matching the query.
3. Introduce stored\_filter\_query which finds the document with the id specified in the query and loads the sequence numbers, and uses a points intersection to find documents with the sequence numbers.

A few questions arise:

- This approach requires that a stored filter document is stored on each shard. This can be done by client or by server. Currently, I am relying on the client to partitioning by shard id and sending a request to store the stored filter on that shard. Is there an issue in having index requests or searches return the shard id for the documents they return?

- Are sequence numbers unique within a shard or is the pair (primary term, sequence number) the only thing guaranteed to be unique?

---

<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:** [September 18, 2017, 7:04pm UTC](https://discuss.elastic.co/t/stored-filters-using-sequence-numbers/97816/2 "2017-09-18T19:04:05Z")

</div>

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