diff --git a/src/main/java/org/elasticsearch/action/admin/indices/validate/query/ValidateQueryRequest.java b/src/main/java/org/elasticsearch/action/admin/indices/validate/query/ValidateQueryRequest.java index 18030e6d175..bf38df36092 100644 --- a/src/main/java/org/elasticsearch/action/admin/indices/validate/query/ValidateQueryRequest.java +++ b/src/main/java/org/elasticsearch/action/admin/indices/validate/query/ValidateQueryRequest.java @@ -121,11 +121,11 @@ public class ValidateQueryRequest extends BroadcastOperationRequest { */ @Required public ValidateQueryRequest query(QueryBuilder queryBuilder) { - BytesStream bos = queryBuilder.buildAsUnsafeBytes(); + BytesStream bos = queryBuilder.buildAsBytes(); this.querySource = bos.underlyingBytes(); this.querySourceOffset = 0; this.querySourceLength = bos.size(); - this.querySourceUnsafe = true; + this.querySourceUnsafe = false; return this; } diff --git a/src/main/java/org/elasticsearch/action/count/CountRequest.java b/src/main/java/org/elasticsearch/action/count/CountRequest.java index bd604fb5ac3..47d67fd6cce 100644 --- a/src/main/java/org/elasticsearch/action/count/CountRequest.java +++ b/src/main/java/org/elasticsearch/action/count/CountRequest.java @@ -169,11 +169,11 @@ public class CountRequest extends BroadcastOperationRequest { */ @Required public CountRequest query(QueryBuilder queryBuilder) { - BytesStream bos = queryBuilder.buildAsUnsafeBytes(); + BytesStream bos = queryBuilder.buildAsBytes(); this.querySource = bos.underlyingBytes(); this.querySourceOffset = 0; this.querySourceLength = bos.size(); - this.querySourceUnsafe = true; + this.querySourceUnsafe = false; return this; } diff --git a/src/main/java/org/elasticsearch/action/deletebyquery/DeleteByQueryRequest.java b/src/main/java/org/elasticsearch/action/deletebyquery/DeleteByQueryRequest.java index 8b135edb086..25f4d917214 100644 --- a/src/main/java/org/elasticsearch/action/deletebyquery/DeleteByQueryRequest.java +++ b/src/main/java/org/elasticsearch/action/deletebyquery/DeleteByQueryRequest.java @@ -122,11 +122,11 @@ public class DeleteByQueryRequest extends IndicesReplicationOperationRequest { */ @Required public DeleteByQueryRequest query(QueryBuilder queryBuilder) { - BytesStream bos = queryBuilder.buildAsUnsafeBytes(); + BytesStream bos = queryBuilder.buildAsBytes(); this.querySource = bos.underlyingBytes(); this.querySourceOffset = 0; this.querySourceLength = bos.size(); - this.querySourceUnsafe = true; + this.querySourceUnsafe = false; return this; } diff --git a/src/main/java/org/elasticsearch/index/query/BaseQueryBuilder.java b/src/main/java/org/elasticsearch/index/query/BaseQueryBuilder.java index e5bd7512624..b1c54511871 100644 --- a/src/main/java/org/elasticsearch/index/query/BaseQueryBuilder.java +++ b/src/main/java/org/elasticsearch/index/query/BaseQueryBuilder.java @@ -44,32 +44,16 @@ public abstract class BaseQueryBuilder implements QueryBuilder { } @Override - public BytesStream buildAsUnsafeBytes() throws QueryBuilderException { - return buildAsUnsafeBytes(XContentType.JSON); - } - - @Override - public BytesStream buildAsUnsafeBytes(XContentType contentType) throws QueryBuilderException { - try { - XContentBuilder builder = XContentFactory.contentBuilder(contentType); - toXContent(builder, EMPTY_PARAMS); - return builder.underlyingStream(); - } catch (Exception e) { - throw new QueryBuilderException("Failed to build query", e); - } - } - - @Override - public byte[] buildAsBytes() throws QueryBuilderException { + public BytesStream buildAsBytes() throws QueryBuilderException { return buildAsBytes(XContentType.JSON); } @Override - public byte[] buildAsBytes(XContentType contentType) throws QueryBuilderException { + public BytesStream buildAsBytes(XContentType contentType) throws QueryBuilderException { try { XContentBuilder builder = XContentFactory.contentBuilder(contentType); toXContent(builder, EMPTY_PARAMS); - return builder.copiedBytes(); + return builder.underlyingStream(); } catch (Exception e) { throw new QueryBuilderException("Failed to build query", e); } diff --git a/src/main/java/org/elasticsearch/index/query/IndexQueryParserService.java b/src/main/java/org/elasticsearch/index/query/IndexQueryParserService.java index 1729808ca7e..142a2d805fd 100644 --- a/src/main/java/org/elasticsearch/index/query/IndexQueryParserService.java +++ b/src/main/java/org/elasticsearch/index/query/IndexQueryParserService.java @@ -159,8 +159,8 @@ public class IndexQueryParserService extends AbstractIndexComponent { public ParsedQuery parse(QueryBuilder queryBuilder) throws ElasticSearchException { XContentParser parser = null; try { - BytesStream unsafeBytes = queryBuilder.buildAsUnsafeBytes(); - parser = XContentFactory.xContent(unsafeBytes.underlyingBytes(), 0, unsafeBytes.size()).createParser(unsafeBytes.underlyingBytes(), 0, unsafeBytes.size()); + BytesStream bytes = queryBuilder.buildAsBytes(); + parser = XContentFactory.xContent(bytes.underlyingBytes(), 0, bytes.size()).createParser(bytes.underlyingBytes(), 0, bytes.size()); return parse(cache.get(), parser); } catch (QueryParsingException e) { throw e; diff --git a/src/main/java/org/elasticsearch/index/query/QueryBuilder.java b/src/main/java/org/elasticsearch/index/query/QueryBuilder.java index c4101345b30..0fd67ba01a5 100644 --- a/src/main/java/org/elasticsearch/index/query/QueryBuilder.java +++ b/src/main/java/org/elasticsearch/index/query/QueryBuilder.java @@ -28,11 +28,7 @@ import org.elasticsearch.common.xcontent.XContentType; */ public interface QueryBuilder extends ToXContent { - BytesStream buildAsUnsafeBytes() throws QueryBuilderException; + BytesStream buildAsBytes() throws QueryBuilderException; - BytesStream buildAsUnsafeBytes(XContentType contentType) throws QueryBuilderException; - - byte[] buildAsBytes() throws QueryBuilderException; - - byte[] buildAsBytes(XContentType contentType) throws QueryBuilderException; + BytesStream buildAsBytes(XContentType contentType) throws QueryBuilderException; } diff --git a/src/main/java/org/elasticsearch/rest/action/admin/indices/validate/query/RestValidateQueryAction.java b/src/main/java/org/elasticsearch/rest/action/admin/indices/validate/query/RestValidateQueryAction.java index 9da168c2c4c..f41225c2b5a 100644 --- a/src/main/java/org/elasticsearch/rest/action/admin/indices/validate/query/RestValidateQueryAction.java +++ b/src/main/java/org/elasticsearch/rest/action/admin/indices/validate/query/RestValidateQueryAction.java @@ -20,11 +20,12 @@ package org.elasticsearch.rest.action.admin.indices.validate.query; import org.elasticsearch.action.ActionListener; -import org.elasticsearch.action.support.broadcast.BroadcastOperationThreading; import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest; import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse; +import org.elasticsearch.action.support.broadcast.BroadcastOperationThreading; import org.elasticsearch.client.Client; import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.common.io.BytesStream; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.rest.*; @@ -75,9 +76,9 @@ public class RestValidateQueryAction extends BaseRestHandler { if (source != null) { validateQueryRequest.query(source); } else { - byte[] querySource = RestActions.parseQuerySource(request); + BytesStream querySource = RestActions.parseQuerySource(request); if (querySource != null) { - validateQueryRequest.query(querySource); + validateQueryRequest.query(querySource.underlyingBytes(), 0, querySource.size(), false); } } } diff --git a/src/main/java/org/elasticsearch/rest/action/count/RestCountAction.java b/src/main/java/org/elasticsearch/rest/action/count/RestCountAction.java index d755fcc250c..4cc3a128691 100644 --- a/src/main/java/org/elasticsearch/rest/action/count/RestCountAction.java +++ b/src/main/java/org/elasticsearch/rest/action/count/RestCountAction.java @@ -25,6 +25,7 @@ import org.elasticsearch.action.count.CountResponse; import org.elasticsearch.action.support.broadcast.BroadcastOperationThreading; import org.elasticsearch.client.Client; import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.common.io.BytesStream; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.rest.*; @@ -76,9 +77,9 @@ public class RestCountAction extends BaseRestHandler { if (source != null) { countRequest.query(source); } else { - byte[] querySource = RestActions.parseQuerySource(request); + BytesStream querySource = RestActions.parseQuerySource(request); if (querySource != null) { - countRequest.query(querySource); + countRequest.query(querySource.underlyingBytes(), 0, querySource.size(), false); } } } diff --git a/src/main/java/org/elasticsearch/rest/action/deletebyquery/RestDeleteByQueryAction.java b/src/main/java/org/elasticsearch/rest/action/deletebyquery/RestDeleteByQueryAction.java index 81588388110..0e5107e25a6 100644 --- a/src/main/java/org/elasticsearch/rest/action/deletebyquery/RestDeleteByQueryAction.java +++ b/src/main/java/org/elasticsearch/rest/action/deletebyquery/RestDeleteByQueryAction.java @@ -28,6 +28,7 @@ import org.elasticsearch.action.deletebyquery.ShardDeleteByQueryRequest; import org.elasticsearch.action.support.replication.ReplicationType; import org.elasticsearch.client.Client; import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.common.io.BytesStream; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.rest.*; @@ -67,7 +68,8 @@ public class RestDeleteByQueryAction extends BaseRestHandler { if (source != null) { deleteByQueryRequest.query(source); } else { - deleteByQueryRequest.query(RestActions.parseQuerySource(request)); + BytesStream bytes = RestActions.parseQuerySource(request); + deleteByQueryRequest.query(bytes.underlyingBytes(), 0, bytes.size(), false); } } deleteByQueryRequest.types(splitTypes(request.param("type"))); diff --git a/src/main/java/org/elasticsearch/rest/action/support/RestActions.java b/src/main/java/org/elasticsearch/rest/action/support/RestActions.java index ae4bd7b8db9..b74b9d15c84 100644 --- a/src/main/java/org/elasticsearch/rest/action/support/RestActions.java +++ b/src/main/java/org/elasticsearch/rest/action/support/RestActions.java @@ -23,6 +23,7 @@ import org.elasticsearch.ElasticSearchIllegalArgumentException; import org.elasticsearch.action.ShardOperationFailedException; import org.elasticsearch.action.support.broadcast.BroadcastOperationResponse; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.io.BytesStream; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.QueryStringQueryBuilder; @@ -69,7 +70,7 @@ public class RestActions { builder.endObject(); } - public static byte[] parseQuerySource(RestRequest request) { + public static BytesStream parseQuerySource(RestRequest request) { String queryString = request.param("q"); if (queryString == null) { return null; diff --git a/src/test/java/org/elasticsearch/test/unit/index/query/SimpleIndexQueryParserTests.java b/src/test/java/org/elasticsearch/test/unit/index/query/SimpleIndexQueryParserTests.java index ec90f20f5c0..0137c06a735 100644 --- a/src/test/java/org/elasticsearch/test/unit/index/query/SimpleIndexQueryParserTests.java +++ b/src/test/java/org/elasticsearch/test/unit/index/query/SimpleIndexQueryParserTests.java @@ -288,7 +288,7 @@ public class SimpleIndexQueryParserTests { @Test public void testTermQueryBuilder() throws IOException { IndexQueryParserService queryParser = queryParser(); - Query parsedQuery = queryParser.parse(termQuery("age", 34).buildAsBytes()).query(); + Query parsedQuery = queryParser.parse(termQuery("age", 34).buildAsBytes().copiedByteArray()).query(); assertThat(parsedQuery, instanceOf(NumericRangeQuery.class)); NumericRangeQuery fieldQuery = (NumericRangeQuery) parsedQuery; assertThat(fieldQuery.getMin().intValue(), equalTo(34)); @@ -313,7 +313,7 @@ public class SimpleIndexQueryParserTests { @Test public void testFuzzyQueryBuilder() throws IOException { IndexQueryParserService queryParser = queryParser(); - Query parsedQuery = queryParser.parse(fuzzyQuery("name.first", "sh").buildAsBytes()).query(); + Query parsedQuery = queryParser.parse(fuzzyQuery("name.first", "sh").buildAsBytes().copiedByteArray()).query(); assertThat(parsedQuery, instanceOf(FuzzyQuery.class)); FuzzyQuery fuzzyQuery = (FuzzyQuery) parsedQuery; assertThat(fuzzyQuery.getTerm(), equalTo(new Term("name.first", "sh"))); @@ -332,7 +332,7 @@ public class SimpleIndexQueryParserTests { @Test public void testFuzzyQueryWithFieldsBuilder() throws IOException { IndexQueryParserService queryParser = queryParser(); - Query parsedQuery = queryParser.parse(fuzzyQuery("name.first", "sh").minSimilarity(0.1f).prefixLength(1).boost(2.0f).buildAsBytes()).query(); + Query parsedQuery = queryParser.parse(fuzzyQuery("name.first", "sh").minSimilarity(0.1f).prefixLength(1).boost(2.0f).buildAsBytes().copiedByteArray()).query(); assertThat(parsedQuery, instanceOf(FuzzyQuery.class)); FuzzyQuery fuzzyQuery = (FuzzyQuery) parsedQuery; assertThat(fuzzyQuery.getTerm(), equalTo(new Term("name.first", "sh"))); @@ -368,7 +368,7 @@ public class SimpleIndexQueryParserTests { @Test public void testFieldQueryBuilder1() throws IOException { IndexQueryParserService queryParser = queryParser(); - Query parsedQuery = queryParser.parse(fieldQuery("age", 34).buildAsBytes()).query(); + Query parsedQuery = queryParser.parse(fieldQuery("age", 34).buildAsBytes().copiedByteArray()).query(); assertThat(parsedQuery, instanceOf(NumericRangeQuery.class)); NumericRangeQuery fieldQuery = (NumericRangeQuery) parsedQuery; assertThat(fieldQuery.getMin().intValue(), equalTo(34));