/** * Parses the template query replacing template parameters with provided * values. Handles both submitting the template as part of the request as * well as referencing only the template name. * * @param parseContext * parse context containing the templated query. */ @Override @Nullable public Query parse(QueryParseContext parseContext) throws IOException { XContentParser parser = parseContext.parser(); Template template = parse(parser, parseContext.parseFieldMatcher()); ExecutableScript executable = this.scriptService.executable(template, ScriptContext.Standard.SEARCH, SearchContext.current(), Collections.<String, String>emptyMap()); BytesReference querySource = (BytesReference) executable.run(); try (XContentParser qSourceParser = XContentFactory.xContent(querySource).createParser(querySource)) { final QueryParseContext context = new QueryParseContext(parseContext.index(), parseContext.indexQueryParserService()); context.reset(qSourceParser); return context.parseInnerQuery(); } }
@Override public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException { XContentParser parser = parseContext.parser(); XContentParser.Token token = parser.nextToken(); if (token != XContentParser.Token.FIELD_NAME) { throw new QueryParsingException(parseContext, "[wrapper] query malformed"); } String fieldName = parser.currentName(); if (!fieldName.equals("query")) { throw new QueryParsingException(parseContext, "[wrapper] query malformed"); } parser.nextToken(); byte[] querySource = parser.binaryValue(); try (XContentParser qSourceParser = XContentFactory.xContent(querySource).createParser(querySource)) { final QueryParseContext context = new QueryParseContext(parseContext.index(), parseContext.indexQueryParserService()); context.reset(qSourceParser); Query result = context.parseInnerQuery(); parser.nextToken(); parseContext.combineNamedQueries(context); return result; } } }