/** * creates random suggestion builder, renders it to xContent and back to new instance that should * be equal to original */ public void testFromXContent() throws IOException { Suggesters suggesters = new Suggesters(Collections.emptyMap()); QueryParseContext context = new QueryParseContext(null); context.parseFieldMatcher(new ParseFieldMatcher(Settings.EMPTY)); for (int runs = 0; runs < NUMBER_OF_RUNS; runs++) { SuggestBuilder suggestBuilder = createTestModel(); XContentBuilder xContentBuilder = XContentFactory.contentBuilder(randomFrom(XContentType.values())); if (randomBoolean()) { xContentBuilder.prettyPrint(); } suggestBuilder.toXContent(xContentBuilder, ToXContent.EMPTY_PARAMS); XContentParser parser = XContentHelper.createParser(xContentBuilder.bytes()); context.reset(parser); SuggestBuilder secondSuggestBuilder = SuggestBuilder.fromXContent(context, suggesters); assertNotSame(suggestBuilder, secondSuggestBuilder); assertEquals(suggestBuilder, secondSuggestBuilder); assertEquals(suggestBuilder.hashCode(), secondSuggestBuilder.hashCode()); } }
@Override public void handleRequest( final RestRequest request, final RestChannel channel, final Client client) throws IOException { DeleteByQueryRequest delete = new DeleteByQueryRequest(Strings.splitStringByCommaToArray(request.param("index"))); delete.indicesOptions(IndicesOptions.fromRequest(request, delete.indicesOptions())); delete.routing(request.param("routing")); if (request.hasParam("timeout")) { delete.timeout(request.paramAsTime("timeout", null)); } if (request.hasContent()) { XContentParser requestParser = XContentFactory.xContent(request.content()).createParser(request.content()); QueryParseContext context = new QueryParseContext(indicesQueriesRegistry); context.reset(requestParser); context.parseFieldMatcher(parseFieldMatcher); final QueryBuilder<?> builder = context.parseInnerQueryBuilder(); delete.query(builder); } else { String source = request.param("source"); if (source != null) { XContentParser requestParser = XContentFactory.xContent(source).createParser(source); QueryParseContext context = new QueryParseContext(indicesQueriesRegistry); context.reset(requestParser); final QueryBuilder<?> builder = context.parseInnerQueryBuilder(); delete.query(builder); } else { QueryBuilder<?> queryBuilder = RestActions.urlParamsToQueryBuilder(request); if (queryBuilder != null) { delete.query(queryBuilder); } } } delete.types(Strings.splitStringByCommaToArray(request.param("type"))); client.execute(INSTANCE, delete, new RestToXContentListener<DeleteByQueryResponse>(channel)); }
public SearchSourceBuilder fromXContent(XContentParser parser, QueryParseContext context) throws IOException { SearchSourceBuilder builder = new SearchSourceBuilder(); XContentParser.Token token = parser.currentToken(); String currentFieldName = null; if (token != XContentParser.Token.START_OBJECT && (token = parser.nextToken()) != XContentParser.Token.START_OBJECT) { throw new ParsingException( parser.getTokenLocation(), "Expected [" + XContentParser.Token.START_OBJECT + "] but found [" + token + "]", parser.getTokenLocation()); } while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token.isValue()) { if (context.parseFieldMatcher().match(currentFieldName, FROM_FIELD)) { builder.from = parser.intValue(); } else if (context.parseFieldMatcher().match(currentFieldName, SIZE_FIELD)) { builder.size = parser.intValue(); } else if (context.parseFieldMatcher().match(currentFieldName, TIMEOUT_FIELD)) { builder.timeoutInMillis = parser.longValue(); } else if (context.parseFieldMatcher().match(currentFieldName, TERMINATE_AFTER_FIELD)) { builder.terminateAfter = parser.intValue(); } else if (context.parseFieldMatcher().match(currentFieldName, MIN_SCORE_FIELD)) { builder.minScore = parser.floatValue(); } else if (context.parseFieldMatcher().match(currentFieldName, VERSION_FIELD)) { builder.version = parser.booleanValue(); } else if (context.parseFieldMatcher().match(currentFieldName, EXPLAIN_FIELD)) { builder.explain = parser.booleanValue(); } else if (context.parseFieldMatcher().match(currentFieldName, TRACK_SCORES_FIELD)) { builder.trackScores = parser.booleanValue(); } else if (context.parseFieldMatcher().match(currentFieldName, _SOURCE_FIELD)) { builder.fetchSourceContext = FetchSourceContext.parse(parser, context); } else if (context.parseFieldMatcher().match(currentFieldName, FIELDS_FIELD)) { List<String> fieldNames = new ArrayList<>(); fieldNames.add(parser.text()); builder.fieldNames = fieldNames; } else if (context.parseFieldMatcher().match(currentFieldName, SORT_FIELD)) { builder.sort(parser.text()); } else { throw new ParsingException( parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation()); } } else if (token == XContentParser.Token.START_OBJECT) { if (context.parseFieldMatcher().match(currentFieldName, QUERY_FIELD)) { builder.queryBuilder = context.parseInnerQueryBuilder(); } else if (context.parseFieldMatcher().match(currentFieldName, POST_FILTER_FIELD)) { builder.postQueryBuilder = context.parseInnerQueryBuilder(); } else if (context.parseFieldMatcher().match(currentFieldName, _SOURCE_FIELD)) { builder.fetchSourceContext = FetchSourceContext.parse(parser, context); } else if (context.parseFieldMatcher().match(currentFieldName, SCRIPT_FIELDS_FIELD)) { List<ScriptField> scriptFields = new ArrayList<>(); while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { String scriptFieldName = parser.currentName(); token = parser.nextToken(); if (token == XContentParser.Token.START_OBJECT) { Script script = null; boolean ignoreFailure = false; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token.isValue()) { if (context.parseFieldMatcher().match(currentFieldName, SCRIPT_FIELD)) { script = Script.parse(parser, context.parseFieldMatcher()); } else if (context .parseFieldMatcher() .match(currentFieldName, IGNORE_FAILURE_FIELD)) { ignoreFailure = parser.booleanValue(); } else { throw new ParsingException( parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation()); } } else if (token == XContentParser.Token.START_OBJECT) { if (context.parseFieldMatcher().match(currentFieldName, SCRIPT_FIELD)) { script = Script.parse(parser, context.parseFieldMatcher()); } else { throw new ParsingException( parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation()); } } else { throw new ParsingException( parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation()); } } scriptFields.add(new ScriptField(scriptFieldName, script, ignoreFailure)); } else { throw new ParsingException( parser.getTokenLocation(), "Expected [" + XContentParser.Token.START_OBJECT + "] in [" + currentFieldName + "] but found [" + token + "]", parser.getTokenLocation()); } } builder.scriptFields = scriptFields; } else if (context.parseFieldMatcher().match(currentFieldName, INDICES_BOOST_FIELD)) { ObjectFloatHashMap<String> indexBoost = new ObjectFloatHashMap<String>(); while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token.isValue()) { indexBoost.put(currentFieldName, parser.floatValue()); } else { throw new ParsingException( parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation()); } } builder.indexBoost = indexBoost; } else if (context.parseFieldMatcher().match(currentFieldName, AGGREGATIONS_FIELD)) { List<BytesReference> aggregations = new ArrayList<>(); while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { currentFieldName = parser.currentName(); token = parser.nextToken(); if (token == XContentParser.Token.START_OBJECT) { XContentBuilder xContentBuilder = XContentFactory.contentBuilder(parser.contentType()); xContentBuilder.startObject(); xContentBuilder.field(currentFieldName); xContentBuilder.copyCurrentStructure(parser); xContentBuilder.endObject(); aggregations.add(xContentBuilder.bytes()); } else { throw new ParsingException( parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation()); } } builder.aggregations = aggregations; } else if (context.parseFieldMatcher().match(currentFieldName, HIGHLIGHT_FIELD)) { XContentBuilder xContentBuilder = XContentFactory.contentBuilder(parser.contentType()).copyCurrentStructure(parser); builder.highlightBuilder = xContentBuilder.bytes(); } else if (context.parseFieldMatcher().match(currentFieldName, INNER_HITS_FIELD)) { XContentBuilder xContentBuilder = XContentFactory.contentBuilder(parser.contentType()).copyCurrentStructure(parser); builder.innerHitsBuilder = xContentBuilder.bytes(); } else if (context.parseFieldMatcher().match(currentFieldName, SUGGEST_FIELD)) { XContentBuilder xContentBuilder = XContentFactory.contentBuilder(parser.contentType()); xContentBuilder.copyCurrentStructure(parser); builder.suggestBuilder = xContentBuilder.bytes(); } else if (context.parseFieldMatcher().match(currentFieldName, SORT_FIELD)) { List<BytesReference> sorts = new ArrayList<>(); XContentBuilder xContentBuilder = XContentFactory.contentBuilder(parser.contentType()).copyCurrentStructure(parser); sorts.add(xContentBuilder.bytes()); builder.sorts = sorts; } else if (context.parseFieldMatcher().match(currentFieldName, EXT_FIELD)) { XContentBuilder xContentBuilder = XContentFactory.contentBuilder(parser.contentType()).copyCurrentStructure(parser); builder.ext = xContentBuilder.bytes(); } else { throw new ParsingException( parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation()); } } else if (token == XContentParser.Token.START_ARRAY) { if (context.parseFieldMatcher().match(currentFieldName, FIELDS_FIELD)) { List<String> fieldNames = new ArrayList<>(); while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { if (token == XContentParser.Token.VALUE_STRING) { fieldNames.add(parser.text()); } else { throw new ParsingException( parser.getTokenLocation(), "Expected [" + XContentParser.Token.VALUE_STRING + "] in [" + currentFieldName + "] but found [" + token + "]", parser.getTokenLocation()); } } builder.fieldNames = fieldNames; } else if (context.parseFieldMatcher().match(currentFieldName, FIELDDATA_FIELDS_FIELD)) { List<String> fieldDataFields = new ArrayList<>(); while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { if (token == XContentParser.Token.VALUE_STRING) { fieldDataFields.add(parser.text()); } else { throw new ParsingException( parser.getTokenLocation(), "Expected [" + XContentParser.Token.VALUE_STRING + "] in [" + currentFieldName + "] but found [" + token + "]", parser.getTokenLocation()); } } builder.fieldDataFields = fieldDataFields; } else if (context.parseFieldMatcher().match(currentFieldName, SORT_FIELD)) { List<BytesReference> sorts = new ArrayList<>(); while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { XContentBuilder xContentBuilder = XContentFactory.contentBuilder(parser.contentType()).copyCurrentStructure(parser); sorts.add(xContentBuilder.bytes()); } builder.sorts = sorts; } else if (context.parseFieldMatcher().match(currentFieldName, RESCORE_FIELD)) { List<BytesReference> rescoreBuilders = new ArrayList<>(); while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { XContentBuilder xContentBuilder = XContentFactory.contentBuilder(parser.contentType()).copyCurrentStructure(parser); rescoreBuilders.add(xContentBuilder.bytes()); } builder.rescoreBuilders = rescoreBuilders; } else if (context.parseFieldMatcher().match(currentFieldName, STATS_FIELD)) { List<String> stats = new ArrayList<>(); while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { if (token == XContentParser.Token.VALUE_STRING) { stats.add(parser.text()); } else { throw new ParsingException( parser.getTokenLocation(), "Expected [" + XContentParser.Token.VALUE_STRING + "] in [" + currentFieldName + "] but found [" + token + "]", parser.getTokenLocation()); } } builder.stats = stats; } else if (context.parseFieldMatcher().match(currentFieldName, _SOURCE_FIELD)) { builder.fetchSourceContext = FetchSourceContext.parse(parser, context); } else { throw new ParsingException( parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation()); } } else { throw new ParsingException( parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation()); } } return builder; }