-
Notifications
You must be signed in to change notification settings - Fork 24.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12400 from brwe/plug-fetch-sub-phases
Make fetch sub phases pluggable
- Loading branch information
Showing
16 changed files
with
501 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
core/src/main/java/org/elasticsearch/search/fetch/FetchSubPhaseContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* 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; | ||
|
||
/** | ||
* All configuration and context needed by the FetchSubPhase to execute on hits. | ||
* The only required information in this base class is whether or not the sub phase needs to be run at all. | ||
* It can be extended by FetchSubPhases to hold information the phase needs to execute on hits. | ||
* See {@link org.elasticsearch.search.fetch.FetchSubPhase.ContextFactory} and also {@link org.elasticsearch.search.fetch.fielddata.FieldDataFieldsContext} for an example. | ||
*/ | ||
public class FetchSubPhaseContext { | ||
|
||
// This is to store if the FetchSubPhase should be executed at all. | ||
private boolean hitExecutionNeeded = false; | ||
|
||
/** | ||
* Set if this phase should be executed at all. | ||
*/ | ||
void setHitExecutionNeeded(boolean hitExecutionNeeded) { | ||
this.hitExecutionNeeded = hitExecutionNeeded; | ||
} | ||
|
||
/** | ||
* Returns if this phase be executed at all. | ||
*/ | ||
public boolean hitExecutionNeeded() { | ||
return hitExecutionNeeded; | ||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
core/src/main/java/org/elasticsearch/search/fetch/FetchSubPhaseModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* 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 com.google.common.collect.Lists; | ||
import org.elasticsearch.common.inject.AbstractModule; | ||
import org.elasticsearch.common.inject.multibindings.Multibinder; | ||
import org.elasticsearch.search.fetch.explain.ExplainFetchSubPhase; | ||
import org.elasticsearch.search.fetch.fielddata.FieldDataFieldsFetchSubPhase; | ||
import org.elasticsearch.search.fetch.innerhits.InnerHitsFetchSubPhase; | ||
import org.elasticsearch.search.fetch.matchedqueries.MatchedQueriesFetchSubPhase; | ||
import org.elasticsearch.search.fetch.script.ScriptFieldsFetchSubPhase; | ||
import org.elasticsearch.search.fetch.source.FetchSourceSubPhase; | ||
import org.elasticsearch.search.fetch.version.VersionFetchSubPhase; | ||
import org.elasticsearch.search.highlight.HighlightPhase; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Module for registering fetch sub phases. Fetch phases are executed when the document is finally | ||
* retrieved from the shard. To implement a new fetch phase one needs to implement the following classes and interfaces | ||
* <p/> | ||
* <ul> | ||
* <li> {@link FetchSubPhaseParseElement} </li> | ||
* <li> {@link FetchSubPhase} </li> | ||
* <li> {@link FetchSubPhaseContext} </li> | ||
* </ul> | ||
* <p/> | ||
* The FetchSubPhase must then be registered with this module with {@link FetchSubPhaseModule#registerFetchSubPhase(Class<? extends FetchSubPhase>)}. | ||
* See {@link FieldDataFieldsFetchSubPhase} for an example. | ||
*/ | ||
public class FetchSubPhaseModule extends AbstractModule { | ||
|
||
private List<Class<? extends FetchSubPhase>> fetchSubPhases = Lists.newArrayList(); | ||
|
||
public FetchSubPhaseModule() { | ||
registerFetchSubPhase(ExplainFetchSubPhase.class); | ||
registerFetchSubPhase(FieldDataFieldsFetchSubPhase.class); | ||
registerFetchSubPhase(ScriptFieldsFetchSubPhase.class); | ||
registerFetchSubPhase(FetchSourceSubPhase.class); | ||
registerFetchSubPhase(VersionFetchSubPhase.class); | ||
registerFetchSubPhase(MatchedQueriesFetchSubPhase.class); | ||
registerFetchSubPhase(HighlightPhase.class); | ||
} | ||
|
||
public void registerFetchSubPhase(Class<? extends FetchSubPhase> subPhase) { | ||
fetchSubPhases.add(subPhase); | ||
} | ||
|
||
@Override | ||
protected void configure() { | ||
Multibinder<FetchSubPhase> parserMapBinder = Multibinder.newSetBinder(binder(), FetchSubPhase.class); | ||
for (Class<? extends FetchSubPhase> clazz : fetchSubPhases) { | ||
parserMapBinder.addBinding().to(clazz); | ||
} | ||
bind(InnerHitsFetchSubPhase.class).asEagerSingleton(); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
core/src/main/java/org/elasticsearch/search/fetch/FetchSubPhaseParseElement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* 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; | ||
|
||
/** | ||
* A parse element for a {@link org.elasticsearch.search.fetch.FetchSubPhase} that is used when parsing a search request. | ||
*/ | ||
public abstract class FetchSubPhaseParseElement<SubPhaseContext extends FetchSubPhaseContext> implements SearchParseElement { | ||
|
||
@Override | ||
final public void parse(XContentParser parser, SearchContext context) throws Exception { | ||
SubPhaseContext fetchSubPhaseContext = context.getFetchSubPhaseContext(getContextFactory()); | ||
// this is to make sure that the SubFetchPhase knows it should execute | ||
fetchSubPhaseContext.setHitExecutionNeeded(true); | ||
innerParse(parser, fetchSubPhaseContext, context); | ||
} | ||
|
||
/** | ||
* Implement the actual parsing here. | ||
*/ | ||
protected abstract void innerParse(XContentParser parser, SubPhaseContext fetchSubPhaseContext, SearchContext searchContext) throws Exception; | ||
|
||
/** | ||
* Return the ContextFactory for this FetchSubPhase. | ||
*/ | ||
protected abstract FetchSubPhase.ContextFactory<SubPhaseContext> getContextFactory(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.