# Need to get documents with field have length \> 200 words

**URL:** https://discuss.elastic.co/t/need-to-get-documents-with-field-have-length-200-words/312432
**Category:** Kibana
**Tags:** painless
**Created:** [August 19, 2022, 6:42am UTC](https://discuss.elastic.co/t/need-to-get-documents-with-field-have-length-200-words/312432 "2022-08-19T06:42:06Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![jsanz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jsanz/32/53734_2.png) [@jsanz](https://discuss.elastic.co/u/jsanz)
#### Post date: [September 7, 2022, 3:04pm UTC](https://discuss.elastic.co/t/need-to-get-documents-with-field-have-length-200-words/312432/2 "2022-09-07T15:04:01Z")

</div>

This should work, tested on the kibana flights sample dataset to search for documents where the `Dest` field has more than `55` characters. It uses a [Runtime Field](https://www.elastic.co/guide/en/elasticsearch/reference/8.4/runtime.html) to compute the length and then a regular search by range. Mind that this procedure works with `keyword` fields but not with `text` fields where you should compute the length at ingest (or reindexing) time, check this [answer](https://discuss.elastic.co/t/how-to-filter-docs-by-text-field-length/242941/2).

Also, mind that runtime fields have a cost in performance, so maybe again you should consider adding this to your ingest process if you are going to use it a lot.

```auto

GET kibana_sample_data_flights/_search
{
  "_source": [
    "Dest"
  ],
  "fields": [
    "DestLength"
  ],
  "runtime_mappings": {
    "DestLength": {
      "type": "long",
      "script": {
        "source": """
String name = doc['Dest'].value;
if (name != null){
  emit(name.length());
} else {
  emit(0)
}
      """
      }
    }
  },
  "query": {
    "bool": {
      "should": [
        {
          "range": {
            "DestLength": {
              "gt": "55"
            }
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

```

---

_[View the full topic](https://discuss.elastic.co/t/need-to-get-documents-with-field-have-length-200-words/312432)._
