New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make fetch sub phases pluggable #12400
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bcebb61
make fetch sub phases pluggable
brwe 5f0f669
make fielddata fields a plugin
brwe df339f4
toArray
brwe ea14f67
remove hasFetchSubPhaseContext
brwe 3ee3238
remove unused imports and vars
brwe 24c4b27
add some documentation
brwe 6295738
make Context generic so we don't have to cast
brwe d113b59
add SearchContext to innerParse method
brwe 6754a26
more generic?
brwe b86d07f
generic for sub classes also?
brwe 8a49579
more review comments
brwe File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you want to add sorting here? I forget what the outcome of that discussion was. Right now the order is random and that might be wrong for some use cases. Or maybe we just wait until we see such use cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could not think of any use case and I believe the order was random before. Tests pass so I guess that is OK?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Find by me.