Need to get documents with field have length > 200 words

Trying to find the documents having field values lenght > 200 characters

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 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.

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.


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
    }
  }
}

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