query builder builds a "safe" byte array
This commit is contained in:
parent
39e41ab819
commit
d149cbb06e
|
@ -121,11 +121,11 @@ public class ValidateQueryRequest extends BroadcastOperationRequest {
|
||||||
*/
|
*/
|
||||||
@Required
|
@Required
|
||||||
public ValidateQueryRequest query(QueryBuilder queryBuilder) {
|
public ValidateQueryRequest query(QueryBuilder queryBuilder) {
|
||||||
BytesStream bos = queryBuilder.buildAsUnsafeBytes();
|
BytesStream bos = queryBuilder.buildAsBytes();
|
||||||
this.querySource = bos.underlyingBytes();
|
this.querySource = bos.underlyingBytes();
|
||||||
this.querySourceOffset = 0;
|
this.querySourceOffset = 0;
|
||||||
this.querySourceLength = bos.size();
|
this.querySourceLength = bos.size();
|
||||||
this.querySourceUnsafe = true;
|
this.querySourceUnsafe = false;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -169,11 +169,11 @@ public class CountRequest extends BroadcastOperationRequest {
|
||||||
*/
|
*/
|
||||||
@Required
|
@Required
|
||||||
public CountRequest query(QueryBuilder queryBuilder) {
|
public CountRequest query(QueryBuilder queryBuilder) {
|
||||||
BytesStream bos = queryBuilder.buildAsUnsafeBytes();
|
BytesStream bos = queryBuilder.buildAsBytes();
|
||||||
this.querySource = bos.underlyingBytes();
|
this.querySource = bos.underlyingBytes();
|
||||||
this.querySourceOffset = 0;
|
this.querySourceOffset = 0;
|
||||||
this.querySourceLength = bos.size();
|
this.querySourceLength = bos.size();
|
||||||
this.querySourceUnsafe = true;
|
this.querySourceUnsafe = false;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -122,11 +122,11 @@ public class DeleteByQueryRequest extends IndicesReplicationOperationRequest {
|
||||||
*/
|
*/
|
||||||
@Required
|
@Required
|
||||||
public DeleteByQueryRequest query(QueryBuilder queryBuilder) {
|
public DeleteByQueryRequest query(QueryBuilder queryBuilder) {
|
||||||
BytesStream bos = queryBuilder.buildAsUnsafeBytes();
|
BytesStream bos = queryBuilder.buildAsBytes();
|
||||||
this.querySource = bos.underlyingBytes();
|
this.querySource = bos.underlyingBytes();
|
||||||
this.querySourceOffset = 0;
|
this.querySourceOffset = 0;
|
||||||
this.querySourceLength = bos.size();
|
this.querySourceLength = bos.size();
|
||||||
this.querySourceUnsafe = true;
|
this.querySourceUnsafe = false;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -44,32 +44,16 @@ public abstract class BaseQueryBuilder implements QueryBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BytesStream buildAsUnsafeBytes() throws QueryBuilderException {
|
public BytesStream buildAsBytes() 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 {
|
|
||||||
return buildAsBytes(XContentType.JSON);
|
return buildAsBytes(XContentType.JSON);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] buildAsBytes(XContentType contentType) throws QueryBuilderException {
|
public BytesStream buildAsBytes(XContentType contentType) throws QueryBuilderException {
|
||||||
try {
|
try {
|
||||||
XContentBuilder builder = XContentFactory.contentBuilder(contentType);
|
XContentBuilder builder = XContentFactory.contentBuilder(contentType);
|
||||||
toXContent(builder, EMPTY_PARAMS);
|
toXContent(builder, EMPTY_PARAMS);
|
||||||
return builder.copiedBytes();
|
return builder.underlyingStream();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new QueryBuilderException("Failed to build query", e);
|
throw new QueryBuilderException("Failed to build query", e);
|
||||||
}
|
}
|
||||||
|
|
|
@ -159,8 +159,8 @@ public class IndexQueryParserService extends AbstractIndexComponent {
|
||||||
public ParsedQuery parse(QueryBuilder queryBuilder) throws ElasticSearchException {
|
public ParsedQuery parse(QueryBuilder queryBuilder) throws ElasticSearchException {
|
||||||
XContentParser parser = null;
|
XContentParser parser = null;
|
||||||
try {
|
try {
|
||||||
BytesStream unsafeBytes = queryBuilder.buildAsUnsafeBytes();
|
BytesStream bytes = queryBuilder.buildAsBytes();
|
||||||
parser = XContentFactory.xContent(unsafeBytes.underlyingBytes(), 0, unsafeBytes.size()).createParser(unsafeBytes.underlyingBytes(), 0, unsafeBytes.size());
|
parser = XContentFactory.xContent(bytes.underlyingBytes(), 0, bytes.size()).createParser(bytes.underlyingBytes(), 0, bytes.size());
|
||||||
return parse(cache.get(), parser);
|
return parse(cache.get(), parser);
|
||||||
} catch (QueryParsingException e) {
|
} catch (QueryParsingException e) {
|
||||||
throw e;
|
throw e;
|
||||||
|
|
|
@ -28,11 +28,7 @@ import org.elasticsearch.common.xcontent.XContentType;
|
||||||
*/
|
*/
|
||||||
public interface QueryBuilder extends ToXContent {
|
public interface QueryBuilder extends ToXContent {
|
||||||
|
|
||||||
BytesStream buildAsUnsafeBytes() throws QueryBuilderException;
|
BytesStream buildAsBytes() throws QueryBuilderException;
|
||||||
|
|
||||||
BytesStream buildAsUnsafeBytes(XContentType contentType) throws QueryBuilderException;
|
BytesStream buildAsBytes(XContentType contentType) throws QueryBuilderException;
|
||||||
|
|
||||||
byte[] buildAsBytes() throws QueryBuilderException;
|
|
||||||
|
|
||||||
byte[] buildAsBytes(XContentType contentType) throws QueryBuilderException;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,11 +20,12 @@
|
||||||
package org.elasticsearch.rest.action.admin.indices.validate.query;
|
package org.elasticsearch.rest.action.admin.indices.validate.query;
|
||||||
|
|
||||||
import org.elasticsearch.action.ActionListener;
|
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.ValidateQueryRequest;
|
||||||
import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse;
|
import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse;
|
||||||
|
import org.elasticsearch.action.support.broadcast.BroadcastOperationThreading;
|
||||||
import org.elasticsearch.client.Client;
|
import org.elasticsearch.client.Client;
|
||||||
import org.elasticsearch.common.inject.Inject;
|
import org.elasticsearch.common.inject.Inject;
|
||||||
|
import org.elasticsearch.common.io.BytesStream;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
import org.elasticsearch.rest.*;
|
import org.elasticsearch.rest.*;
|
||||||
|
@ -75,9 +76,9 @@ public class RestValidateQueryAction extends BaseRestHandler {
|
||||||
if (source != null) {
|
if (source != null) {
|
||||||
validateQueryRequest.query(source);
|
validateQueryRequest.query(source);
|
||||||
} else {
|
} else {
|
||||||
byte[] querySource = RestActions.parseQuerySource(request);
|
BytesStream querySource = RestActions.parseQuerySource(request);
|
||||||
if (querySource != null) {
|
if (querySource != null) {
|
||||||
validateQueryRequest.query(querySource);
|
validateQueryRequest.query(querySource.underlyingBytes(), 0, querySource.size(), false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,7 @@ import org.elasticsearch.action.count.CountResponse;
|
||||||
import org.elasticsearch.action.support.broadcast.BroadcastOperationThreading;
|
import org.elasticsearch.action.support.broadcast.BroadcastOperationThreading;
|
||||||
import org.elasticsearch.client.Client;
|
import org.elasticsearch.client.Client;
|
||||||
import org.elasticsearch.common.inject.Inject;
|
import org.elasticsearch.common.inject.Inject;
|
||||||
|
import org.elasticsearch.common.io.BytesStream;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
import org.elasticsearch.rest.*;
|
import org.elasticsearch.rest.*;
|
||||||
|
@ -76,9 +77,9 @@ public class RestCountAction extends BaseRestHandler {
|
||||||
if (source != null) {
|
if (source != null) {
|
||||||
countRequest.query(source);
|
countRequest.query(source);
|
||||||
} else {
|
} else {
|
||||||
byte[] querySource = RestActions.parseQuerySource(request);
|
BytesStream querySource = RestActions.parseQuerySource(request);
|
||||||
if (querySource != null) {
|
if (querySource != null) {
|
||||||
countRequest.query(querySource);
|
countRequest.query(querySource.underlyingBytes(), 0, querySource.size(), false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,6 +28,7 @@ import org.elasticsearch.action.deletebyquery.ShardDeleteByQueryRequest;
|
||||||
import org.elasticsearch.action.support.replication.ReplicationType;
|
import org.elasticsearch.action.support.replication.ReplicationType;
|
||||||
import org.elasticsearch.client.Client;
|
import org.elasticsearch.client.Client;
|
||||||
import org.elasticsearch.common.inject.Inject;
|
import org.elasticsearch.common.inject.Inject;
|
||||||
|
import org.elasticsearch.common.io.BytesStream;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
import org.elasticsearch.rest.*;
|
import org.elasticsearch.rest.*;
|
||||||
|
@ -67,7 +68,8 @@ public class RestDeleteByQueryAction extends BaseRestHandler {
|
||||||
if (source != null) {
|
if (source != null) {
|
||||||
deleteByQueryRequest.query(source);
|
deleteByQueryRequest.query(source);
|
||||||
} else {
|
} 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")));
|
deleteByQueryRequest.types(splitTypes(request.param("type")));
|
||||||
|
|
|
@ -23,6 +23,7 @@ import org.elasticsearch.ElasticSearchIllegalArgumentException;
|
||||||
import org.elasticsearch.action.ShardOperationFailedException;
|
import org.elasticsearch.action.ShardOperationFailedException;
|
||||||
import org.elasticsearch.action.support.broadcast.BroadcastOperationResponse;
|
import org.elasticsearch.action.support.broadcast.BroadcastOperationResponse;
|
||||||
import org.elasticsearch.common.Strings;
|
import org.elasticsearch.common.Strings;
|
||||||
|
import org.elasticsearch.common.io.BytesStream;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
import org.elasticsearch.index.query.QueryBuilders;
|
import org.elasticsearch.index.query.QueryBuilders;
|
||||||
import org.elasticsearch.index.query.QueryStringQueryBuilder;
|
import org.elasticsearch.index.query.QueryStringQueryBuilder;
|
||||||
|
@ -69,7 +70,7 @@ public class RestActions {
|
||||||
builder.endObject();
|
builder.endObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] parseQuerySource(RestRequest request) {
|
public static BytesStream parseQuerySource(RestRequest request) {
|
||||||
String queryString = request.param("q");
|
String queryString = request.param("q");
|
||||||
if (queryString == null) {
|
if (queryString == null) {
|
||||||
return null;
|
return null;
|
||||||
|
|
|
@ -288,7 +288,7 @@ public class SimpleIndexQueryParserTests {
|
||||||
@Test
|
@Test
|
||||||
public void testTermQueryBuilder() throws IOException {
|
public void testTermQueryBuilder() throws IOException {
|
||||||
IndexQueryParserService queryParser = queryParser();
|
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));
|
assertThat(parsedQuery, instanceOf(NumericRangeQuery.class));
|
||||||
NumericRangeQuery fieldQuery = (NumericRangeQuery) parsedQuery;
|
NumericRangeQuery fieldQuery = (NumericRangeQuery) parsedQuery;
|
||||||
assertThat(fieldQuery.getMin().intValue(), equalTo(34));
|
assertThat(fieldQuery.getMin().intValue(), equalTo(34));
|
||||||
|
@ -313,7 +313,7 @@ public class SimpleIndexQueryParserTests {
|
||||||
@Test
|
@Test
|
||||||
public void testFuzzyQueryBuilder() throws IOException {
|
public void testFuzzyQueryBuilder() throws IOException {
|
||||||
IndexQueryParserService queryParser = queryParser();
|
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));
|
assertThat(parsedQuery, instanceOf(FuzzyQuery.class));
|
||||||
FuzzyQuery fuzzyQuery = (FuzzyQuery) parsedQuery;
|
FuzzyQuery fuzzyQuery = (FuzzyQuery) parsedQuery;
|
||||||
assertThat(fuzzyQuery.getTerm(), equalTo(new Term("name.first", "sh")));
|
assertThat(fuzzyQuery.getTerm(), equalTo(new Term("name.first", "sh")));
|
||||||
|
@ -332,7 +332,7 @@ public class SimpleIndexQueryParserTests {
|
||||||
@Test
|
@Test
|
||||||
public void testFuzzyQueryWithFieldsBuilder() throws IOException {
|
public void testFuzzyQueryWithFieldsBuilder() throws IOException {
|
||||||
IndexQueryParserService queryParser = queryParser();
|
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));
|
assertThat(parsedQuery, instanceOf(FuzzyQuery.class));
|
||||||
FuzzyQuery fuzzyQuery = (FuzzyQuery) parsedQuery;
|
FuzzyQuery fuzzyQuery = (FuzzyQuery) parsedQuery;
|
||||||
assertThat(fuzzyQuery.getTerm(), equalTo(new Term("name.first", "sh")));
|
assertThat(fuzzyQuery.getTerm(), equalTo(new Term("name.first", "sh")));
|
||||||
|
@ -368,7 +368,7 @@ public class SimpleIndexQueryParserTests {
|
||||||
@Test
|
@Test
|
||||||
public void testFieldQueryBuilder1() throws IOException {
|
public void testFieldQueryBuilder1() throws IOException {
|
||||||
IndexQueryParserService queryParser = queryParser();
|
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));
|
assertThat(parsedQuery, instanceOf(NumericRangeQuery.class));
|
||||||
NumericRangeQuery fieldQuery = (NumericRangeQuery) parsedQuery;
|
NumericRangeQuery fieldQuery = (NumericRangeQuery) parsedQuery;
|
||||||
assertThat(fieldQuery.getMin().intValue(), equalTo(34));
|
assertThat(fieldQuery.getMin().intValue(), equalTo(34));
|
||||||
|
|
Loading…
Reference in New Issue