### Description

There is a hard limit on the size of terms that can be indexed in lucene.  However, we don't check this at query time, which can lead to memory issues if the term query in question is case insensitive.  Very long terms are normally a sign of user error, so we should check up-front the length of the input and throw and exception if it is greater than `IndexWriter.MAX_TERM_LENGTH`.
server/src/main/java/org/elasticsearch/common/lucene/BytesRefs.java
/**
     * Converts a value to a string, taking special care if its a {@link BytesRef} to call
     * {@link org.apache.lucene.util.BytesRef#utf8ToString()}.
     */
    public static String toString(Object value) {
        if (value == null) {
            return null;
        }
        if (value instanceof BytesRef) {
            return ((BytesRef) value).utf8ToString();
        }
        return value.toString();
    }
    /**
     * Converts an object value to BytesRef.
     */
    public static BytesRef toBytesRef(Object value) {
        if (value == null) {
            return null;
        }
        if (value instanceof BytesRef) {
            return (BytesRef) value;
        }
        return new BytesRef(value.toString());
    }
}