From 0e56b75fcb748114f6c46cd228342ddfd41cb825 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Tue, 12 May 2015 15:48:58 +0200 Subject: [PATCH] Add to serialization of nested queries --- .../index/query/BaseQueryBuilder.java | 25 +++++++++++++++---- .../index/query/BoolQueryBuilder.java | 5 ++-- .../index/query/BaseQueryTestCase.java | 6 ++--- .../index/query/BoolQueryBuilderTest.java | 11 +++----- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/elasticsearch/index/query/BaseQueryBuilder.java b/src/main/java/org/elasticsearch/index/query/BaseQueryBuilder.java index 8266a27bbf3c3..8642079f1aa36 100644 --- a/src/main/java/org/elasticsearch/index/query/BaseQueryBuilder.java +++ b/src/main/java/org/elasticsearch/index/query/BaseQueryBuilder.java @@ -30,13 +30,22 @@ import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * Base class with common code for all {@link QueryBuilder} implementations. */ public abstract class BaseQueryBuilder implements QueryBuilder { + private final static Map> queryBuilderRegistry = new HashMap<>(); + static { + queryBuilderRegistry.put(MatchAllQueryParser.NAME, MatchAllQueryBuilder.class); + queryBuilderRegistry.put(TermQueryParser.NAME, TermQueryBuilder.class); + queryBuilderRegistry.put(IdsQueryParser.NAME, IdsQueryBuilder.class); + } + @Override public String toString() { try { @@ -77,6 +86,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws * Temporary default implementation for toQuery that parses the query using its query parser */ //norelease to be removed once all query builders override toQuery providing their own specific implementation. + @Override public Query toQuery(QueryParseContext parseContext) throws QueryParsingException, IOException { return parseContext.indexQueryParserService().queryParser(parserName()).parse(parseContext); } @@ -107,13 +117,18 @@ public void writeTo(StreamOutput out) throws IOException { } protected static QueryBuilder readQueryBuilderFrom(StreamInput in) throws IOException { + QueryBuilder newBuilder; String name = in.readString(); - QueryBuilder newBuilder = null; - switch (name) { - case MatchAllQueryParser.NAME: newBuilder = new MatchAllQueryBuilder(); break; - } - if (newBuilder != null) { + Class newBuilderClass = queryBuilderRegistry.get(name); + if (newBuilderClass != null) { + try { + newBuilder = (QueryBuilder) newBuilderClass.newInstance(); + } catch (InstantiationException | IllegalAccessException e) { + throw new IOException(e); + } newBuilder.readFrom(in); + } else { + throw new IOException("query with name '" + name + "' not registered, could not be deserialized."); } return newBuilder; } diff --git a/src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java b/src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java index 9b057209e0711..7629b2ebec01e 100644 --- a/src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java +++ b/src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java @@ -26,7 +26,6 @@ import org.apache.lucene.search.Query; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.io.stream.Streamable; import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -40,7 +39,7 @@ /** * A Query that matches documents matching boolean combinations of other queries. */ -public class BoolQueryBuilder extends BaseQueryBuilder implements Streamable, BoostableQueryBuilder { +public class BoolQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder { private List mustClauses = new ArrayList<>(); @@ -124,7 +123,7 @@ public BoolQueryBuilder should(QueryBuilder queryBuilder) { * @see #minimumNumberShouldMatch(int) */ public BoolQueryBuilder should(List queryBuilders) { - mustClauses.addAll(queryBuilders); + shouldClauses.addAll(queryBuilders); return this; } diff --git a/src/test/java/org/elasticsearch/index/query/BaseQueryTestCase.java b/src/test/java/org/elasticsearch/index/query/BaseQueryTestCase.java index 6740584d1862d..944e6677819a2 100644 --- a/src/test/java/org/elasticsearch/index/query/BaseQueryTestCase.java +++ b/src/test/java/org/elasticsearch/index/query/BaseQueryTestCase.java @@ -20,6 +20,7 @@ package org.elasticsearch.index.query; import com.carrotsearch.randomizedtesting.annotations.Repeat; + import org.apache.lucene.search.Query; import org.elasticsearch.Version; import org.elasticsearch.cluster.ClusterService; @@ -30,7 +31,6 @@ import org.elasticsearch.common.inject.util.Providers; import org.elasticsearch.common.io.stream.BytesStreamInput; import org.elasticsearch.common.io.stream.BytesStreamOutput; -import org.elasticsearch.common.io.stream.Streamable; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsModule; @@ -59,10 +59,10 @@ import java.io.IOException; -import static org.hamcrest.Matchers.*; +import static org.hamcrest.Matchers.is; @Ignore -public abstract class BaseQueryTestCase extends ElasticsearchTestCase { +public abstract class BaseQueryTestCase extends ElasticsearchTestCase { private static Injector injector; private static IndexQueryParserService queryParserService; diff --git a/src/test/java/org/elasticsearch/index/query/BoolQueryBuilderTest.java b/src/test/java/org/elasticsearch/index/query/BoolQueryBuilderTest.java index f7bfc0b576fe4..94821ea46fe25 100644 --- a/src/test/java/org/elasticsearch/index/query/BoolQueryBuilderTest.java +++ b/src/test/java/org/elasticsearch/index/query/BoolQueryBuilderTest.java @@ -19,8 +19,6 @@ package org.elasticsearch.index.query; -import com.carrotsearch.randomizedtesting.annotations.Repeat; - import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.Query; @@ -31,7 +29,6 @@ import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; -@Repeat(iterations=20) public class BoolQueryBuilderTest extends BaseQueryTestCase { @Override @@ -92,10 +89,10 @@ protected BoolQueryBuilder createTestQueryBuilder() { private static QueryBuilder createRandomQuery() { QueryBuilder query = null; - switch (randomIntBetween(0, 0)) { - case 0: query = new MatchAllQueryBuilderTest().createTestQueryBuilder(); break; -// case 1: return TermQueryBuilderTest.createTestQuery(); -// case 2: return IdsQueryBuilderTest.createTestQuery(); + switch (randomIntBetween(0, 2)) { + case 0: return new MatchAllQueryBuilderTest().createTestQueryBuilder(); + case 1: return new TermQueryBuilderTest().createTestQueryBuilder(); + case 2: return new IdsQueryBuilderTest().createTestQueryBuilder(); } return query; }