# How to exclude records in between two records in elasticsearch?

**URL:** <https://discuss.elastic.co/t/how-to-exclude-records-in-between-two-records-in-elasticsearch/95670>\
**Category:** Elasticsearch\
**Created:** [August 3, 2017, 8:58am UTC](https://discuss.elastic.co/t/how-to-exclude-records-in-between-two-records-in-elasticsearch/95670 "2017-08-03T08:58:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![patrickian](https://avatars.discourse-cdn.com/v4/letter/p/c57346/32.png) [@patrickian](https://discuss.elastic.co/u/patrickian)\
**Post date:** [August 3, 2017, 8:58am UTC](https://discuss.elastic.co/t/how-to-exclude-records-in-between-two-records-in-elasticsearch/95670/1 "2017-08-03T08:58:39Z")

</div>

Let's say I have 10 documents in an index. document 2 has the k-v pair:  
`'activity': 'start'` and document 8 has `'activity':'end'`. I want to exclude all documents between and including documents 2 and 8. How can I do this in elasticsearch?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [August 3, 2017, 9:14am UTC](https://discuss.elastic.co/t/how-to-exclude-records-in-between-two-records-in-elasticsearch/95670/2 "2017-08-03T09:14:22Z")

</div>

I don't think there is an easy way to do that.  
I believe you need to think about your model in a different way and basically solve that problem at index time instead.

---

<div class="post-metadata">

**Author:** ![patrickian](https://avatars.discourse-cdn.com/v4/letter/p/c57346/32.png) [@patrickian](https://discuss.elastic.co/u/patrickian)\
**Post date:** [August 3, 2017, 9:19am UTC](https://discuss.elastic.co/t/how-to-exclude-records-in-between-two-records-in-elasticsearch/95670/3 "2017-08-03T09:19:12Z")

</div>

If it helps, these documents have a timestamp field. Is there a way to get the timestamp of document 2 and document 8 and use it in a range filter (in a single query)?

What I had in mind requires more complex steps. I'd first query the timestamps of the 2 documents, then do another query that has a range filter.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [August 3, 2017, 12:59pm UTC](https://discuss.elastic.co/t/how-to-exclude-records-in-between-two-records-in-elasticsearch/95670/4 "2017-08-03T12:59:07Z")

</div>

> Is there a way to get the timestamp of document 2 and document 8 and use it in a range filter (in a single query)?

I don't think so.

---

<div class="post-metadata">

**Author:** ![patrickian](https://avatars.discourse-cdn.com/v4/letter/p/c57346/32.png) [@patrickian](https://discuss.elastic.co/u/patrickian)\
**Post date:** [August 3, 2017, 1:13pm UTC](https://discuss.elastic.co/t/how-to-exclude-records-in-between-two-records-in-elasticsearch/95670/5 "2017-08-03T13:13:08Z")

</div>

I'll have to do 2 separate queries then?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [August 3, 2017, 3:16pm UTC](https://discuss.elastic.co/t/how-to-exclude-records-in-between-two-records-in-elasticsearch/95670/6 "2017-08-03T15:16:30Z")

</div>

May be. Probably.

I don't exactly know the use case. I mean what problem you are trying to solve.  
You always wrote things about how you want to solve it, the HOW. Not the WHAT.

As I said, I think you should may be "solve that problem at index time instead."

In case it helps, there is the Multi Search API: [https://www.elastic.co/guide/en/elasticsearch/reference/5.5/search-multi-search.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.5/search-multi-search.html)

But you can't take the result of a first query as the input of the second one.

---

<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:** [August 31, 2017, 3:16pm UTC](https://discuss.elastic.co/t/how-to-exclude-records-in-between-two-records-in-elasticsearch/95670/7 "2017-08-31T15:16:31Z")

</div>

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