diff --git a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java
index a655184f89e7c..6ba8394e1e598 100644
--- a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java
+++ b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java
@@ -274,7 +274,7 @@ public PercolateQueryBuilder(String field, String documentType, String indexedDo
}
if (documents.isEmpty() == false) {
if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
- documentXContentType = XContentType.readFrom(in);
+ documentXContentType = in.readEnum(XContentType.class);
} else {
documentXContentType = XContentFactory.xContentType(documents.iterator().next());
}
@@ -331,7 +331,7 @@ protected void doWriteTo(StreamOutput out) throws IOException {
out.writeOptionalBytesReference(doc);
}
if (documents.isEmpty() == false && out.getVersion().onOrAfter(Version.V_5_3_0)) {
- documentXContentType.writeTo(out);
+ out.writeEnum(documentXContentType);
}
}
diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/PutStoredScriptRequest.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/PutStoredScriptRequest.java
index e3392df885075..5ab5c49d0c791 100644
--- a/server/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/PutStoredScriptRequest.java
+++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/PutStoredScriptRequest.java
@@ -123,7 +123,7 @@ public void readFrom(StreamInput in) throws IOException {
id = in.readOptionalString();
content = in.readBytesReference();
if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
- xContentType = XContentType.readFrom(in);
+ xContentType = in.readEnum(XContentType.class);
} else {
xContentType = XContentFactory.xContentType(content);
}
@@ -145,7 +145,7 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeOptionalString(id);
out.writeBytesReference(content);
if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
- xContentType.writeTo(out);
+ out.writeEnum(xContentType);
}
if (out.getVersion().onOrAfter(Version.V_6_0_0_alpha2)) {
out.writeOptionalString(context);
diff --git a/server/src/main/java/org/elasticsearch/action/index/IndexRequest.java b/server/src/main/java/org/elasticsearch/action/index/IndexRequest.java
index 46d51ee0b40e4..f2ddca1955878 100644
--- a/server/src/main/java/org/elasticsearch/action/index/IndexRequest.java
+++ b/server/src/main/java/org/elasticsearch/action/index/IndexRequest.java
@@ -542,7 +542,11 @@ public void readFrom(StreamInput in) throws IOException {
pipeline = in.readOptionalString();
isRetry = in.readBoolean();
autoGeneratedTimestamp = in.readLong();
- contentType = in.readOptionalWriteable(XContentType::readFrom);
+ if (in.readBoolean()) {
+ contentType = in.readEnum(XContentType.class);
+ } else {
+ contentType = null;
+ }
}
@Override
@@ -566,7 +570,12 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeOptionalString(pipeline);
out.writeBoolean(isRetry);
out.writeLong(autoGeneratedTimestamp);
- out.writeOptionalWriteable(contentType);
+ if (contentType != null) {
+ out.writeBoolean(true);
+ out.writeEnum(contentType);
+ } else {
+ out.writeBoolean(false);
+ }
}
@Override
diff --git a/server/src/main/java/org/elasticsearch/action/ingest/PutPipelineRequest.java b/server/src/main/java/org/elasticsearch/action/ingest/PutPipelineRequest.java
index 394349ca01691..10b19ed8cfa17 100644
--- a/server/src/main/java/org/elasticsearch/action/ingest/PutPipelineRequest.java
+++ b/server/src/main/java/org/elasticsearch/action/ingest/PutPipelineRequest.java
@@ -81,7 +81,7 @@ public void readFrom(StreamInput in) throws IOException {
id = in.readString();
source = in.readBytesReference();
if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
- xContentType = XContentType.readFrom(in);
+ xContentType = in.readEnum(XContentType.class);
} else {
xContentType = XContentFactory.xContentType(source);
}
@@ -93,7 +93,7 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeString(id);
out.writeBytesReference(source);
if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
- xContentType.writeTo(out);
+ out.writeEnum(xContentType);
}
}
}
diff --git a/server/src/main/java/org/elasticsearch/action/ingest/SimulatePipelineRequest.java b/server/src/main/java/org/elasticsearch/action/ingest/SimulatePipelineRequest.java
index 170f0bc8518cf..428df00a68e9c 100644
--- a/server/src/main/java/org/elasticsearch/action/ingest/SimulatePipelineRequest.java
+++ b/server/src/main/java/org/elasticsearch/action/ingest/SimulatePipelineRequest.java
@@ -76,7 +76,7 @@ public SimulatePipelineRequest(BytesReference source, XContentType xContentType)
verbose = in.readBoolean();
source = in.readBytesReference();
if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
- xContentType = XContentType.readFrom(in);
+ xContentType = in.readEnum(XContentType.class);
} else {
xContentType = XContentFactory.xContentType(source);
}
@@ -123,7 +123,7 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeBoolean(verbose);
out.writeBytesReference(source);
if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
- xContentType.writeTo(out);
+ out.writeEnum(xContentType);
}
}
diff --git a/server/src/main/java/org/elasticsearch/action/termvectors/TermVectorsRequest.java b/server/src/main/java/org/elasticsearch/action/termvectors/TermVectorsRequest.java
index 9a26e63f1afd4..3aa7b832f1f4e 100644
--- a/server/src/main/java/org/elasticsearch/action/termvectors/TermVectorsRequest.java
+++ b/server/src/main/java/org/elasticsearch/action/termvectors/TermVectorsRequest.java
@@ -516,7 +516,7 @@ public void readFrom(StreamInput in) throws IOException {
if (in.readBoolean()) {
doc = in.readBytesReference();
if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
- xContentType = XContentType.readFrom(in);
+ xContentType = in.readEnum(XContentType.class);
} else {
xContentType = XContentFactory.xContentType(doc);
}
@@ -561,7 +561,7 @@ public void writeTo(StreamOutput out) throws IOException {
if (doc != null) {
out.writeBytesReference(doc);
if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
- xContentType.writeTo(out);
+ out.writeEnum(xContentType);
}
}
out.writeOptionalString(routing);
diff --git a/server/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java b/server/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java
index 9274bfbf345ae..e673c2a4b7ca2 100644
--- a/server/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java
+++ b/server/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java
@@ -593,7 +593,7 @@ public XContentBuilder field(String name, BytesRef value) throws IOException {
/**
* Writes the binary content of the given {@link BytesRef} as UTF-8 bytes.
*
- * Use {@link XContentParser#utf8Bytes()} to read the value back
+ * Use {@link XContentParser#charBuffer()} to read the value back
*/
public XContentBuilder utf8Field(String name, BytesRef value) throws IOException {
return field(name).utf8Value(value);
@@ -615,7 +615,7 @@ public XContentBuilder binaryValue(BytesRef value) throws IOException {
/**
* Writes the binary content of the given {@link BytesRef} as UTF-8 bytes.
*
- * Use {@link XContentParser#utf8Bytes()} to read the value back
+ * Use {@link XContentParser#charBuffer()} to read the value back
*/
public XContentBuilder utf8Value(BytesRef value) throws IOException {
if (value == null) {
diff --git a/server/src/main/java/org/elasticsearch/common/xcontent/XContentParser.java b/server/src/main/java/org/elasticsearch/common/xcontent/XContentParser.java
index dbeb678f3afad..a9037b74ce9ed 100644
--- a/server/src/main/java/org/elasticsearch/common/xcontent/XContentParser.java
+++ b/server/src/main/java/org/elasticsearch/common/xcontent/XContentParser.java
@@ -19,10 +19,9 @@
package org.elasticsearch.common.xcontent;
-import org.apache.lucene.util.BytesRef;
-import org.elasticsearch.common.lease.Releasable;
-
+import java.io.Closeable;
import java.io.IOException;
+import java.nio.CharBuffer;
import java.util.List;
import java.util.Map;
@@ -37,7 +36,7 @@
* NamedXContentRegistry.EMPTY, ParserField."{\"key\" : \"value\"}");
*
*/
-public interface XContentParser extends Releasable {
+public interface XContentParser extends Closeable {
enum Token {
START_OBJECT {
@@ -144,17 +143,13 @@ enum NumberType {
String textOrNull() throws IOException;
- /**
- * Returns a BytesRef holding UTF-8 bytes or null if a null value is {@link Token#VALUE_NULL}.
- * This method should be used to read text only binary content should be read through {@link #binaryValue()}
- */
- BytesRef utf8BytesOrNull() throws IOException;
+ CharBuffer charBufferOrNull() throws IOException;
/**
- * Returns a BytesRef holding UTF-8 bytes.
+ * Returns a {@link CharBuffer} holding UTF-8 bytes.
* This method should be used to read text only binary content should be read through {@link #binaryValue()}
*/
- BytesRef utf8Bytes() throws IOException;
+ CharBuffer charBuffer() throws IOException;
Object objectText() throws IOException;
@@ -248,8 +243,6 @@ enum NumberType {
*
* these methods write UTF-8 encoded strings and must be read through:
*
- * - {@link XContentParser#utf8Bytes()}
- * - {@link XContentParser#utf8BytesOrNull()}}
* - {@link XContentParser#text()} ()}
* - {@link XContentParser#textOrNull()} ()}
* - {@link XContentParser#textCharacters()} ()}}
diff --git a/server/src/main/java/org/elasticsearch/common/xcontent/XContentType.java b/server/src/main/java/org/elasticsearch/common/xcontent/XContentType.java
index 40caa6e911087..7196fdbf984e7 100644
--- a/server/src/main/java/org/elasticsearch/common/xcontent/XContentType.java
+++ b/server/src/main/java/org/elasticsearch/common/xcontent/XContentType.java
@@ -19,22 +19,18 @@
package org.elasticsearch.common.xcontent;
-import org.elasticsearch.common.io.stream.StreamInput;
-import org.elasticsearch.common.io.stream.StreamOutput;
-import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.cbor.CborXContent;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.common.xcontent.smile.SmileXContent;
import org.elasticsearch.common.xcontent.yaml.YamlXContent;
-import java.io.IOException;
import java.util.Locale;
import java.util.Objects;
/**
* The content type of {@link org.elasticsearch.common.xcontent.XContent}.
*/
-public enum XContentType implements Writeable {
+public enum XContentType {
/**
* A JSON based content type.
@@ -183,18 +179,4 @@ public String mediaType() {
public abstract String mediaTypeWithoutParameters();
- public static XContentType readFrom(StreamInput in) throws IOException {
- int index = in.readVInt();
- for (XContentType contentType : values()) {
- if (index == contentType.index) {
- return contentType;
- }
- }
- throw new IllegalStateException("Unknown XContentType with index [" + index + "]");
- }
-
- @Override
- public void writeTo(StreamOutput out) throws IOException {
- out.writeVInt(index);
- }
}
diff --git a/server/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentParser.java b/server/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentParser.java
index d1075dd0173b0..70461e07ea151 100644
--- a/server/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentParser.java
+++ b/server/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentParser.java
@@ -23,7 +23,6 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
-import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.IOUtils;
import org.elasticsearch.common.xcontent.DeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
@@ -88,8 +87,8 @@ public String text() throws IOException {
}
@Override
- public BytesRef utf8Bytes() throws IOException {
- return new BytesRef(CharBuffer.wrap(parser.getTextCharacters(), parser.getTextOffset(), parser.getTextLength()));
+ public CharBuffer charBuffer() throws IOException {
+ return CharBuffer.wrap(parser.getTextCharacters(), parser.getTextOffset(), parser.getTextLength());
}
@Override
@@ -114,7 +113,7 @@ public Object objectText() throws IOException {
public Object objectBytes() throws IOException {
JsonToken currentToken = parser.getCurrentToken();
if (currentToken == JsonToken.VALUE_STRING) {
- return utf8Bytes();
+ return charBuffer();
} else if (currentToken == JsonToken.VALUE_NUMBER_INT || currentToken == JsonToken.VALUE_NUMBER_FLOAT) {
return parser.getNumberValue();
} else if (currentToken == JsonToken.VALUE_TRUE) {
@@ -124,8 +123,7 @@ public Object objectBytes() throws IOException {
} else if (currentToken == JsonToken.VALUE_NULL) {
return null;
} else {
- //TODO should this really do UTF-8 conversion?
- return utf8Bytes();
+ return charBuffer();
}
}
diff --git a/server/src/main/java/org/elasticsearch/common/xcontent/support/AbstractXContentParser.java b/server/src/main/java/org/elasticsearch/common/xcontent/support/AbstractXContentParser.java
index 6d41c5b83da12..008dca1b537ca 100644
--- a/server/src/main/java/org/elasticsearch/common/xcontent/support/AbstractXContentParser.java
+++ b/server/src/main/java/org/elasticsearch/common/xcontent/support/AbstractXContentParser.java
@@ -19,7 +19,6 @@
package org.elasticsearch.common.xcontent.support;
-import org.apache.lucene.util.BytesRef;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.Booleans;
import org.elasticsearch.common.Numbers;
@@ -28,6 +27,7 @@
import org.elasticsearch.common.xcontent.XContentParser;
import java.io.IOException;
+import java.nio.CharBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
@@ -240,13 +240,12 @@ public final String textOrNull() throws IOException {
return text();
}
-
@Override
- public BytesRef utf8BytesOrNull() throws IOException {
+ public CharBuffer charBufferOrNull() throws IOException {
if (currentToken() == Token.VALUE_NULL) {
return null;
}
- return utf8Bytes();
+ return charBuffer();
}
@Override
diff --git a/server/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java
index f7334df8e0e84..d1337d5258aa9 100644
--- a/server/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java
+++ b/server/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java
@@ -37,6 +37,7 @@
import org.elasticsearch.common.xcontent.XContentParser;
import java.io.IOException;
+import java.nio.CharBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@@ -194,27 +195,31 @@ public final int hashCode() {
protected abstract int doHashCode();
/**
- * This helper method checks if the object passed in is a string, if so it
- * converts it to a {@link BytesRef}.
+ * This helper method checks if the object passed in is a string or {@link CharBuffer},
+ * if so it converts it to a {@link BytesRef}.
* @param obj the input object
* @return the same input object or a {@link BytesRef} representation if input was of type string
*/
- static Object convertToBytesRefIfString(Object obj) {
+ static Object maybeConvertToBytesRef(Object obj) {
if (obj instanceof String) {
return BytesRefs.toBytesRef(obj);
+ } else if (obj instanceof CharBuffer) {
+ return new BytesRef((CharBuffer) obj);
}
return obj;
}
/**
- * This helper method checks if the object passed in is a {@link BytesRef}, if so it
- * converts it to a utf8 string.
+ * This helper method checks if the object passed in is a {@link BytesRef} or {@link CharBuffer},
+ * if so it converts it to a utf8 string.
* @param obj the input object
- * @return the same input object or a utf8 string if input was of type {@link BytesRef}
+ * @return the same input object or a utf8 string if input was of type {@link BytesRef} or {@link CharBuffer}
*/
- static Object convertToStringIfBytesRef(Object obj) {
+ static Object maybeConvertToString(Object obj) {
if (obj instanceof BytesRef) {
return ((BytesRef) obj).utf8ToString();
+ } else if (obj instanceof CharBuffer) {
+ return new BytesRef((CharBuffer) obj).utf8ToString();
}
return obj;
}
diff --git a/server/src/main/java/org/elasticsearch/index/query/BaseTermQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/BaseTermQueryBuilder.java
index d8ca025dafa86..a3ef36204f032 100644
--- a/server/src/main/java/org/elasticsearch/index/query/BaseTermQueryBuilder.java
+++ b/server/src/main/java/org/elasticsearch/index/query/BaseTermQueryBuilder.java
@@ -116,7 +116,7 @@ public BaseTermQueryBuilder(String fieldName, Object value) {
throw new IllegalArgumentException("value cannot be null");
}
this.fieldName = fieldName;
- this.value = convertToBytesRefIfString(value);
+ this.value = maybeConvertToBytesRef(value);
}
/**
@@ -144,14 +144,14 @@ public String fieldName() {
* If necessary, converts internal {@link BytesRef} representation back to string.
*/
public Object value() {
- return convertToStringIfBytesRef(this.value);
+ return maybeConvertToString(this.value);
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(getName());
builder.startObject(fieldName);
- builder.field(VALUE_FIELD.getPreferredName(), convertToStringIfBytesRef(this.value));
+ builder.field(VALUE_FIELD.getPreferredName(), maybeConvertToString(this.value));
printBoostAndQueryName(builder);
builder.endObject();
builder.endObject();
diff --git a/server/src/main/java/org/elasticsearch/index/query/FuzzyQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/FuzzyQueryBuilder.java
index 91843ffbd3c9d..379f583ef6082 100644
--- a/server/src/main/java/org/elasticsearch/index/query/FuzzyQueryBuilder.java
+++ b/server/src/main/java/org/elasticsearch/index/query/FuzzyQueryBuilder.java
@@ -153,7 +153,7 @@ public FuzzyQueryBuilder(String fieldName, Object value) {
throw new IllegalArgumentException("query value cannot be null");
}
this.fieldName = fieldName;
- this.value = convertToBytesRefIfString(value);
+ this.value = maybeConvertToBytesRef(value);
}
/**
@@ -186,7 +186,7 @@ public String fieldName() {
}
public Object value() {
- return convertToStringIfBytesRef(this.value);
+ return maybeConvertToString(this.value);
}
public FuzzyQueryBuilder fuzziness(Fuzziness fuzziness) {
@@ -238,7 +238,7 @@ public String rewrite() {
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
builder.startObject(fieldName);
- builder.field(VALUE_FIELD.getPreferredName(), convertToStringIfBytesRef(this.value));
+ builder.field(VALUE_FIELD.getPreferredName(), maybeConvertToString(this.value));
fuzziness.toXContent(builder, params);
builder.field(PREFIX_LENGTH_FIELD.getPreferredName(), prefixLength);
builder.field(MAX_EXPANSIONS_FIELD.getPreferredName(), maxExpansions);
@@ -274,9 +274,9 @@ public static FuzzyQueryBuilder fromXContent(XContentParser parser) throws IOExc
currentFieldName = parser.currentName();
} else if (token.isValue()) {
if (TERM_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
- value = parser.objectBytes();
+ value = maybeConvertToBytesRef(parser.objectBytes());
} else if (VALUE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
- value = parser.objectBytes();
+ value = maybeConvertToBytesRef(parser.objectBytes());
} else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
boost = parser.floatValue();
} else if (Fuzziness.FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
@@ -303,7 +303,7 @@ public static FuzzyQueryBuilder fromXContent(XContentParser parser) throws IOExc
} else {
throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, parser.currentName());
fieldName = parser.currentName();
- value = parser.objectBytes();
+ value = maybeConvertToBytesRef(parser.objectBytes());
}
}
return new FuzzyQueryBuilder(fieldName, value)
diff --git a/server/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java
index a94c2dae283a5..d0aa588aeb997 100644
--- a/server/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java
+++ b/server/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java
@@ -221,7 +221,7 @@ public Item(@Nullable String index, @Nullable String type, XContentBuilder doc)
if (in.readBoolean()) {
doc = (BytesReference) in.readGenericValue();
if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
- xContentType = XContentType.readFrom(in);
+ xContentType = in.readEnum(XContentType.class);
} else {
xContentType = XContentFactory.xContentType(doc);
}
@@ -243,7 +243,7 @@ public void writeTo(StreamOutput out) throws IOException {
if (doc != null) {
out.writeGenericValue(doc);
if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
- xContentType.writeTo(out);
+ out.writeEnum(xContentType);
}
} else {
out.writeString(id);
diff --git a/server/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java
index 9cf008e1b1b2e..48f7f9335ab13 100644
--- a/server/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java
+++ b/server/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java
@@ -162,7 +162,7 @@ public String fieldName() {
* of query to be equal regardless of whether it was created from XContent or via Java API.
*/
public RangeQueryBuilder from(Object from, boolean includeLower) {
- this.from = convertToBytesRefIfString(from);
+ this.from = maybeConvertToBytesRef(from);
this.includeLower = includeLower;
return this;
}
@@ -178,7 +178,7 @@ public RangeQueryBuilder from(Object from) {
* Gets the lower range value for this query.
*/
public Object from() {
- return convertToStringIfBytesRef(this.from);
+ return maybeConvertToString(this.from);
}
/**
@@ -199,7 +199,7 @@ public RangeQueryBuilder gte(Object from) {
* The to part of the range query. Null indicates unbounded.
*/
public RangeQueryBuilder to(Object to, boolean includeUpper) {
- this.to = convertToBytesRefIfString(to);
+ this.to = maybeConvertToBytesRef(to);
this.includeUpper = includeUpper;
return this;
}
@@ -218,7 +218,7 @@ public RangeQueryBuilder to(Object to) {
* of query to be equal regardless of whether it was created from XContent or via Java API.
*/
public Object to() {
- return convertToStringIfBytesRef(this.to);
+ return maybeConvertToString(this.to);
}
/**
@@ -334,8 +334,8 @@ public RangeQueryBuilder relation(String relation) {
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
builder.startObject(fieldName);
- builder.field(FROM_FIELD.getPreferredName(), convertToStringIfBytesRef(this.from));
- builder.field(TO_FIELD.getPreferredName(), convertToStringIfBytesRef(this.to));
+ builder.field(FROM_FIELD.getPreferredName(), maybeConvertToString(this.from));
+ builder.field(TO_FIELD.getPreferredName(), maybeConvertToString(this.to));
builder.field(INCLUDE_LOWER_FIELD.getPreferredName(), includeLower);
builder.field(INCLUDE_UPPER_FIELD.getPreferredName(), includeUpper);
if (timeZone != null) {
@@ -377,9 +377,9 @@ public static RangeQueryBuilder fromXContent(XContentParser parser) throws IOExc
currentFieldName = parser.currentName();
} else {
if (FROM_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
- from = parser.objectBytes();
+ from = maybeConvertToBytesRef(parser.objectBytes());
} else if (TO_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
- to = parser.objectBytes();
+ to = maybeConvertToBytesRef(parser.objectBytes());
} else if (INCLUDE_LOWER_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
includeLower = parser.booleanValue();
} else if (INCLUDE_UPPER_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
@@ -387,16 +387,16 @@ public static RangeQueryBuilder fromXContent(XContentParser parser) throws IOExc
} else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
boost = parser.floatValue();
} else if (GT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
- from = parser.objectBytes();
+ from = maybeConvertToBytesRef(parser.objectBytes());
includeLower = false;
} else if (GTE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
- from = parser.objectBytes();
+ from = maybeConvertToBytesRef(parser.objectBytes());
includeLower = true;
} else if (LT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
- to = parser.objectBytes();
+ to = maybeConvertToBytesRef(parser.objectBytes());
includeUpper = false;
} else if (LTE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
- to = parser.objectBytes();
+ to = maybeConvertToBytesRef(parser.objectBytes());
includeUpper = true;
} else if (TIME_ZONE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
timeZone = parser.text();
diff --git a/server/src/main/java/org/elasticsearch/index/query/SpanTermQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/SpanTermQueryBuilder.java
index f5b286451863f..0a02ff125d1cf 100644
--- a/server/src/main/java/org/elasticsearch/index/query/SpanTermQueryBuilder.java
+++ b/server/src/main/java/org/elasticsearch/index/query/SpanTermQueryBuilder.java
@@ -109,9 +109,9 @@ public static SpanTermQueryBuilder fromXContent(XContentParser parser) throws IO
currentFieldName = parser.currentName();
} else {
if (TERM_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
- value = parser.objectBytes();
+ value = maybeConvertToBytesRef(parser.objectBytes());
} else if (BaseTermQueryBuilder.VALUE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
- value = parser.objectBytes();
+ value = maybeConvertToBytesRef(parser.objectBytes());
} else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
boost = parser.floatValue();
} else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
@@ -125,7 +125,7 @@ public static SpanTermQueryBuilder fromXContent(XContentParser parser) throws IO
} else {
throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, parser.currentName());
fieldName = parser.currentName();
- value = parser.objectBytes();
+ value = maybeConvertToBytesRef(parser.objectBytes());
}
}
diff --git a/server/src/main/java/org/elasticsearch/index/query/TermQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/TermQueryBuilder.java
index 0df4973329d36..c35aa9b03d581 100644
--- a/server/src/main/java/org/elasticsearch/index/query/TermQueryBuilder.java
+++ b/server/src/main/java/org/elasticsearch/index/query/TermQueryBuilder.java
@@ -100,9 +100,9 @@ public static TermQueryBuilder fromXContent(XContentParser parser) throws IOExce
currentFieldName = parser.currentName();
} else {
if (TERM_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
- value = parser.objectBytes();
+ value = maybeConvertToBytesRef(parser.objectBytes());
} else if (VALUE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
- value = parser.objectBytes();
+ value = maybeConvertToBytesRef(parser.objectBytes());
} else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
queryName = parser.text();
} else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
@@ -116,7 +116,7 @@ public static TermQueryBuilder fromXContent(XContentParser parser) throws IOExce
} else if (token.isValue()) {
throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, parser.currentName());
fieldName = currentFieldName;
- value = parser.objectBytes();
+ value = maybeConvertToBytesRef(parser.objectBytes());
} else if (token == XContentParser.Token.START_ARRAY) {
throw new ParsingException(parser.getTokenLocation(), "[term] query does not support array of values");
}
diff --git a/server/src/main/java/org/elasticsearch/index/query/TermsQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/TermsQueryBuilder.java
index f235c785cb195..a144cbf088a01 100644
--- a/server/src/main/java/org/elasticsearch/index/query/TermsQueryBuilder.java
+++ b/server/src/main/java/org/elasticsearch/index/query/TermsQueryBuilder.java
@@ -44,6 +44,7 @@
import org.elasticsearch.indices.TermsLookup;
import java.io.IOException;
+import java.nio.CharBuffer;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
@@ -234,11 +235,12 @@ private static List> convert(Iterable> values) {
/**
* Convert the list in a way that optimizes storage in the case that all
- * elements are either integers or {@link String}s/{@link BytesRef}s. This
- * is useful to help garbage collections for use-cases that involve sending
- * very large terms queries to Elasticsearch. If the list does not only
- * contain integers or {@link String}s, then a list is returned where all
- * {@link String}s have been replaced with {@link BytesRef}s.
+ * elements are either integers or {@link String}s/{@link BytesRef}/
+ * {@link CharBuffer}s. This is useful to help garbage collections for
+ * use-cases that involve sending very large terms queries to Elasticsearch.
+ * If the list does not only contain integers or {@link String}s, then a
+ * list is returned where all {@link String}/{@link CharBuffer}s have been
+ * replaced with {@link BytesRef}s.
*/
static List> convert(List> list) {
if (list.isEmpty()) {
@@ -270,6 +272,8 @@ public int size() {
BytesRef b;
if (o instanceof BytesRef) {
b = (BytesRef) o;
+ } else if (o instanceof CharBuffer) {
+ b = new BytesRef((CharBuffer) o);
} else {
builder.copyChars(o.toString());
b = builder.get();
@@ -395,7 +399,7 @@ public static TermsQueryBuilder fromXContent(XContentParser parser) throws IOExc
static List