# Length filter: array index out of bounds exception

**URL:** <https://discuss.elastic.co/t/length-filter-array-index-out-of-bounds-exception/225261>\
**Category:** Elasticsearch\
**Created:** [March 26, 2020, 5:51pm UTC](https://discuss.elastic.co/t/length-filter-array-index-out-of-bounds-exception/225261 "2020-03-26T17:51:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jetnet](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@jetnet](https://discuss.elastic.co/u/jetnet)\
**Post date:** [March 26, 2020, 5:51pm UTC](https://discuss.elastic.co/t/length-filter-array-index-out-of-bounds-exception/225261/1 "2020-03-26T17:51:54Z")

</div>

I encountered a strange issue, when a query fails depending on the position of a term and only then the `length` filter is active (see below):

1. works:

```auto
GET test/_search?filter_path=**.productNumber
{
  "query": {
    "match": {
      "productNumber": {
        "query": "abc def ghij 3d"
      }
    }
  }
}

```

1. fails

```auto
GET test/_search?filter_path=**.productNumber
{
  "query": {
    "match": {
      "productNumber": {
        "query": "abc def 3d ghij"
      }
    }
  }
}

```

Exception:

```auto
          "caused_by" : {
            "type" : "array_index_out_of_bounds_exception",
            "reason" : "Index 0 out of bounds for length 0"
          }

```

ES versions tested: 7.5, 7.6

Index settings:

```auto
PUT test
{
  "settings": {
    "number_of_shards": "1",
    "number_of_replicas": "0",
    "analysis": {
      "filter": {
        "length_min_2": {
          "type": "length",
          "min": 2
        },
        "word_split_product_number": {
          "type": "word_delimiter_graph",
          "split_on_numerics": true,
          "generate_number_parts": true,
          "catenate_words": true,
          "catenate_numbers": true,
          "catenate_all": true,
          "preserve_original": true
        }
      },
      "analyzer": {
        "word_split_product_number_analyzer": {
          "filter": [
            "lowercase",
            "word_split_product_number",
            "length_min_2"
          ],
          "tokenizer": "standard"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "productNumber": {
        "type": "text",
        "analyzer": "word_split_product_number_analyzer"
      }
    }
  }
}

```

Test docs:

```auto
PUT test/_bulk
{"index":{}}
{"productNumber":"ABC-DEF-GHIJ-3A"}
{"index":{}}
{"productNumber":"ABC-DEF-GHIJ-3B"}
{"index":{}}
{"productNumber":"ABC-DEF-GHIJ-3C"}
{"index":{}}
{"productNumber":"ABC-DEF-GHIJ-3D"}

```

---

<div class="post-metadata">

**Author:** ![jetnet](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@jetnet](https://discuss.elastic.co/u/jetnet)\
**Post date:** [March 26, 2020, 9:11pm UTC](https://discuss.elastic.co/t/length-filter-array-index-out-of-bounds-exception/225261/2 "2020-03-26T21:11:49Z")

</div>

I found the following solution as workaround, but it'd be great to have the `length` filter working too:  
instead of:

```auto
        "length_min_2": {
          "type": "length",
          "min": 2
        },

```

use those:

```auto
        "stop_empty": {
          "type": "stop",
          "stopwords": [""]
        },
        "pattern_length_min_2": {
          "type": "pattern_replace",
          "pattern": "^.$",
          "replacement": ""
        },

...
      "analyzer": {
        "word_split_product_number_analyzer": {
          "filter": [
            "lowercase"
            ,"word_split_product_number"
            ,"pattern_length_min_2"
            ,"stop_empty"
            ,"unique"
          ],
          "tokenizer": "whitespace"
        }
      }

```

---

<div class="post-metadata">

**Author:** ![jetnet](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@jetnet](https://discuss.elastic.co/u/jetnet)\
**Post date:** [March 27, 2020, 6:52am UTC](https://discuss.elastic.co/t/length-filter-array-index-out-of-bounds-exception/225261/3 "2020-03-27T06:52:00Z")

</div>

The "workaround" above does not work today ☹  
I'm getting the same error as above. It seems I made some mistakes during testing...

UPDATE:  
today's workaround: use the combination `word_delimiter` (not `graph`!) + `flatten_graph`  
So, a bug in a graph token stream?

---

<div class="post-metadata">

**Author:** ![jetnet](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@jetnet](https://discuss.elastic.co/u/jetnet)\
**Post date:** [March 30, 2020, 2:51pm UTC](https://discuss.elastic.co/t/length-filter-array-index-out-of-bounds-exception/225261/4 "2020-03-30T14:51:22Z")

</div>

Any feedback from the ES engineers? Thanks!

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [March 30, 2020, 3:08pm UTC](https://discuss.elastic.co/t/length-filter-array-index-out-of-bounds-exception/225261/5 "2020-03-30T15:08:38Z")

</div>

I opened [https://github.com/elastic/elasticsearch/issues/54434](https://github.com/elastic/elasticsearch/issues/54434) - as the least thing that should happen is either a proper exception or a fix 🙂

---

<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:** [April 27, 2020, 3:08pm UTC](https://discuss.elastic.co/t/length-filter-array-index-out-of-bounds-exception/225261/6 "2020-04-27T15:08:53Z")

</div>

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