# Filter by \_version and show \_version in elasticsearch query

**URL:** https://discuss.elastic.co/t/filter-by--version-and-show--version-in-elasticsearch-query/22024
**Category:** Elasticsearch
**Created:** [February 7, 2015, 11:07am UTC](https://discuss.elastic.co/t/filter-by--version-and-show--version-in-elasticsearch-query/22024 "2015-02-07T11:07:43Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![es5z](https://avatars.discourse-cdn.com/v4/letter/e/a4c791/32.png) [@es5z](https://discuss.elastic.co/u/es5z)
#### Post date: [February 7, 2015, 11:07am UTC](https://discuss.elastic.co/t/filter-by--version-and-show--version-in-elasticsearch-query/22024/1 "2015-02-07T11:07:43Z")

</div>

How can I filter by the `_version` field in elasticsearch? I tried to run  
the following query (using YAML instead of JSON) to get documents with  
\_version greater than or equal to 4, but nothing is getting returned even  
though the document definitely has documents with that version number.

```
query:
    filtered:
        filter:
            range:
                _version:
                    gte: 4

```

I know `_version` is working because in general I can do aggregations on it  
like

```
aggs:
    versions:
        terms:
            field: _version

```

Also how can I get `_version` to appear in the list of fields? I try the  
following but only the `name` field is shown, not version.

```
fields:
    - name
    - _version

```

I haven't done anything in my mapping regarding \_version, do I need to  
customize it's mapping in some way before I can filter or display it?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/16d20efb-3967-43f8-a860-b694d70ef4ee%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/16d20efb-3967-43f8-a860-b694d70ef4ee%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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: [February 8, 2015, 6:48pm UTC](https://discuss.elastic.co/t/filter-by--version-and-show--version-in-elasticsearch-query/22024/2 "2015-02-08T18:48:10Z")

</div>

Hi,

While using the \_version might seem to work in certain cases, I would  
recommend to never use it for anything else than optimistic locking of  
updates. In particular, versions do not carry any meaning: they might look  
like the number of times a document has been modified but it is not always  
the case (for instance if you create a new document which has the same ID  
as a document that you just deleted, the version number of the new document  
will not be 1), and more importantly it is an implementation detail, this  
behaviour might change in the future.

On Sat, Feb 7, 2015 at 12:07 PM, es5z [veerukrishnan@hotmail.com](mailto:veerukrishnan@hotmail.com) wrote:

> How can I filter by the `_version` field in elasticsearch? I tried to run  
> the following query (using YAML instead of JSON) to get documents with  
> \_version greater than or equal to 4, but nothing is getting returned even  
> though the document definitely has documents with that version number.
> 
> ```
> query:
> filtered:
> filter:
> range:
> _version:
> gte: 4
> 
> ```
> 
> I know `_version` is working because in general I can do aggregations on  
> it like
> 
> ```
> aggs:
> versions:
> terms:
> field: _version
> 
> ```
> 
> Also how can I get `_version` to appear in the list of fields? I try the  
> following but only the `name` field is shown, not version.
> 
> ```
> fields:
> - name
> - _version
> 
> ```
> 
> I haven't done anything in my mapping regarding \_version, do I need to  
> customize it's mapping in some way before I can filter or display it?
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/16d20efb-3967-43f8-a860-b694d70ef4ee%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/16d20efb-3967-43f8-a860-b694d70ef4ee%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/16d20efb-3967-43f8-a860-b694d70ef4ee%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/16d20efb-3967-43f8-a860-b694d70ef4ee%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
Adrien Grand

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j6RCOK350GjQLOEu%2B1ZgMZMTvsYG61%3DxKfX6Sj%3D6bs%3DDA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j6RCOK350GjQLOEu%2B1ZgMZMTvsYG61%3DxKfX6Sj%3D6bs%3DDA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![es5z](https://avatars.discourse-cdn.com/v4/letter/e/a4c791/32.png) [@es5z](https://discuss.elastic.co/u/es5z)
#### Post date: [February 16, 2015, 5:48pm UTC](https://discuss.elastic.co/t/filter-by--version-and-show--version-in-elasticsearch-query/22024/3 "2015-02-16T17:48:01Z")

</div>

Wasn't aware it's an implementation detail, thanks. That being said, does  
it mean there's no way to list \_version in the fields that have been  
output, or to filter by them? I'd assume that since aggregations with them  
work that there'd be a way to filter and view it.

On Sunday, February 8, 2015 at 7:48:26 PM UTC+1, Adrien Grand wrote:

> Hi,
> 
> While using the \_version might seem to work in certain cases, I would  
> recommend to never use it for anything else than optimistic locking of  
> updates. In particular, versions do not carry any meaning: they might look  
> like the number of times a document has been modified but it is not always  
> the case (for instance if you create a new document which has the same ID  
> as a document that you just deleted, the version number of the new document  
> will not be 1), and more importantly it is an implementation detail, this  
> behaviour might change in the future.
> 
> On Sat, Feb 7, 2015 at 12:07 PM, es5z \<[veeruk...@hotmail.com](mailto:veeruk...@hotmail.com) \<javascript:\>
> 
> > wrote:
> 
> > How can I filter by the `_version` field in elasticsearch? I tried to run  
> > the following query (using YAML instead of JSON) to get documents with  
> > \_version greater than or equal to 4, but nothing is getting returned even  
> > though the document definitely has documents with that version number.
> > 
> > ```
> > query:
> > filtered:
> > filter:
> > range:
> > _version:
> > gte: 4
> > 
> > ```
> > 
> > I know `_version` is working because in general I can do aggregations on  
> > it like
> > 
> > ```
> > aggs:
> > versions:
> > terms:
> > field: _version
> > 
> > ```
> > 
> > Also how can I get `_version` to appear in the list of fields? I try the  
> > following but only the `name` field is shown, not version.
> > 
> > ```
> > fields:
> > - name
> > - _version
> > 
> > ```
> > 
> > I haven't done anything in my mapping regarding \_version, do I need to  
> > customize it's mapping in some way before I can filter or display it?
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/16d20efb-3967-43f8-a860-b694d70ef4ee%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/16d20efb-3967-43f8-a860-b694d70ef4ee%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/16d20efb-3967-43f8-a860-b694d70ef4ee%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/16d20efb-3967-43f8-a860-b694d70ef4ee%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> --  
> Adrien Grand

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/c7df181d-179c-4480-bfeb-322a9497c611%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/c7df181d-179c-4480-bfeb-322a9497c611%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 12:32am UTC](https://discuss.elastic.co/t/filter-by--version-and-show--version-in-elasticsearch-query/22024/4 "2017-07-06T00:32:24Z")

</div>


