Fix parsing single `rescore` element in SearchSourceBuilder

We are currently only parsing the array-syntax for the rescore part
in SearchSourceBuilder ("rescore" : [ {...}, {...} ]) . We also need
to support "rescore" : {...}

Closes #18439
This commit is contained in:
Christoph Büscher 2016-05-18 14:25:20 +02:00
parent 1022123f59
commit 808ef6cec7
2 changed files with 55 additions and 0 deletions

View File

@ -1033,6 +1033,9 @@ public final class SearchSourceBuilder extends ToXContentToBytes implements Writ
suggestBuilder = SuggestBuilder.fromXContent(context, suggesters);
} else if (context.getParseFieldMatcher().match(currentFieldName, SORT_FIELD)) {
sorts = new ArrayList<>(SortBuilder.fromXContent(context));
} else if (context.getParseFieldMatcher().match(currentFieldName, RESCORE_FIELD)) {
rescoreBuilders = new ArrayList<>();
rescoreBuilders.add(RescoreBuilder.parseFromXContent(context));
} else if (context.getParseFieldMatcher().match(currentFieldName, EXT_FIELD)) {
XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().copyCurrentStructure(parser);
ext = xContentBuilder.bytes();

View File

@ -72,6 +72,7 @@ import org.elasticsearch.search.aggregations.AggregatorParsers;
import org.elasticsearch.search.fetch.source.FetchSourceContext;
import org.elasticsearch.search.highlight.HighlightBuilderTests;
import org.elasticsearch.search.rescore.QueryRescoreBuilderTests;
import org.elasticsearch.search.rescore.QueryRescorerBuilder;
import org.elasticsearch.search.searchafter.SearchAfterBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.ScoreSortBuilder;
@ -561,6 +562,57 @@ public class SearchSourceBuilderTests extends ESTestCase {
}
}
/**
* test that we can parse the `rescore` element either as single object or as array
*/
public void testParseRescore() throws IOException {
{
String restContent = "{\n" +
" \"query\" : {\n" +
" \"match\": { \"content\": { \"query\": \"foo bar\" }}\n" +
" },\n" +
" \"rescore\": {" +
" \"window_size\": 50,\n" +
" \"query\": {\n" +
" \"rescore_query\" : {\n" +
" \"match\": { \"content\": { \"query\": \"baz\" } }\n" +
" }\n" +
" }\n" +
" }\n" +
"}\n";
try (XContentParser parser = XContentFactory.xContent(restContent).createParser(restContent)) {
SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.fromXContent(createParseContext(parser),
aggParsers, suggesters);
assertEquals(1, searchSourceBuilder.rescores().size());
assertEquals(new QueryRescorerBuilder(QueryBuilders.matchQuery("content", "baz")).windowSize(50),
searchSourceBuilder.rescores().get(0));
}
}
{
String restContent = "{\n" +
" \"query\" : {\n" +
" \"match\": { \"content\": { \"query\": \"foo bar\" }}\n" +
" },\n" +
" \"rescore\": [ {" +
" \"window_size\": 50,\n" +
" \"query\": {\n" +
" \"rescore_query\" : {\n" +
" \"match\": { \"content\": { \"query\": \"baz\" } }\n" +
" }\n" +
" }\n" +
" } ]\n" +
"}\n";
try (XContentParser parser = XContentFactory.xContent(restContent).createParser(restContent)) {
SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.fromXContent(createParseContext(parser),
aggParsers, suggesters);
assertEquals(1, searchSourceBuilder.rescores().size());
assertEquals(new QueryRescorerBuilder(QueryBuilders.matchQuery("content", "baz")).windowSize(50),
searchSourceBuilder.rescores().get(0));
}
}
}
public void testEmptyPostFilter() throws IOException {
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.postFilter(new EmptyQueryBuilder());