/** * Return the query represented by the XContentStructure object, * returning the cached Query if it has already been parsed. * @param types types to be used during the inner query parsing */ @Override public Query asQuery(String... types) throws IOException { if (!queryParsed) { // query can be null this.query = super.asQuery(types); } return this.query; } }
/** * "Freeze" the parsing content, which means copying the current parser's * structure into an internal {@link BytesReference} to be parsed later. * @return the original XContentStructure object */ public XContentStructure freeze() throws IOException { this.bytes(XContentFactory.smileBuilder().copyCurrentStructure(parseContext.parser()).bytes()); return this; }
public InnerQuery(QueryParseContext parseContext1, @Nullable String... types) throws IOException { super(parseContext1); if (types != null) { String[] origTypes = QueryParseContext.setTypesWithPrevious(types); try { query = parseContext1.parseInnerQuery(); queryParsed = true; } finally { QueryParseContext.setTypes(origTypes); } } else { BytesReference innerBytes = XContentFactory.smileBuilder().copyCurrentStructure(parseContext1.parser()).bytes(); super.bytes(innerBytes); } }
/** * Use the captured bytes to parse the inner query using the specified * types. The original QueryParseContext's parser is switched during this * parsing, so this method is NOT thread-safe. * @param types types to be used during the inner query parsing * @return {@link Query} parsed from the bytes captured in {@code freeze()} */ public Query asQuery(String... types) throws IOException { BytesReference br = this.bytes(); assert br != null : "innerBytes must be set with .bytes(bytes) or .freeze() before parsing"; XContentParser innerParser = XContentHelper.createParser(br); String[] origTypes = QueryParseContext.setTypesWithPrevious(types); XContentParser old = parseContext.parser(); parseContext.parser(innerParser); try { return parseContext.parseInnerQuery(); } finally { parseContext.parser(old); QueryParseContext.setTypes(origTypes); } }