elastic / elasticsearch-rails

Elasticsearch integrations for ActiveModel/Record and Ruby on Rails
Apache License 2.0
3.07k stars 793 forks source link

NPE happens on short prefix query on a field with index_prefixes #1038

Open mattjarnevic opened 2 years ago

mattjarnevic commented 2 years ago

Running ElasticSearch version 7.17.5 Running latest release (7.2.1) of elasticsearch-rails and elasticsearch-model

When a prefix query run on a field with index_prefixes, if the length of characters in the query is [min_chars of the index_prefixes option] - 1, an error is responded. The root cause seems to be NullPointerException.

To Reproduce Steps to reproduce the behavior:

  1. Create an index which has a field with index_prefixes option; its min_chars is implicitly 2.
    PUT test
    {
    "mappings": {
    "properties": {
      "t": {
        "type": "text",
        "index_prefixes": {}
      }
    }
    }
    }
  2. Search by prefix query with one character on the field.
    GET test/_search
    {"query":{"prefix":{"t": "a"}}}
    An error is responded. It says the root cause is NullPointerException.
    {
    "error" : {
    "root_cause" : [
      {
        "type" : "query_shard_exception",
        "reason" : "failed to create query: Cannot invoke \"Object.hashCode()\" because \"this.rewriteMethod\" is null",
        "index" : "test",
        "index_uuid" : "XPqfSP1xT7WJt9Sxzt65qg"
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "test",
        "node" : "QA35rulpSza20Wsx6rNSEg",
        "reason" : {
          "type" : "query_shard_exception",
          "reason" : "failed to create query: Cannot invoke \"Object.hashCode()\" because \"this.rewriteMethod\" is null",
          "index" : "test",
          "index_uuid" : "XPqfSP1xT7WJt9Sxzt65qg",
          "caused_by" : {
            "type" : "null_pointer_exception",
            "reason" : "Cannot invoke \"Object.hashCode()\" because \"this.rewriteMethod\" is null"
          }
        }
      }
    ]
    },
    "status" : 400
    }

    simple_query_string also fails.

    # fails with the same error
    GET test/_search
    {"query":{"simple_query_string":{"fields":["t"], "query":"a*"}}}

    Expected behavior Run without errors. I know it is not efficient.

This issue was found in OpenSearch and has a very detailed explanation of the issue along with a fix. Copied the details over for the basics of when this occurs. https://github.com/opensearch-project/OpenSearch/pull/2879