Java Code Examples for org.elasticsearch.common.xcontent.XContent#createParser()

The following examples show how to use org.elasticsearch.common.xcontent.XContent#createParser() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonXContentGenerator.java    From crate with Apache License 2.0 5 votes vote down vote up
protected void copyRawValue(InputStream stream, XContent xContent) throws IOException {
    // EMPTY is safe here because we never call namedObject
    try (XContentParser parser = xContent
             // It's okay to pass the throwing deprecation handler because we
             // should not be writing raw fields when generating JSON
             .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, stream)) {
        copyCurrentStructure(parser);
    }
}
 
Example 2
Source File: ObjectPath.java    From crate with Apache License 2.0 5 votes vote down vote up
public static ObjectPath createFromXContent(XContent xContent, BytesReference input) throws IOException {
    try (XContentParser parser = xContent
            .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, input.streamInput())) {
        if (parser.nextToken() == XContentParser.Token.START_ARRAY) {
            return new ObjectPath(parser.listOrderedMap());
        }
        return new ObjectPath(parser.mapOrdered());
    }
}
 
Example 3
Source File: AnomalyDetectorRestTestCase.java    From anomaly-detection with Apache License 2.0 4 votes vote down vote up
protected final XContentParser createAdParser(XContent xContent, InputStream data) throws IOException {
    return xContent.createParser(TestHelpers.xContentRegistry(), LoggingDeprecationHandler.INSTANCE, data);
}
 
Example 4
Source File: ElasticsearchJavaAPIScriptTest.java    From syncer with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private XContentParser createParser(XContent xContent, BytesReference data) throws IOException {
  return xContent.createParser(xContentRegistry(), data);
}
 
Example 5
Source File: MultiSearchRequest.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public MultiSearchRequest add(BytesReference data, boolean isTemplateRequest, @Nullable String[] indices, @Nullable String[] types, @Nullable String searchType, @Nullable String routing, IndicesOptions indicesOptions, boolean allowExplicitIndex) throws Exception {
    XContent xContent = XContentFactory.xContent(data);
    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);
        }
        searchRequest.searchType(searchType);

        IndicesOptions defaultOptions = IndicesOptions.strictExpandOpenAndForbidClosed();


        // now parse the action
        if (nextMarker - from > 0) {
            try (XContentParser parser = xContent.createParser(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 percolate 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));
                    } 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;
        }
        if (isTemplateRequest) {
            searchRequest.templateSource(data.slice(from,  nextMarker - from));
        } else {
            searchRequest.source(data.slice(from, nextMarker - from));
        }
        // move pointers
        from = nextMarker + 1;

        add(searchRequest);
    }

    return this;
}
 
Example 6
Source File: ESTestCase.java    From crate with Apache License 2.0 4 votes vote down vote up
/**
 * Create a new {@link XContentParser}.
 */
protected final XContentParser createParser(XContent xContent, String data) throws IOException {
    return xContent.createParser(xContentRegistry(), LoggingDeprecationHandler.INSTANCE, data);
}
 
Example 7
Source File: ESTestCase.java    From crate with Apache License 2.0 4 votes vote down vote up
/**
 * Create a new {@link XContentParser}.
 */
protected final XContentParser createParser(XContent xContent, InputStream data) throws IOException {
    return xContent.createParser(xContentRegistry(), LoggingDeprecationHandler.INSTANCE, data);
}
 
Example 8
Source File: ESTestCase.java    From crate with Apache License 2.0 4 votes vote down vote up
/**
 * Create a new {@link XContentParser}.
 */
protected final XContentParser createParser(XContent xContent, byte[] data) throws IOException {
    return xContent.createParser(xContentRegistry(), LoggingDeprecationHandler.INSTANCE, data);
}
 
Example 9
Source File: ESTestCase.java    From crate with Apache License 2.0 4 votes vote down vote up
/**
 * Create a new {@link XContentParser}.
 */
protected final XContentParser createParser(XContent xContent, BytesReference data) throws IOException {
    return xContent.createParser(xContentRegistry(), LoggingDeprecationHandler.INSTANCE, data.streamInput());
}