Search in sources :

Example 96 with XContentParser

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.

the class RestMultiSearchAction method parseMultiLineRequest.

/**
     * Parses a multi-line {@link RestRequest} body, instantiating a {@link SearchRequest} for each line and applying the given consumer.
     */
public static void parseMultiLineRequest(RestRequest request, IndicesOptions indicesOptions, boolean allowExplicitIndex, BiConsumer<SearchRequest, XContentParser> consumer) throws IOException {
    String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
    String[] types = Strings.splitStringByCommaToArray(request.param("type"));
    String searchType = request.param("search_type");
    String routing = request.param("routing");
    final Tuple<XContentType, BytesReference> sourceTuple = request.contentOrSourceParam();
    final XContent xContent = sourceTuple.v1().xContent();
    final BytesReference data = sourceTuple.v2();
    int from = 0;
    int length = data.length();
    byte marker = xContent.streamSeparator();
    while (true) {
        int nextMarker = findNextMarker(marker, from, data, length);
        if (nextMarker == -1) {
            break;
        }
        // support first line with \n
        if (nextMarker == 0) {
            from = nextMarker + 1;
            continue;
        }
        SearchRequest searchRequest = new SearchRequest();
        if (indices != null) {
            searchRequest.indices(indices);
        }
        if (indicesOptions != null) {
            searchRequest.indicesOptions(indicesOptions);
        }
        if (types != null && types.length > 0) {
            searchRequest.types(types);
        }
        if (routing != null) {
            searchRequest.routing(routing);
        }
        if (searchType != null) {
            searchRequest.searchType(searchType);
        }
        IndicesOptions defaultOptions = IndicesOptions.strictExpandOpenAndForbidClosed();
        // now parse the action
        if (nextMarker - from > 0) {
            try (XContentParser parser = xContent.createParser(request.getXContentRegistry(), data.slice(from, nextMarker - from))) {
                Map<String, Object> source = parser.map();
                for (Map.Entry<String, Object> entry : source.entrySet()) {
                    Object value = entry.getValue();
                    if ("index".equals(entry.getKey()) || "indices".equals(entry.getKey())) {
                        if (!allowExplicitIndex) {
                            throw new IllegalArgumentException("explicit index in multi search is not allowed");
                        }
                        searchRequest.indices(nodeStringArrayValue(value));
                    } else if ("type".equals(entry.getKey()) || "types".equals(entry.getKey())) {
                        searchRequest.types(nodeStringArrayValue(value));
                    } else if ("search_type".equals(entry.getKey()) || "searchType".equals(entry.getKey())) {
                        searchRequest.searchType(nodeStringValue(value, null));
                    } else if ("request_cache".equals(entry.getKey()) || "requestCache".equals(entry.getKey())) {
                        searchRequest.requestCache(nodeBooleanValue(value, entry.getKey()));
                    } else if ("preference".equals(entry.getKey())) {
                        searchRequest.preference(nodeStringValue(value, null));
                    } else if ("routing".equals(entry.getKey())) {
                        searchRequest.routing(nodeStringValue(value, null));
                    }
                }
                defaultOptions = IndicesOptions.fromMap(source, defaultOptions);
            }
        }
        searchRequest.indicesOptions(defaultOptions);
        // move pointers
        from = nextMarker + 1;
        // now for the body
        nextMarker = findNextMarker(marker, from, data, length);
        if (nextMarker == -1) {
            break;
        }
        BytesReference bytes = data.slice(from, nextMarker - from);
        try (XContentParser parser = xContent.createParser(request.getXContentRegistry(), bytes)) {
            consumer.accept(searchRequest, parser);
        }
        // move pointers
        from = nextMarker + 1;
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) SearchRequest(org.elasticsearch.action.search.SearchRequest) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) XContentType(org.elasticsearch.common.xcontent.XContentType) XContent(org.elasticsearch.common.xcontent.XContent) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) Map(java.util.Map) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 97 with XContentParser

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.

