Ignore script fields when size is 0 (#31917)

This change adds a check so that when parsing the search source, script fields are 
ignored when the requested search result size is 0. This helps with e.g. clients like 
Kibana that sends a list of script fields that they may need for convenience, but they
don't require any hits. Before this change, user sometimes ran into confusing behaviour, 
e.g. the script compilation limit to breaking although no hits were requested.

Closes #31824
This commit is contained in:
Parth Verma 2018-08-07 14:26:44 +05:30 committed by Christoph Büscher
parent ab81078949
commit 6fe6247dc8
2 changed files with 18 additions and 1 deletions

View File

@ -806,7 +806,7 @@ public class SearchService extends AbstractLifecycleComponent implements IndexEv
throw new SearchContextException(context, "failed to create SearchContextHighlighter", e); throw new SearchContextException(context, "failed to create SearchContextHighlighter", e);
} }
} }
if (source.scriptFields() != null) { if (source.scriptFields() != null && source.size() != 0) {
int maxAllowedScriptFields = context.mapperService().getIndexSettings().getMaxScriptFields(); int maxAllowedScriptFields = context.mapperService().getIndexSettings().getMaxScriptFields();
if (source.scriptFields().size() > maxAllowedScriptFields) { if (source.scriptFields().size() > maxAllowedScriptFields) {
throw new IllegalArgumentException( throw new IllegalArgumentException(

View File

@ -345,6 +345,23 @@ public class SearchServiceTests extends ESSingleNodeTestCase {
} }
} }
public void testIgnoreScriptfieldIfSizeZero() throws IOException {
createIndex("index");
final SearchService service = getInstanceFromNode(SearchService.class);
final IndicesService indicesService = getInstanceFromNode(IndicesService.class);
final IndexService indexService = indicesService.indexServiceSafe(resolveIndex("index"));
final IndexShard indexShard = indexService.getShard(0);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.scriptField("field" + 0,
new Script(ScriptType.INLINE, MockScriptEngine.NAME, CustomScriptPlugin.DUMMY_SCRIPT, Collections.emptyMap()));
searchSourceBuilder.size(0);
try (SearchContext context = service.createContext(new ShardSearchLocalRequest(indexShard.shardId(), 1, SearchType.DEFAULT,
searchSourceBuilder, new String[0], false, new AliasFilter(null, Strings.EMPTY_ARRAY), 1.0f, true, null, null))) {
assertEquals(0, context.scriptFields().fields().size());
}
}
public static class FailOnRewriteQueryPlugin extends Plugin implements SearchPlugin { public static class FailOnRewriteQueryPlugin extends Plugin implements SearchPlugin {
@Override @Override
public List<QuerySpec<?>> getQueries() { public List<QuerySpec<?>> getQueries() {