diff --git a/core/src/main/java/org/elasticsearch/percolator/PercolateContext.java b/core/src/main/java/org/elasticsearch/percolator/PercolateContext.java index 1a18f021597..c1b357e9b28 100644 --- a/core/src/main/java/org/elasticsearch/percolator/PercolateContext.java +++ b/core/src/main/java/org/elasticsearch/percolator/PercolateContext.java @@ -294,11 +294,6 @@ public class PercolateContext extends SearchContext { return subPhaseContexts.get(subPhaseName); } - @Override - public boolean hasFetchSubPhaseContext(FetchSubPhase.ContextFactory contextFactory) { - return subPhaseContexts.get(contextFactory.getName()) != null; - } - // Unused: @Override public void preProcess() { diff --git a/core/src/main/java/org/elasticsearch/search/fetch/FetchSubPhaseContext.java b/core/src/main/java/org/elasticsearch/search/fetch/FetchSubPhaseContext.java index 907d20c52ee..17125b42bcf 100644 --- a/core/src/main/java/org/elasticsearch/search/fetch/FetchSubPhaseContext.java +++ b/core/src/main/java/org/elasticsearch/search/fetch/FetchSubPhaseContext.java @@ -19,6 +19,16 @@ package org.elasticsearch.search.fetch; -public interface FetchSubPhaseContext { +public class FetchSubPhaseContext { + + private boolean hitExecutionNeeded = false; + + void setHitExecutionNeeded(boolean hitExecutionNeeded) { + this.hitExecutionNeeded = hitExecutionNeeded; + } + + public boolean hitExecutionNeeded() { + return hitExecutionNeeded; + } } diff --git a/core/src/main/java/org/elasticsearch/search/fetch/FetchSubPhaseParseElement.java b/core/src/main/java/org/elasticsearch/search/fetch/FetchSubPhaseParseElement.java new file mode 100644 index 00000000000..6e35b54bd64 --- /dev/null +++ b/core/src/main/java/org/elasticsearch/search/fetch/FetchSubPhaseParseElement.java @@ -0,0 +1,38 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.search.fetch; + +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.search.SearchParseElement; +import org.elasticsearch.search.internal.SearchContext; + +public abstract class FetchSubPhaseParseElement implements SearchParseElement { + + @Override + public void parse(XContentParser parser, SearchContext context) throws Exception { + FetchSubPhaseContext fetchSubPhaseContext = context.getFetchSubPhaseContext(getContextFactory()); + fetchSubPhaseContext.setHitExecutionNeeded(true); + innerParse(parser, fetchSubPhaseContext); + } + + protected abstract void innerParse(XContentParser parser, FetchSubPhaseContext fetchSubPhaseContext) throws Exception; + + protected abstract FetchSubPhase.ContextFactory getContextFactory(); +} diff --git a/core/src/main/java/org/elasticsearch/search/fetch/fielddata/FieldDataFieldsContext.java b/core/src/main/java/org/elasticsearch/search/fetch/fielddata/FieldDataFieldsContext.java index 14c389785b8..9f1bfc08ed7 100644 --- a/core/src/main/java/org/elasticsearch/search/fetch/fielddata/FieldDataFieldsContext.java +++ b/core/src/main/java/org/elasticsearch/search/fetch/fielddata/FieldDataFieldsContext.java @@ -26,7 +26,7 @@ import java.util.List; /** * All the required context to pull a field from the field data cache. */ -public class FieldDataFieldsContext implements FetchSubPhaseContext{ +public class FieldDataFieldsContext extends FetchSubPhaseContext { public static class FieldDataField { private final String name; diff --git a/core/src/main/java/org/elasticsearch/search/fetch/fielddata/FieldDataFieldsFetchSubPhase.java b/core/src/main/java/org/elasticsearch/search/fetch/fielddata/FieldDataFieldsFetchSubPhase.java index 0ff61e7bb3e..0c4e6d2cfa0 100644 --- a/core/src/main/java/org/elasticsearch/search/fetch/fielddata/FieldDataFieldsFetchSubPhase.java +++ b/core/src/main/java/org/elasticsearch/search/fetch/fielddata/FieldDataFieldsFetchSubPhase.java @@ -81,7 +81,7 @@ public class FieldDataFieldsFetchSubPhase implements FetchSubPhase { @Override public boolean hitExecutionNeeded(SearchContext context) { - return context.hasFetchSubPhaseContext(CONTEXT_FACTORY); + return context.getFetchSubPhaseContext(CONTEXT_FACTORY).hitExecutionNeeded(); } @Override diff --git a/core/src/main/java/org/elasticsearch/search/fetch/fielddata/FieldDataFieldsParseElement.java b/core/src/main/java/org/elasticsearch/search/fetch/fielddata/FieldDataFieldsParseElement.java index 2088de6aa86..993a2c45252 100644 --- a/core/src/main/java/org/elasticsearch/search/fetch/fielddata/FieldDataFieldsParseElement.java +++ b/core/src/main/java/org/elasticsearch/search/fetch/fielddata/FieldDataFieldsParseElement.java @@ -20,12 +20,15 @@ package org.elasticsearch.search.fetch.fielddata; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.search.SearchParseElement; +import org.elasticsearch.search.fetch.FetchSubPhase; +import org.elasticsearch.search.fetch.FetchSubPhaseContext; +import org.elasticsearch.search.fetch.FetchSubPhaseParseElement; import org.elasticsearch.search.internal.SearchContext; /** * Parses field name values from the {@code fielddata_fields} parameter in a * search request. - * + *
** { * "query": {...}, @@ -33,10 +36,11 @@ import org.elasticsearch.search.internal.SearchContext; * } **/ -public class FieldDataFieldsParseElement implements SearchParseElement { +public class FieldDataFieldsParseElement extends FetchSubPhaseParseElement { + @Override - public void parse(XContentParser parser, SearchContext context) throws Exception { - FieldDataFieldsContext fieldDataFieldsContext = (FieldDataFieldsContext)context.getFetchSubPhaseContext(FieldDataFieldsFetchSubPhase.CONTEXT_FACTORY); + protected void innerParse(XContentParser parser, FetchSubPhaseContext fetchSubPhaseContext) throws Exception { + FieldDataFieldsContext fieldDataFieldsContext = (FieldDataFieldsContext) fetchSubPhaseContext; XContentParser.Token token = parser.currentToken(); if (token == XContentParser.Token.START_ARRAY) { while (parser.nextToken() != XContentParser.Token.END_ARRAY) { @@ -46,8 +50,13 @@ public class FieldDataFieldsParseElement implements SearchParseElement { } else if (token == XContentParser.Token.VALUE_STRING) { String fieldName = parser.text(); fieldDataFieldsContext.add(new FieldDataFieldsContext.FieldDataField(fieldName)); - } else { + } else { throw new IllegalStateException("Expected either a VALUE_STRING or an START_ARRAY but got " + token); } } + + @Override + protected FetchSubPhase.ContextFactory getContextFactory() { + return FieldDataFieldsFetchSubPhase.CONTEXT_FACTORY; + } } diff --git a/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java b/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java index f178f94bb00..5914b31c742 100644 --- a/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java +++ b/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java @@ -727,11 +727,6 @@ public class DefaultSearchContext extends SearchContext { return timeEstimateCounter; } - @Override - public boolean hasFetchSubPhaseContext(FetchSubPhase.ContextFactory contextFactory) { - return subPhaseContexts.get(contextFactory.getName()) != null; - } - @Override public void innerHits(InnerHitsContext innerHitsContext) { this.innerHitsContext = innerHitsContext; diff --git a/core/src/main/java/org/elasticsearch/search/internal/FilteredSearchContext.java b/core/src/main/java/org/elasticsearch/search/internal/FilteredSearchContext.java index ac17aca1ec1..98874ba0048 100644 --- a/core/src/main/java/org/elasticsearch/search/internal/FilteredSearchContext.java +++ b/core/src/main/java/org/elasticsearch/search/internal/FilteredSearchContext.java @@ -625,9 +625,4 @@ public abstract class FilteredSearchContext extends SearchContext { public FetchSubPhaseContext getFetchSubPhaseContext(FetchSubPhase.ContextFactory subPhase) { return in.getFetchSubPhaseContext(subPhase); } - - @Override - public boolean hasFetchSubPhaseContext(FetchSubPhase.ContextFactory contextFactory) { - return in.hasFetchSubPhaseContext(contextFactory); - } } diff --git a/core/src/main/java/org/elasticsearch/search/internal/SearchContext.java b/core/src/main/java/org/elasticsearch/search/internal/SearchContext.java index 8829c697d72..b9d16b1ab27 100644 --- a/core/src/main/java/org/elasticsearch/search/internal/SearchContext.java +++ b/core/src/main/java/org/elasticsearch/search/internal/SearchContext.java @@ -360,8 +360,6 @@ public abstract class SearchContext implements Releasable, HasContextAndHeaders public abstract Counter timeEstimateCounter(); - public abstract boolean hasFetchSubPhaseContext(FetchSubPhase.ContextFactory contextFactory); - /** * The life time of an object that is used during search execution. */ diff --git a/core/src/test/java/org/elasticsearch/search/fetch/FetchSubPhasePluginTests.java b/core/src/test/java/org/elasticsearch/search/fetch/FetchSubPhasePluginTests.java index df7cc6da5a9..29af400ba58 100644 --- a/core/src/test/java/org/elasticsearch/search/fetch/FetchSubPhasePluginTests.java +++ b/core/src/test/java/org/elasticsearch/search/fetch/FetchSubPhasePluginTests.java @@ -153,7 +153,7 @@ public class FetchSubPhasePluginTests extends ElasticsearchIntegrationTest { @Override public boolean hitExecutionNeeded(SearchContext context) { - return context.hasFetchSubPhaseContext(CONTEXT_FACTORY); + return context.getFetchSubPhaseContext(CONTEXT_FACTORY).hitExecutionNeeded(); } @Override @@ -183,21 +183,27 @@ public class FetchSubPhasePluginTests extends ElasticsearchIntegrationTest { } } - public static class TermVectorsFetchParseElement implements SearchParseElement { + public static class TermVectorsFetchParseElement extends FetchSubPhaseParseElement { @Override - public void parse(XContentParser parser, SearchContext context) throws Exception { + protected void innerParse(XContentParser parser, FetchSubPhaseContext fetchSubPhaseContext) throws Exception { XContentParser.Token token = parser.currentToken(); + TermVectorsFetchContext termVectorsFetchContext = (TermVectorsFetchContext) fetchSubPhaseContext; if (token == XContentParser.Token.VALUE_STRING) { String fieldName = parser.text(); - ((TermVectorsFetchContext) context.getFetchSubPhaseContext(TermVectorsFetchSubPhase.CONTEXT_FACTORY)).setField(fieldName); + termVectorsFetchContext.setField(fieldName); } else { throw new IllegalStateException("Expected a VALUE_STRING but got " + token); } } + + @Override + protected FetchSubPhase.ContextFactory getContextFactory() { + return TermVectorsFetchSubPhase.CONTEXT_FACTORY; + } } - public static class TermVectorsFetchContext implements FetchSubPhaseContext { + public static class TermVectorsFetchContext extends FetchSubPhaseContext { private String field = null; diff --git a/core/src/test/java/org/elasticsearch/test/TestSearchContext.java b/core/src/test/java/org/elasticsearch/test/TestSearchContext.java index fc0d2e0fba3..9c4d0c34d40 100644 --- a/core/src/test/java/org/elasticsearch/test/TestSearchContext.java +++ b/core/src/test/java/org/elasticsearch/test/TestSearchContext.java @@ -213,11 +213,6 @@ public class TestSearchContext extends SearchContext { return subPhaseContexts.get(subPhaseName); } - @Override - public boolean hasFetchSubPhaseContext(FetchSubPhase.ContextFactory contextFactory) { - return subPhaseContexts.get(contextFactory.getName()) != null; - } - @Override public SearchContextHighlight highlight() { return null;