the class RestSearchScrollAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    String scrollId = request.param("scroll_id");
    SearchScrollRequest searchScrollRequest = new SearchScrollRequest();
    searchScrollRequest.scrollId(scrollId);
    String scroll = request.param("scroll");
    if (scroll != null) {
        searchScrollRequest.scroll(new Scroll(parseTimeValue(scroll, null, "scroll")));
    }
    request.withContentOrSourceParamParserOrNull(xContentParser -> {
        if (xContentParser != null) {
            try {
                buildFromContent(xContentParser, searchScrollRequest);
            } catch (IOException e) {
                throw new IllegalArgumentException("Failed to parse request body", e);
            }
        }
    });
    return channel -> client.searchScroll(searchScrollRequest, new RestStatusToXContentListener<>(channel));
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) GET(org.elasticsearch.rest.RestRequest.Method.GET) RestStatusToXContentListener(org.elasticsearch.rest.action.RestStatusToXContentListener) Scroll(org.elasticsearch.search.Scroll) IOException(java.io.IOException) RestController(org.elasticsearch.rest.RestController) XContentParser(org.elasticsearch.common.xcontent.XContentParser) POST(org.elasticsearch.rest.RestRequest.Method.POST) Settings(org.elasticsearch.common.settings.Settings) TimeValue(org.elasticsearch.common.unit.TimeValue) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) TimeValue.parseTimeValue(org.elasticsearch.common.unit.TimeValue.parseTimeValue) SearchScrollRequest(org.elasticsearch.action.search.SearchScrollRequest) Scroll(org.elasticsearch.search.Scroll) IOException(java.io.IOException) SearchScrollRequest(org.elasticsearch.action.search.SearchScrollRequest)

Example 98 with XContentParser

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.

the class RestClearScrollAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    String scrollIds = request.param("scroll_id");
    ClearScrollRequest clearRequest = new ClearScrollRequest();
    clearRequest.setScrollIds(Arrays.asList(splitScrollIds(scrollIds)));
    request.withContentOrSourceParamParserOrNull((xContentParser -> {
        if (xContentParser != null) {
            clearRequest.setScrollIds(null);
            try {
                buildFromContent(xContentParser, clearRequest);
            } catch (IOException e) {
                throw new IllegalArgumentException("Failed to parse request body", e);
            }
        }
    }));
    return channel -> client.clearScroll(clearRequest, new RestStatusToXContentListener<>(channel));
}
Also used : XContentParser(org.elasticsearch.common.xcontent.XContentParser) BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) Arrays(java.util.Arrays) Settings(org.elasticsearch.common.settings.Settings) DELETE(org.elasticsearch.rest.RestRequest.Method.DELETE) ClearScrollRequest(org.elasticsearch.action.search.ClearScrollRequest) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) RestStatusToXContentListener(org.elasticsearch.rest.action.RestStatusToXContentListener) IOException(java.io.IOException) RestController(org.elasticsearch.rest.RestController) Strings(org.elasticsearch.common.Strings) ClearScrollRequest(org.elasticsearch.action.search.ClearScrollRequest) IOException(java.io.IOException)

Example 99 with XContentParser

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.

the class AggregatorFactories method parseAggregators.

