Search in sources :

Example 16 with XContentType

use of org.elasticsearch.common.xcontent.XContentType 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 17 with XContentType

use of org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.

the class RestSimulatePipelineAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException {
    Tuple<XContentType, BytesReference> sourceTuple = restRequest.contentOrSourceParam();
    SimulatePipelineRequest request = new SimulatePipelineRequest(sourceTuple.v2(), sourceTuple.v1());
    request.setId(restRequest.param("id"));
    request.setVerbose(restRequest.paramAsBoolean("verbose", false));
    return channel -> client.admin().cluster().simulatePipeline(request, new RestToXContentListener<>(channel));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) Settings(org.elasticsearch.common.settings.Settings) XContentType(org.elasticsearch.common.xcontent.XContentType) RestToXContentListener(org.elasticsearch.rest.action.RestToXContentListener) SimulatePipelineRequest(org.elasticsearch.action.ingest.SimulatePipelineRequest) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) IOException(java.io.IOException) RestController(org.elasticsearch.rest.RestController) BytesReference(org.elasticsearch.common.bytes.BytesReference) Tuple(org.elasticsearch.common.collect.Tuple) XContentType(org.elasticsearch.common.xcontent.XContentType) SimulatePipelineRequest(org.elasticsearch.action.ingest.SimulatePipelineRequest)

Example 18 with XContentType

use of org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.

the class BulkRequestTests method testSmileIsSupported.

public void testSmileIsSupported() throws IOException {
    XContentType xContentType = XContentType.SMILE;
    BytesReference data;
    try (BytesStreamOutput out = new BytesStreamOutput()) {
        try (XContentBuilder builder = XContentFactory.contentBuilder(xContentType, out)) {
            builder.startObject();
            builder.startObject("index");
            builder.field("_index", "index");
            builder.field("_type", "type");
            builder.field("_id", "test");
            builder.endObject();
            builder.endObject();
        }
        out.write(xContentType.xContent().streamSeparator());
        try (XContentBuilder builder = XContentFactory.contentBuilder(xContentType, out)) {
            builder.startObject();
            builder.field("field", "value");
            builder.endObject();
        }
        out.write(xContentType.xContent().streamSeparator());
        data = out.bytes();
    }
    BulkRequest bulkRequest = new BulkRequest();
    bulkRequest.add(data, null, null, xContentType);
    assertEquals(1, bulkRequest.requests().size());
    DocWriteRequest docWriteRequest = bulkRequest.requests().get(0);
    assertEquals(DocWriteRequest.OpType.INDEX, docWriteRequest.opType());
    assertEquals("index", docWriteRequest.index());
    assertEquals("type", docWriteRequest.type());
    assertEquals("test", docWriteRequest.id());
    assertThat(docWriteRequest, instanceOf(IndexRequest.class));
    IndexRequest request = (IndexRequest) docWriteRequest;
    assertEquals(1, request.sourceAsMap().size());
    assertEquals("value", request.sourceAsMap().get("field"));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 19 with XContentType

use of org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.

the class BulkItemResponseTests method testToAndFromXContent.

public void testToAndFromXContent() throws IOException {
    final XContentType xContentType = randomFrom(XContentType.values());
    for (DocWriteRequest.OpType opType : DocWriteRequest.OpType.values()) {
        int bulkItemId = randomIntBetween(0, 100);
        boolean humanReadable = randomBoolean();
        Tuple<? extends DocWriteResponse, ? extends DocWriteResponse> randomDocWriteResponses = null;
        if (opType == DocWriteRequest.OpType.INDEX || opType == DocWriteRequest.OpType.CREATE) {
            randomDocWriteResponses = IndexResponseTests.randomIndexResponse();
        } else if (opType == DocWriteRequest.OpType.DELETE) {
            randomDocWriteResponses = DeleteResponseTests.randomDeleteResponse();
        } else if (opType == DocWriteRequest.OpType.UPDATE) {
            randomDocWriteResponses = UpdateResponseTests.randomUpdateResponse(xContentType);
        } else {
            fail("Test does not support opType [" + opType + "]");
        }
        BulkItemResponse bulkItemResponse = new BulkItemResponse(bulkItemId, opType, randomDocWriteResponses.v1());
        BulkItemResponse expectedBulkItemResponse = new BulkItemResponse(bulkItemId, opType, randomDocWriteResponses.v2());
        BytesReference originalBytes = toXContent(bulkItemResponse, xContentType, humanReadable);
        // Shuffle the XContent fields
        if (randomBoolean()) {
            try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
                originalBytes = shuffleXContent(parser, randomBoolean()).bytes();
            }
        }
        BulkItemResponse parsedBulkItemResponse;
        try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
            assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
            parsedBulkItemResponse = BulkItemResponse.fromXContent(parser, bulkItemId);
            assertNull(parser.nextToken());
        }
        assertBulkItemResponse(expectedBulkItemResponse, parsedBulkItemResponse);
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 20 with XContentType

use of org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.

the class SuggestTests method testFromXContent.

public void testFromXContent() throws IOException {
    ToXContent.Params params = new ToXContent.MapParams(Collections.singletonMap(RestSearchAction.TYPED_KEYS_PARAM, "true"));
    Suggest suggest = createTestItem();
    XContentType xContentType = randomFrom(XContentType.values());
    boolean humanReadable = randomBoolean();
    BytesReference originalBytes = toXContent(suggest, xContentType, params, humanReadable);
    Suggest parsed;
    try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
        ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
        ensureFieldName(parser, parser.nextToken(), Suggest.NAME);
        ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
        parsed = Suggest.fromXContent(parser);
        assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
        assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
        assertNull(parser.nextToken());
    }
    assertEquals(suggest.size(), parsed.size());
    for (Suggestion suggestion : suggest) {
        Suggestion<? extends Entry<? extends Option>> parsedSuggestion = parsed.getSuggestion(suggestion.getName());
        assertNotNull(parsedSuggestion);
        assertEquals(suggestion.getClass(), parsedSuggestion.getClass());
    }
    assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, params, humanReadable), xContentType);
}
Also used : ToXContent(org.elasticsearch.common.xcontent.ToXContent) BytesReference(org.elasticsearch.common.bytes.BytesReference) CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) Suggestion(org.elasticsearch.search.suggest.Suggest.Suggestion) PhraseSuggestion(org.elasticsearch.search.suggest.phrase.PhraseSuggestion) TermSuggestion(org.elasticsearch.search.suggest.term.TermSuggestion) XContentType(org.elasticsearch.common.xcontent.XContentType) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

XContentType (org.elasticsearch.common.xcontent.XContentType)82 BytesReference (org.elasticsearch.common.bytes.BytesReference)48 XContentParser (org.elasticsearch.common.xcontent.XContentParser)42 Map (java.util.Map)19 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)19 HashMap (java.util.HashMap)18 IndexRequest (org.elasticsearch.action.index.IndexRequest)11 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)10 IOException (java.io.IOException)9 BytesRef (org.apache.lucene.util.BytesRef)8 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)8 Collections.emptyMap (java.util.Collections.emptyMap)6 ElasticsearchException (org.elasticsearch.ElasticsearchException)6 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)6 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)6 Collections.singletonMap (java.util.Collections.singletonMap)5 HttpEntity (org.apache.http.HttpEntity)5 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)5 GetRequest (org.elasticsearch.action.get.GetRequest)5 WriteRequest (org.elasticsearch.action.support.WriteRequest)5