# Search By Document Length

**URL:** <https://discuss.elastic.co/t/search-by-document-length/64013>\
**Category:** Elasticsearch\
**Created:** [October 26, 2016, 2:10pm UTC](https://discuss.elastic.co/t/search-by-document-length/64013 "2016-10-26T14:10:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![krmathieu](https://avatars.discourse-cdn.com/v4/letter/k/b5e925/32.png) [@krmathieu](https://discuss.elastic.co/u/krmathieu)\
**Post date:** [October 26, 2016, 2:10pm UTC](https://discuss.elastic.co/t/search-by-document-length/64013/1 "2016-10-26T14:10:11Z")

</div>

I have a field called document.content that contains the overall text of the document that I am indexing and I want to return only those records where document.content is greater than a certain length. How would I go about doing that? I did come across some examples using scripts but really didn't understand the format of the query. Here is one example I saw and I was expecting to be able to do something more like document.content.length() \> 50:

{"query": {"constant\_score": {"filter" : {"script" : {"script": "doc["text"].values.size() \> 50" } } } }}}'

In this case above, I am not sure what that doc array reference is about or what the values is referring to. Also, I gather that there may be some issues using scripts in general (aside from the security concerns) for something like this so any suggestions on how to do this in the most performant way would be appreciated. Thanks.

Kevin

---

<div class="post-metadata">

**Author:** ![Camilo\_Sierra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/camilo_sierra/32/14397_2.png) [@Camilo\_Sierra](https://discuss.elastic.co/u/Camilo_Sierra)\
**Post date:** [October 26, 2016, 2:20pm UTC](https://discuss.elastic.co/t/search-by-document-length/64013/2 "2016-10-26T14:20:12Z")

</div>

it will be easy and faster if you stock the lengh of your text in the same document but new field "length\_doc" ( int value) , and in your query you filter \>\> range "length\_doc" \> 50 !

i hope it helps

---

<div class="post-metadata">

**Author:** ![krmathieu](https://avatars.discourse-cdn.com/v4/letter/k/b5e925/32.png) [@krmathieu](https://discuss.elastic.co/u/krmathieu)\
**Post date:** [October 26, 2016, 2:32pm UTC](https://discuss.elastic.co/t/search-by-document-length/64013/3 "2016-10-26T14:32:30Z")

</div>

Thanks for the suggestion. We have 1.8 million docs in our cluster and it took quite a while to get them in because of all the nested objects and we don't want to have to reprocess them if we can avoid it. Is there a way to add the field to the index and then dynamically update all length fields with a script call or something?

---

<div class="post-metadata">

**Author:** ![Camilo\_Sierra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/camilo_sierra/32/14397_2.png) [@Camilo\_Sierra](https://discuss.elastic.co/u/Camilo_Sierra)\
**Post date:** [October 26, 2016, 2:43pm UTC](https://discuss.elastic.co/t/search-by-document-length/64013/4 "2016-10-26T14:43:50Z")

</div>

you have the reindex API [https://www.elastic.co/guide/en/elasticsearch/reference/2.3/docs-reindex.html#\_reindex\_to\_change\_the\_name\_of\_a\_field](https://www.elastic.co/guide/en/elasticsearch/reference/2.3/docs-reindex.html#_reindex_to_change_the_name_of_a_field) , you can put your script and create a new field with the new count content !  
I know that is not the most easy way but think that you made your script only once (at the reindex time) and not in each search query !

---

<div class="post-metadata">

**Author:** ![krmathieu](https://avatars.discourse-cdn.com/v4/letter/k/b5e925/32.png) [@krmathieu](https://discuss.elastic.co/u/krmathieu)\
**Post date:** [October 26, 2016, 3:33pm UTC](https://discuss.elastic.co/t/search-by-document-length/64013/5 "2016-10-26T15:33:58Z")

</div>

Ok, thanks for the additional info, we will check it out.

---

<div class="post-metadata">

**Author:** ![xavierfacq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavierfacq/32/8744_2.png) [@xavierfacq](https://discuss.elastic.co/u/xavierfacq)\
**Post date:** [October 27, 2016, 7:12am UTC](https://discuss.elastic.co/t/search-by-document-length/64013/6 "2016-10-27T07:12:06Z")

</div>

Hello,

I think you can do it using a script.

Create a simple file in the script directory : ordered\_by\_length.groovy  
contains

```
        _source.your_field_name.length()

```

In you query you can use it:

```
    {
    "query": {
        "match_all": {}
    },
    "sort": {
        "_script": {
        "type": "number",
        "script": {
            "lang": "groovy",
            "file": "ordered_by_length"
        },
        "order": "asc"
        }
    },
    "from": 0,
    "size": 10,
    "aggs": {}
    }

```

I seems to work, but i will not be very fast I think... If you need to add a limit it's also possible  
to pass the value as param to the script.

Bye,  
Xavier

---

<div class="post-metadata">

**Author:** ![krmathieu](https://avatars.discourse-cdn.com/v4/letter/k/b5e925/32.png) [@krmathieu](https://discuss.elastic.co/u/krmathieu)\
**Post date:** [October 28, 2016, 3:37pm UTC](https://discuss.elastic.co/t/search-by-document-length/64013/7 "2016-10-28T15:37:51Z")

</div>

Thanks for the suggestion, we will check it out.

Kevin

---

<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:08pm UTC](https://discuss.elastic.co/t/search-by-document-length/64013/8 "2017-07-05T22:08:41Z")

</div>


