FetchSubPhase to support a single parser that extends SearchExtParser

This commit is contained in:
javanna 2016-09-08 10:30:48 +02:00 committed by Luca Cavanna
parent f9530dfe8f
commit 12eaeb3945
5 changed files with 40 additions and 18 deletions

View File

@ -63,7 +63,7 @@ import org.elasticsearch.search.dfs.DfsPhase;
import org.elasticsearch.search.dfs.DfsSearchResult;
import org.elasticsearch.search.fetch.FetchPhase;
import org.elasticsearch.search.fetch.FetchSearchResult;
import org.elasticsearch.search.fetch.FetchSubPhaseParser;
import org.elasticsearch.search.fetch.SearchExtParser;
import org.elasticsearch.search.fetch.QueryFetchSearchResult;
import org.elasticsearch.search.fetch.ScrollQueryFetchSearchResult;
import org.elasticsearch.search.fetch.ShardFetchRequest;
@ -144,7 +144,7 @@ public class SearchService extends AbstractLifecycleComponent implements IndexEv
private final ConcurrentMapLong<SearchContext> activeContexts = ConcurrentCollections.newConcurrentMapLongWithAggressiveConcurrency();
private final Map<String, FetchSubPhaseParser> fetchPhaseParsers;
private final Map<String, SearchExtParser> fetchPhaseParsers;
private final ParseFieldMatcher parseFieldMatcher;
@ -763,8 +763,8 @@ public class SearchService extends AbstractLifecycleComponent implements IndexEv
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = extParser.currentName();
} else {
FetchSubPhaseParser fetchSubPhaseParser = this.fetchPhaseParsers.get(currentFieldName);
if (fetchSubPhaseParser == null) {
SearchExtParser searchExtParser = this.fetchPhaseParsers.get(currentFieldName);
if (searchExtParser == null) {
if (currentFieldName != null && currentFieldName.equals("suggest")) {
throw new SearchParseException(context,
"suggest is not supported in [ext], please use SearchSourceBuilder#suggest(SuggestBuilder) instead",
@ -773,7 +773,7 @@ public class SearchService extends AbstractLifecycleComponent implements IndexEv
throw new SearchParseException(context, "Unknown element [" + currentFieldName + "] in [ext]",
extParser.getTokenLocation());
} else {
Object fetchSubPhaseBuilder = fetchSubPhaseParser.parse(extParser);
Object fetchSubPhaseBuilder = searchExtParser.parse(extParser);
context.putFetchSubPhaseBuilder(currentFieldName, fetchSubPhaseBuilder);
}
}

View File

@ -77,10 +77,12 @@ public class FetchPhase implements SearchPhase {
this.fetchSubPhases[fetchSubPhases.size()] = new InnerHitsFetchSubPhase(this);
}
public Map<String, ? extends FetchSubPhaseParser> parsers() {
Map<String, FetchSubPhaseParser> parsers = new HashMap<>();
public Map<String, ? extends SearchExtParser> parsers() {
Map<String, SearchExtParser> parsers = new HashMap<>();
for (FetchSubPhase fetchSubPhase : fetchSubPhases) {
parsers.putAll(fetchSubPhase.parsers());
if (fetchSubPhase.parser() != null) {
parsers.put(fetchSubPhase.parser().getName(), fetchSubPhase.parser());
}
}
return unmodifiableMap(parsers);
}

View File

@ -25,7 +25,6 @@ import org.apache.lucene.search.IndexSearcher;
import org.elasticsearch.search.internal.InternalSearchHit;
import org.elasticsearch.search.internal.SearchContext;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -77,8 +76,11 @@ public interface FetchSubPhase {
}
default Map<String, ? extends FetchSubPhaseParser> parsers() {
return Collections.emptyMap();
/**
* Returns the parser for the optional config to be put in the ext section of the search request
*/
default SearchExtParser parser() {
return null;
}
/**

View File

@ -22,9 +22,17 @@ package org.elasticsearch.search.fetch;
import org.elasticsearch.common.xcontent.XContentParser;
/**
* Parser for the ext section of a search request, which can hold custom fetch sub phase
* Parser for the ext section of a search request, which can hold custom fetch sub phases config
*/
public interface FetchSubPhaseParser {
public interface SearchExtParser {
/**
* Returns the name of the element that this parser is able to parse
*/
String getName();
/**
* Parses the element whose name is returned by {@link #getName()}
*/
Object parse(XContentParser parser) throws Exception;
}

View File

@ -50,7 +50,6 @@ import java.util.List;
import java.util.Map;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
import static org.elasticsearch.client.Requests.indexRequest;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse;
@ -109,8 +108,8 @@ public class FetchSubPhasePluginIT extends ESIntegTestCase {
private static final String NAME = "term_vectors_fetch";
@Override
public Map<String, ? extends FetchSubPhaseParser> parsers() {
return singletonMap(NAME, new TermVectorsFetchParser());
public SearchExtParser parser() {
return TermVectorsFetchParser.INSTANCE;
}
@Override
@ -145,7 +144,18 @@ public class FetchSubPhasePluginIT extends ESIntegTestCase {
}
}
public static class TermVectorsFetchParser implements FetchSubPhaseParser {
public static final class TermVectorsFetchParser implements SearchExtParser {
private static final TermVectorsFetchParser INSTANCE = new TermVectorsFetchParser();
private TermVectorsFetchParser() {
}
@Override
public String getName() {
return TermVectorsFetchSubPhase.NAME;
}
@Override
public TermVectorsFetchBuilder parse(XContentParser parser) throws Exception {
String field;
@ -162,7 +172,7 @@ public class FetchSubPhasePluginIT extends ESIntegTestCase {
}
}
public static class TermVectorsFetchBuilder {
public static final class TermVectorsFetchBuilder {
private final String field;
private TermVectorsFetchBuilder(String field) {