diff --git a/core/src/main/java/org/elasticsearch/action/search/SearchRequest.java b/core/src/main/java/org/elasticsearch/action/search/SearchRequest.java index 1fabe313536..60f565bdf16 100644 --- a/core/src/main/java/org/elasticsearch/action/search/SearchRequest.java +++ b/core/src/main/java/org/elasticsearch/action/search/SearchRequest.java @@ -332,7 +332,7 @@ public class SearchRequest extends ActionRequest implements Indic scroll = readScroll(in); } if (in.readBoolean()) { - source = SearchSourceBuilder.PROTOTYPE.readFrom(in); + source = SearchSourceBuilder.readSearchSourceFrom(in); } types = in.readStringArray(); diff --git a/core/src/main/java/org/elasticsearch/action/search/SearchRequestBuilder.java b/core/src/main/java/org/elasticsearch/action/search/SearchRequestBuilder.java index bcb86256a4d..2e3084c0ee7 100644 --- a/core/src/main/java/org/elasticsearch/action/search/SearchRequestBuilder.java +++ b/core/src/main/java/org/elasticsearch/action/search/SearchRequestBuilder.java @@ -45,8 +45,6 @@ import java.util.List; */ public class SearchRequestBuilder extends ActionRequestBuilder { - private SearchSourceBuilder sourceBuilder; - public SearchRequestBuilder(ElasticsearchClient client, SearchAction action) { super(client, action, new SearchRequest()); } @@ -475,54 +473,18 @@ public class SearchRequestBuilder extends ActionRequestBuilder builder = context.parseInnerQueryBuilder(); countRequest.query(builder); } catch (IOException e) { throw new ElasticsearchException("failed to parse source", e); + } finally { + context.reset(null); } } else { QueryBuilder queryBuilder = RestActions.urlParamsToQueryBuilder(request); diff --git a/core/src/main/java/org/elasticsearch/rest/action/search/RestMultiSearchAction.java b/core/src/main/java/org/elasticsearch/rest/action/search/RestMultiSearchAction.java index 31bf0ab3022..2921e914f2b 100644 --- a/core/src/main/java/org/elasticsearch/rest/action/search/RestMultiSearchAction.java +++ b/core/src/main/java/org/elasticsearch/rest/action/search/RestMultiSearchAction.java @@ -34,7 +34,10 @@ import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.query.TemplateQueryParser; import org.elasticsearch.indices.query.IndicesQueriesRegistry; -import org.elasticsearch.rest.*; +import org.elasticsearch.rest.BaseRestHandler; +import org.elasticsearch.rest.RestChannel; +import org.elasticsearch.rest.RestController; +import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.support.RestActions; import org.elasticsearch.rest.action.support.RestToXContentListener; import org.elasticsearch.script.Template; @@ -181,7 +184,7 @@ public class RestMultiSearchAction extends BaseRestHandler { } else { try (XContentParser requestParser = XContentFactory.xContent(slice).createParser(slice)) { queryParseContext.reset(requestParser); - searchRequest.source(SearchSourceBuilder.PROTOTYPE.fromXContent(requestParser, queryParseContext)); + searchRequest.source(SearchSourceBuilder.parseSearchSource(requestParser, queryParseContext)); } } // move pointers diff --git a/core/src/main/java/org/elasticsearch/rest/action/support/RestActions.java b/core/src/main/java/org/elasticsearch/rest/action/support/RestActions.java index 4dcccb01717..62467d5d880 100644 --- a/core/src/main/java/org/elasticsearch/rest/action/support/RestActions.java +++ b/core/src/main/java/org/elasticsearch/rest/action/support/RestActions.java @@ -116,7 +116,7 @@ public class RestActions { XContentParser parser = XContentFactory.xContent(sourceBytes).createParser(sourceBytes); QueryParseContext queryParseContext = new QueryParseContext(queryRegistry); queryParseContext.reset(parser); - SearchSourceBuilder source = SearchSourceBuilder.PROTOTYPE.fromXContent(parser, queryParseContext); + SearchSourceBuilder source = SearchSourceBuilder.parseSearchSource(parser, queryParseContext); return source; } diff --git a/core/src/main/java/org/elasticsearch/search/SearchService.java b/core/src/main/java/org/elasticsearch/search/SearchService.java index b1bb5e432b3..d00e8ddce94 100644 --- a/core/src/main/java/org/elasticsearch/search/SearchService.java +++ b/core/src/main/java/org/elasticsearch/search/SearchService.java @@ -578,7 +578,7 @@ public class SearchService extends AbstractLifecycleComponent { try (XContentParser parser = XContentFactory.xContent(run).createParser(run)) { QueryParseContext queryParseContext = new QueryParseContext(indexService.queryParserService().indicesQueriesRegistry()); queryParseContext.reset(parser); - parseSource(context, SearchSourceBuilder.PROTOTYPE.fromXContent(parser, queryParseContext)); + parseSource(context, SearchSourceBuilder.parseSearchSource(parser, queryParseContext)); } } parseSource(context, request.source()); diff --git a/core/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java b/core/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java index 400ce985997..47d8a7f4f49 100644 --- a/core/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java @@ -60,12 +60,9 @@ import java.util.Objects; /** * A search source builder allowing to easily build search source. Simple * construction using - * {@link org.elasticsearch.search.builder.NewSearchSourceBuilder#searchSource()}. - * - * @see org.elasticsearch.action.search.SearchRequest#source(NewSearchSourceBuilder) - */ -/** + * {@link org.elasticsearch.search.builder.SearchSourceBuilder#searchSource()}. * + * @see org.elasticsearch.action.search.SearchRequest#source(SearchSourceBuilder) */ public final class SearchSourceBuilder extends ToXContentToBytes implements Writeable { @@ -95,7 +92,15 @@ public final class SearchSourceBuilder extends ToXContentToBytes implements Writ public static final ParseField STATS_FIELD = new ParseField("stats"); public static final ParseField EXT_FIELD = new ParseField("ext"); - public static final SearchSourceBuilder PROTOTYPE = new SearchSourceBuilder(); + private static final SearchSourceBuilder PROTOTYPE = new SearchSourceBuilder(); + + public static SearchSourceBuilder readSearchSourceFrom(StreamInput in) throws IOException { + return PROTOTYPE.readFrom(in); + } + + public static SearchSourceBuilder parseSearchSource(XContentParser parser, QueryParseContext context) throws IOException { + return PROTOTYPE.fromXContent(parser, context); + } /** * A static factory method to construct a new search source. diff --git a/core/src/main/java/org/elasticsearch/search/internal/ShardSearchLocalRequest.java b/core/src/main/java/org/elasticsearch/search/internal/ShardSearchLocalRequest.java index 677f392a6e0..8f0cd98c98a 100644 --- a/core/src/main/java/org/elasticsearch/search/internal/ShardSearchLocalRequest.java +++ b/core/src/main/java/org/elasticsearch/search/internal/ShardSearchLocalRequest.java @@ -175,7 +175,7 @@ public class ShardSearchLocalRequest extends ContextAndHeaderHolder implements S scroll = readScroll(in); } if (in.readBoolean()) { - source = SearchSourceBuilder.PROTOTYPE.readFrom(in); + source = SearchSourceBuilder.readSearchSourceFrom(in); } types = in.readStringArray(); filteringAliases = in.readStringArray(); diff --git a/core/src/main/java/org/elasticsearch/search/warmer/IndexWarmersMetaData.java b/core/src/main/java/org/elasticsearch/search/warmer/IndexWarmersMetaData.java index 76b2e822c1b..1ce27f97cff 100644 --- a/core/src/main/java/org/elasticsearch/search/warmer/IndexWarmersMetaData.java +++ b/core/src/main/java/org/elasticsearch/search/warmer/IndexWarmersMetaData.java @@ -30,8 +30,12 @@ import org.elasticsearch.common.bytes.BytesReference; 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.util.ByteArray; -import org.elasticsearch.common.xcontent.*; +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentGenerator; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.search.builder.SearchSourceBuilder; @@ -279,6 +283,9 @@ public class IndexWarmersMetaData extends AbstractDiffable private SearchSourceBuilder cached; public SearchSource(BytesReference bytesArray) { + if (bytesArray == null) { + throw new IllegalArgumentException("bytesArray must not be null"); + } this.binary = bytesArray; } @@ -299,7 +306,7 @@ public class IndexWarmersMetaData extends AbstractDiffable if (cached == null) { try (XContentParser parser = XContentFactory.xContent(binary).createParser(binary)) { ctx.reset(parser); - cached = SearchSourceBuilder.PROTOTYPE.fromXContent(parser, ctx); + cached = SearchSourceBuilder.parseSearchSource(parser, ctx); } } return cached; diff --git a/core/src/test/java/org/elasticsearch/index/query/plugin/DummyQueryParserPlugin.java b/core/src/test/java/org/elasticsearch/index/query/plugin/DummyQueryParserPlugin.java index 6038de42309..c72470c0cce 100644 --- a/core/src/test/java/org/elasticsearch/index/query/plugin/DummyQueryParserPlugin.java +++ b/core/src/test/java/org/elasticsearch/index/query/plugin/DummyQueryParserPlugin.java @@ -65,11 +65,6 @@ public class DummyQueryParserPlugin extends Plugin { return new DummyQuery(context.isFilter()); } - @Override - public String getWriteableName() { - return NAME; - } - @Override protected DummyQueryBuilder doReadFrom(StreamInput in) throws IOException { return new DummyQueryBuilder(); @@ -89,6 +84,11 @@ public class DummyQueryParserPlugin extends Plugin { protected boolean doEquals(DummyQueryBuilder other) { return true; } + + @Override + public String getWriteableName() { + return NAME; + } } public static class DummyQueryParser implements QueryParser { diff --git a/core/src/test/java/org/elasticsearch/indices/IndicesOptionsIntegrationIT.java b/core/src/test/java/org/elasticsearch/indices/IndicesOptionsIntegrationIT.java index d7d3673c017..348d5bc4eb5 100644 --- a/core/src/test/java/org/elasticsearch/indices/IndicesOptionsIntegrationIT.java +++ b/core/src/test/java/org/elasticsearch/indices/IndicesOptionsIntegrationIT.java @@ -677,6 +677,7 @@ public class IndicesOptionsIntegrationIT extends ESIntegTestCase { @Test public void testDeleteWarmer() throws Exception { SearchSourceBuilder source = new SearchSourceBuilder(); + source.query(QueryBuilders.matchAllQuery()); IndexWarmersMetaData.Entry entry = new IndexWarmersMetaData.Entry("test1", new String[] { "typ1" }, false, new IndexWarmersMetaData.SearchSource(source)); assertAcked(prepareCreate("foobar").addCustom(new IndexWarmersMetaData(entry))); ensureYellow(); @@ -692,6 +693,7 @@ public class IndicesOptionsIntegrationIT extends ESIntegTestCase { verify(client().admin().indices().prepareDeleteWarmer().setIndices("_all").setNames("test1"), true); SearchSourceBuilder source = new SearchSourceBuilder(); + source.query(QueryBuilders.matchAllQuery()); IndexWarmersMetaData.Entry entry = new IndexWarmersMetaData.Entry("test1", new String[] { "type1" }, false, new IndexWarmersMetaData.SearchSource(source)); assertAcked(prepareCreate("foo").addCustom(new IndexWarmersMetaData(entry))); assertAcked(prepareCreate("foobar").addCustom(new IndexWarmersMetaData(entry))); diff --git a/core/src/test/java/org/elasticsearch/search/builder/SearchSourceBuilderTests.java b/core/src/test/java/org/elasticsearch/search/builder/SearchSourceBuilderTests.java index a5e2c5272ac..89a9af44881 100644 --- a/core/src/test/java/org/elasticsearch/search/builder/SearchSourceBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/builder/SearchSourceBuilderTests.java @@ -302,7 +302,7 @@ public class SearchSourceBuilderTests extends ESTestCase { private void assertParseSearchSource(SearchSourceBuilder testBuilder, String builderAsString) throws IOException { XContentParser parser = XContentFactory.xContent(builderAsString).createParser(builderAsString); - SearchSourceBuilder newBuilder = SearchSourceBuilder.PROTOTYPE.fromXContent(parser, createParseContext(parser)); + SearchSourceBuilder newBuilder = SearchSourceBuilder.parseSearchSource(parser, createParseContext(parser)); assertNotSame(testBuilder, newBuilder); assertEquals(testBuilder, newBuilder); assertEquals(testBuilder.hashCode(), newBuilder.hashCode()); @@ -321,7 +321,7 @@ public class SearchSourceBuilderTests extends ESTestCase { try (BytesStreamOutput output = new BytesStreamOutput()) { testBuilder.writeTo(output); try (StreamInput in = new NamedWriteableAwareStreamInput(StreamInput.wrap(output.bytes()), namedWriteableRegistry)) { - SearchSourceBuilder deserializedBuilder = SearchSourceBuilder.PROTOTYPE.readFrom(in); + SearchSourceBuilder deserializedBuilder = SearchSourceBuilder.readSearchSourceFrom(in); assertEquals(deserializedBuilder, testBuilder); assertEquals(deserializedBuilder.hashCode(), testBuilder.hashCode()); assertNotSame(deserializedBuilder, testBuilder); @@ -359,7 +359,7 @@ public class SearchSourceBuilderTests extends ESTestCase { try (BytesStreamOutput output = new BytesStreamOutput()) { builder.writeTo(output); try (StreamInput in = new NamedWriteableAwareStreamInput(StreamInput.wrap(output.bytes()), namedWriteableRegistry)) { - return SearchSourceBuilder.PROTOTYPE.readFrom(in); + return SearchSourceBuilder.readSearchSourceFrom(in); } } } @@ -368,7 +368,7 @@ public class SearchSourceBuilderTests extends ESTestCase { { String restContent = " { \"_source\": { \"includes\": \"include\", \"excludes\": \"*.field2\"}}"; try (XContentParser parser = XContentFactory.xContent(restContent).createParser(restContent)) { - SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.PROTOTYPE.fromXContent(parser, createParseContext(parser)); + SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.parseSearchSource(parser, createParseContext(parser)); assertArrayEquals(new String[]{"*.field2" }, searchSourceBuilder.fetchSource().excludes()); assertArrayEquals(new String[]{"include" }, searchSourceBuilder.fetchSource().includes()); } @@ -376,7 +376,7 @@ public class SearchSourceBuilderTests extends ESTestCase { { String restContent = " { \"_source\": false}"; try (XContentParser parser = XContentFactory.xContent(restContent).createParser(restContent)) { - SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.PROTOTYPE.fromXContent(parser, createParseContext(parser)); + SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.parseSearchSource(parser, createParseContext(parser)); assertArrayEquals(new String[]{}, searchSourceBuilder.fetchSource().excludes()); assertArrayEquals(new String[]{}, searchSourceBuilder.fetchSource().includes()); assertFalse(searchSourceBuilder.fetchSource().fetchSource()); @@ -389,7 +389,7 @@ public class SearchSourceBuilderTests extends ESTestCase { { String restContent = " { \"sort\": \"foo\"}"; try (XContentParser parser = XContentFactory.xContent(restContent).createParser(restContent)) { - SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.PROTOTYPE.fromXContent(parser, createParseContext(parser)); + SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.parseSearchSource(parser, createParseContext(parser)); assertEquals(1, searchSourceBuilder.sorts().size()); assertEquals("{\"foo\":{}}", searchSourceBuilder.sorts().get(0).toUtf8()); } @@ -404,7 +404,7 @@ public class SearchSourceBuilderTests extends ESTestCase { " \"_score\"\n" + " ]}"; try (XContentParser parser = XContentFactory.xContent(restContent).createParser(restContent)) { - SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.PROTOTYPE.fromXContent(parser, createParseContext(parser)); + SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.parseSearchSource(parser, createParseContext(parser)); assertEquals(5, searchSourceBuilder.sorts().size()); assertEquals("{\"post_date\":{\"order\":\"asc\"}}", searchSourceBuilder.sorts().get(0).toUtf8()); assertEquals("\"user\"", searchSourceBuilder.sorts().get(1).toUtf8());