diff --git a/core/src/main/java/org/elasticsearch/search/rescore/RescoreParseElement.java b/core/src/main/java/org/elasticsearch/search/rescore/RescoreParseElement.java deleted file mode 100644 index 702d3666692..00000000000 --- a/core/src/main/java/org/elasticsearch/search/rescore/RescoreParseElement.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * 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.rescore; - -import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.index.query.QueryShardContext; -import org.elasticsearch.search.SearchParseElement; -import org.elasticsearch.search.internal.SearchContext; - -import java.io.IOException; - -/** - * - */ -public class RescoreParseElement implements SearchParseElement { - - @Override - public void parse(XContentParser parser, SearchContext context) throws Exception { - if (parser.currentToken() == XContentParser.Token.START_ARRAY) { - while (parser.nextToken() != XContentParser.Token.END_ARRAY) { - context.addRescore(parseSingleRescoreContext(parser, context.getQueryShardContext())); - } - } else { - context.addRescore(parseSingleRescoreContext(parser, context.getQueryShardContext())); - } - } - - public RescoreSearchContext parseSingleRescoreContext(XContentParser parser, QueryShardContext context) throws ElasticsearchParseException, IOException { - String fieldName = null; - RescoreSearchContext rescoreContext = null; - Integer windowSize = null; - XContentParser.Token token; - while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { - if (token == XContentParser.Token.FIELD_NAME) { - fieldName = parser.currentName(); - if (QueryRescorer.NAME.equals(fieldName)) { - // we only have one at this point - Rescorer rescorer = QueryRescorer.INSTANCE; - token = parser.nextToken(); - if (token != XContentParser.Token.START_OBJECT) { - throw new ElasticsearchParseException("rescore type malformed, must start with start_object"); - } - rescoreContext = rescorer.parse(parser, context); - } - } else if (token.isValue()) { - if ("window_size".equals(fieldName)) { - windowSize = parser.intValue(); - } else { - throw new IllegalArgumentException("rescore doesn't support [" + fieldName + "]"); - } - } - } - if (rescoreContext == null) { - throw new IllegalArgumentException("missing rescore type"); - } - if (windowSize != null) { - rescoreContext.setWindowSize(windowSize.intValue()); - } - return rescoreContext; - } - -} diff --git a/core/src/main/java/org/elasticsearch/search/rescore/RescorePhase.java b/core/src/main/java/org/elasticsearch/search/rescore/RescorePhase.java index d1592aa2c2a..732a80bad20 100644 --- a/core/src/main/java/org/elasticsearch/search/rescore/RescorePhase.java +++ b/core/src/main/java/org/elasticsearch/search/rescore/RescorePhase.java @@ -29,23 +29,25 @@ import org.elasticsearch.search.SearchPhase; import org.elasticsearch.search.internal.SearchContext; import java.io.IOException; +import java.util.Collections; import java.util.Map; -import static java.util.Collections.singletonMap; - /** */ public class RescorePhase extends AbstractComponent implements SearchPhase { - private static final Map PARSE_ELEMENTS = singletonMap("rescore", new RescoreParseElement()); @Inject public RescorePhase(Settings settings) { super(settings); } + /** + * rescorers do not have a parse element, they use + * {@link RescoreBuilder#parseFromXContent(org.elasticsearch.index.query.QueryParseContext)} for parsing instead. + */ @Override public Map parseElements() { - return PARSE_ELEMENTS; + return Collections.emptyMap(); } @Override diff --git a/core/src/test/java/org/elasticsearch/search/rescore/QueryRescoreBuilderTests.java b/core/src/test/java/org/elasticsearch/search/rescore/QueryRescoreBuilderTests.java index 0386dd847f8..7e4ff9449b6 100644 --- a/core/src/test/java/org/elasticsearch/search/rescore/QueryRescoreBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/rescore/QueryRescoreBuilderTests.java @@ -19,6 +19,7 @@ package org.elasticsearch.search.rescore; +import org.apache.lucene.search.Query; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.Version; import org.elasticsearch.cluster.metadata.IndexMetaData; @@ -153,8 +154,8 @@ public class QueryRescoreBuilderTests extends ESTestCase { } /** - * test that build() outputs a {@link RescoreSearchContext} that is similar to the one - * we would get when parsing the xContent the test rescore builder is rendering out + * test that build() outputs a {@link RescoreSearchContext} that has the same properties + * than the test builder */ public void testBuildRescoreSearchContext() throws ElasticsearchParseException, IOException { Settings indexSettings = Settings.settingsBuilder() @@ -171,18 +172,16 @@ public class QueryRescoreBuilderTests extends ESTestCase { }; for (int runs = 0; runs < NUMBER_OF_TESTBUILDERS; runs++) { - RescoreBuilder rescoreBuilder = randomRescoreBuilder(); + QueryRescorerBuilder rescoreBuilder = randomRescoreBuilder(); QueryRescoreContext rescoreContext = rescoreBuilder.build(mockShardContext); - XContentParser parser = createParser(rescoreBuilder); - - QueryRescoreContext parsedRescoreContext = (QueryRescoreContext) new RescoreParseElement().parseSingleRescoreContext(parser, - mockShardContext); - assertNotSame(rescoreContext, parsedRescoreContext); - assertEquals(rescoreContext.window(), parsedRescoreContext.window()); - assertEquals(rescoreContext.query(), parsedRescoreContext.query()); - assertEquals(rescoreContext.queryWeight(), parsedRescoreContext.queryWeight(), Float.MIN_VALUE); - assertEquals(rescoreContext.rescoreQueryWeight(), parsedRescoreContext.rescoreQueryWeight(), Float.MIN_VALUE); - assertEquals(rescoreContext.scoreMode(), parsedRescoreContext.scoreMode()); + int expectedWindowSize = rescoreBuilder.windowSize() == null ? QueryRescoreContext.DEFAULT_WINDOW_SIZE : + rescoreBuilder.windowSize().intValue(); + assertEquals(expectedWindowSize, rescoreContext.window()); + Query expectedQuery = QueryBuilder.rewriteQuery(rescoreBuilder.getRescoreQuery(), mockShardContext).toQuery(mockShardContext); + assertEquals(expectedQuery, rescoreContext.query()); + assertEquals(rescoreBuilder.getQueryWeight(), rescoreContext.queryWeight(), Float.MIN_VALUE); + assertEquals(rescoreBuilder.getRescoreQueryWeight(), rescoreContext.rescoreQueryWeight(), Float.MIN_VALUE); + assertEquals(rescoreBuilder.getScoreMode(), rescoreContext.scoreMode()); } } @@ -318,7 +317,7 @@ public class QueryRescoreBuilderTests extends ESTestCase { /** * create random shape that is put under test */ - public static org.elasticsearch.search.rescore.QueryRescorerBuilder randomRescoreBuilder() { + public static QueryRescorerBuilder randomRescoreBuilder() { QueryBuilder queryBuilder = new MatchAllQueryBuilder().boost(randomFloat()) .queryName(randomAsciiOfLength(20)); org.elasticsearch.search.rescore.QueryRescorerBuilder rescorer = new