Skip to content

Commit

Permalink
Better sizing BytesRefs for Strings in Queries
Browse files Browse the repository at this point in the history
  • Loading branch information
piergm committed Oct 25, 2024
1 parent 6e0bdbe commit 3a56144
Showing 1 changed file with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.apache.lucene.search.NamedMatches;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.UnicodeUtil;
import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
Expand Down Expand Up @@ -216,12 +217,12 @@ public final int hashCode() {
* @return the same input object or a {@link BytesRef} representation if input was of type string
*/
static Object maybeConvertToBytesRef(Object obj) {
if (obj instanceof String) {
return BytesRefs.checkIndexableLength(BytesRefs.toBytesRef(obj));
} else if (obj instanceof CharBuffer) {
return BytesRefs.checkIndexableLength(new BytesRef((CharBuffer) obj));
} else if (obj instanceof BigInteger) {
return BytesRefs.toBytesRef(obj);
if (obj instanceof String v) {
return BytesRefs.checkIndexableLength(new BytesRef(new byte[UnicodeUtil.calcUTF16toUTF8Length(v, 0, v.length())]));
} else if (obj instanceof CharBuffer v) {
return BytesRefs.checkIndexableLength(new BytesRef(v));
} else if (obj instanceof BigInteger v) {
return BytesRefs.toBytesRef(v);
}
return obj;
}
Expand Down

0 comments on commit 3a56144

Please sign in to comment.