private static AggregatorFactories.Builder parseAggregators(QueryParseContext parseContext, int level) throws IOException {
    Matcher validAggMatcher = VALID_AGG_NAME.matcher("");
    AggregatorFactories.Builder factories = new AggregatorFactories.Builder();
    XContentParser.Token token = null;
    XContentParser parser = parseContext.parser();
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token != XContentParser.Token.FIELD_NAME) {
            throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " in [aggs]: aggregations definitions must start with the name of the aggregation.");
        }
        final String aggregationName = parser.currentName();
        if (!validAggMatcher.reset(aggregationName).matches()) {
            throw new ParsingException(parser.getTokenLocation(), "Invalid aggregation name [" + aggregationName + "]. Aggregation names must be alpha-numeric and can only contain '_' and '-'");
        }
        token = parser.nextToken();
        if (token != XContentParser.Token.START_OBJECT) {
            throw new ParsingException(parser.getTokenLocation(), "Aggregation definition for [" + aggregationName + " starts with a [" + token + "], expected a [" + XContentParser.Token.START_OBJECT + "].");
        }
        BaseAggregationBuilder aggBuilder = null;
        AggregatorFactories.Builder subFactories = null;
        Map<String, Object> metaData = null;
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token != XContentParser.Token.FIELD_NAME) {
                throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.FIELD_NAME + "] under a [" + XContentParser.Token.START_OBJECT + "], but got a [" + token + "] in [" + aggregationName + "]", parser.getTokenLocation());
            }
            final String fieldName = parser.currentName();
            token = parser.nextToken();
            if (token == XContentParser.Token.START_OBJECT) {
                switch(fieldName) {
                    case "meta":
                        metaData = parser.map();
                        break;
                    case "aggregations":
                    case "aggs":
                        if (subFactories != null) {
                            throw new ParsingException(parser.getTokenLocation(), "Found two sub aggregation definitions under [" + aggregationName + "]");
                        }
                        subFactories = parseAggregators(parseContext, level + 1);
                        break;
                    default:
                        if (aggBuilder != null) {
                            throw new ParsingException(parser.getTokenLocation(), "Found two aggregation type definitions in [" + aggregationName + "]: [" + aggBuilder.getType() + "] and [" + fieldName + "]");
                        }
                        aggBuilder = parser.namedObject(BaseAggregationBuilder.class, fieldName, new AggParseContext(aggregationName, parseContext));
                }
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.START_OBJECT + "] under [" + fieldName + "], but got a [" + token + "] in [" + aggregationName + "]");
            }
        }
        if (aggBuilder == null) {
            throw new ParsingException(parser.getTokenLocation(), "Missing definition for aggregation [" + aggregationName + "]", parser.getTokenLocation());
        } else {
            if (metaData != null) {
                aggBuilder.setMetaData(metaData);
            }
            if (subFactories != null) {
                aggBuilder.subAggregations(subFactories);
            }
            if (aggBuilder instanceof AggregationBuilder) {
                factories.addAggregator((AggregationBuilder) aggBuilder);
            } else {
                factories.addPipelineAggregator((PipelineAggregationBuilder) aggBuilder);
            }
        }
    }
    return factories;
}
Also used : Matcher(java.util.regex.Matcher) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 100 with XContentParser

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.

the class ElasticsearchExceptionTests method testUnknownFailureToAndFromXContent.

public void testUnknownFailureToAndFromXContent() throws IOException {
    final XContent xContent = randomFrom(XContentType.values()).xContent();
    BytesReference failureBytes = XContentHelper.toXContent((builder, params) -> {
        // Prints a null failure using generateFailureXContent()
        ElasticsearchException.generateFailureXContent(builder, params, null, randomBoolean());
        return builder;
    }, xContent.type(), randomBoolean());
    ElasticsearchException parsedFailure;
    try (XContentParser parser = createParser(xContent, failureBytes)) {
        assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
        assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
        parsedFailure = ElasticsearchException.failureFromXContent(parser);
        assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
        assertNull(parser.nextToken());
    }
    // Failure was null, expecting a "unknown" reason
    assertEquals("Elasticsearch exception [type=exception, reason=unknown]", parsedFailure.getMessage());
    assertEquals(0, parsedFailure.getHeaders().size());
    assertEquals(0, parsedFailure.getMetadata().size());
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContent(org.elasticsearch.common.xcontent.XContent) ToXContent(org.elasticsearch.common.xcontent.ToXContent) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

XContentParser (org.elasticsearch.common.xcontent.XContentParser)463 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)147 ParsingException (org.elasticsearch.common.ParsingException)110 IOException (java.io.IOException)77 BytesReference (org.elasticsearch.common.bytes.BytesReference)63 ArrayList (java.util.ArrayList)59 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)56 XContentType (org.elasticsearch.common.xcontent.XContentType)47 QueryParseContext (org.elasticsearch.index.query.QueryParseContext)44 HashMap (java.util.HashMap)33 Map (java.util.Map)27 Matchers.containsString (org.hamcrest.Matchers.containsString)23 List (java.util.List)22 Settings (org.elasticsearch.common.settings.Settings)18 ToXContent (org.elasticsearch.common.xcontent.ToXContent)16 ShardId (org.elasticsearch.index.shard.ShardId)16 Script (org.elasticsearch.script.Script)16 XContent (org.elasticsearch.common.xcontent.XContent)15 ElasticsearchException (org.elasticsearch.ElasticsearchException)14 NodeClient (org.elasticsearch.client.node.NodeClient)14