Search in sources :

Example 96 with XContentParser

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

the class RequestTests method testUpdate.

public void testUpdate() throws IOException {
    XContentType xContentType = randomFrom(XContentType.values());
    Map<String, String> expectedParams = new HashMap<>();
    String index = randomAsciiOfLengthBetween(3, 10);
    String type = randomAsciiOfLengthBetween(3, 10);
    String id = randomAsciiOfLengthBetween(3, 10);
    UpdateRequest updateRequest = new UpdateRequest(index, type, id);
    updateRequest.detectNoop(randomBoolean());
    if (randomBoolean()) {
        BytesReference source = RandomObjects.randomSource(random(), xContentType);
        updateRequest.doc(new IndexRequest().source(source, xContentType));
        boolean docAsUpsert = randomBoolean();
        updateRequest.docAsUpsert(docAsUpsert);
        if (docAsUpsert) {
            expectedParams.put("doc_as_upsert", "true");
        }
    } else {
        updateRequest.script(new Script("_value + 1"));
        updateRequest.scriptedUpsert(randomBoolean());
    }
    if (randomBoolean()) {
        BytesReference source = RandomObjects.randomSource(random(), xContentType);
        updateRequest.upsert(new IndexRequest().source(source, xContentType));
    }
    if (randomBoolean()) {
        String routing = randomAsciiOfLengthBetween(3, 10);
        updateRequest.routing(routing);
        expectedParams.put("routing", routing);
    }
    if (randomBoolean()) {
        String parent = randomAsciiOfLengthBetween(3, 10);
        updateRequest.parent(parent);
        expectedParams.put("parent", parent);
    }
    if (randomBoolean()) {
        String timeout = randomTimeValue();
        updateRequest.timeout(timeout);
        expectedParams.put("timeout", timeout);
    } else {
        expectedParams.put("timeout", ReplicationRequest.DEFAULT_TIMEOUT.getStringRep());
    }
    if (randomBoolean()) {
        WriteRequest.RefreshPolicy refreshPolicy = randomFrom(WriteRequest.RefreshPolicy.values());
        updateRequest.setRefreshPolicy(refreshPolicy);
        if (refreshPolicy != WriteRequest.RefreshPolicy.NONE) {
            expectedParams.put("refresh", refreshPolicy.getValue());
        }
    }
    if (randomBoolean()) {
        int waitForActiveShards = randomIntBetween(0, 10);
        updateRequest.waitForActiveShards(waitForActiveShards);
        expectedParams.put("wait_for_active_shards", String.valueOf(waitForActiveShards));
    }
    if (randomBoolean()) {
        long version = randomLong();
        updateRequest.version(version);
        if (version != Versions.MATCH_ANY) {
            expectedParams.put("version", Long.toString(version));
        }
    }
    if (randomBoolean()) {
        VersionType versionType = randomFrom(VersionType.values());
        updateRequest.versionType(versionType);
        if (versionType != VersionType.INTERNAL) {
            expectedParams.put("version_type", versionType.name().toLowerCase(Locale.ROOT));
        }
    }
    if (randomBoolean()) {
        int retryOnConflict = randomIntBetween(0, 5);
        updateRequest.retryOnConflict(retryOnConflict);
        if (retryOnConflict > 0) {
            expectedParams.put("retry_on_conflict", String.valueOf(retryOnConflict));
        }
    }
    if (randomBoolean()) {
        randomizeFetchSourceContextParams(updateRequest::fetchSource, expectedParams);
    }
    Request request = Request.update(updateRequest);
    assertEquals("/" + index + "/" + type + "/" + id + "/_update", request.endpoint);
    assertEquals(expectedParams, request.params);
    assertEquals("POST", request.method);
    HttpEntity entity = request.entity;
    assertNotNull(entity);
    assertTrue(entity instanceof ByteArrayEntity);
    UpdateRequest parsedUpdateRequest = new UpdateRequest();
    XContentType entityContentType = XContentType.fromMediaTypeOrFormat(entity.getContentType().getValue());
    try (XContentParser parser = createParser(entityContentType.xContent(), entity.getContent())) {
        parsedUpdateRequest.fromXContent(parser);
    }
    assertEquals(updateRequest.scriptedUpsert(), parsedUpdateRequest.scriptedUpsert());
    assertEquals(updateRequest.docAsUpsert(), parsedUpdateRequest.docAsUpsert());
    assertEquals(updateRequest.detectNoop(), parsedUpdateRequest.detectNoop());
    assertEquals(updateRequest.fetchSource(), parsedUpdateRequest.fetchSource());
    assertEquals(updateRequest.script(), parsedUpdateRequest.script());
    if (updateRequest.doc() != null) {
        assertToXContentEquivalent(updateRequest.doc().source(), parsedUpdateRequest.doc().source(), xContentType);
    } else {
        assertNull(parsedUpdateRequest.doc());
    }
    if (updateRequest.upsertRequest() != null) {
        assertToXContentEquivalent(updateRequest.upsertRequest().source(), parsedUpdateRequest.upsertRequest().source(), xContentType);
    } else {
        assertNull(parsedUpdateRequest.upsertRequest());
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) Script(org.elasticsearch.script.Script) HttpEntity(org.apache.http.HttpEntity) HashMap(java.util.HashMap) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) WriteRequest(org.elasticsearch.action.support.WriteRequest) ReplicatedWriteRequest(org.elasticsearch.action.support.replication.ReplicatedWriteRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) WriteRequest(org.elasticsearch.action.support.WriteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) BulkShardRequest(org.elasticsearch.action.bulk.BulkShardRequest) GetRequest(org.elasticsearch.action.get.GetRequest) ReplicatedWriteRequest(org.elasticsearch.action.support.replication.ReplicatedWriteRequest) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) ReplicationRequest(org.elasticsearch.action.support.replication.ReplicationRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) VersionType(org.elasticsearch.index.VersionType) XContentType(org.elasticsearch.common.xcontent.XContentType) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 97 with XContentParser

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

the class RequestTests method testIndex.

public void testIndex() throws IOException {
    String index = randomAsciiOfLengthBetween(3, 10);
    String type = randomAsciiOfLengthBetween(3, 10);
    IndexRequest indexRequest = new IndexRequest(index, type);
    String id = randomBoolean() ? randomAsciiOfLengthBetween(3, 10) : null;
    indexRequest.id(id);
    Map<String, String> expectedParams = new HashMap<>();
    String method = "POST";
    if (id != null) {
        method = "PUT";
        if (randomBoolean()) {
            indexRequest.opType(DocWriteRequest.OpType.CREATE);
        }
    }
    setRandomTimeout(indexRequest, expectedParams);
    setRandomRefreshPolicy(indexRequest, expectedParams);
    // There is some logic around _create endpoint and version/version type
    if (indexRequest.opType() == DocWriteRequest.OpType.CREATE) {
        indexRequest.version(randomFrom(Versions.MATCH_ANY, Versions.MATCH_DELETED));
        expectedParams.put("version", Long.toString(Versions.MATCH_DELETED));
    } else {
        setRandomVersion(indexRequest, expectedParams);
        setRandomVersionType(indexRequest, expectedParams);
    }
    if (frequently()) {
        if (randomBoolean()) {
            String routing = randomAsciiOfLengthBetween(3, 10);
            indexRequest.routing(routing);
            expectedParams.put("routing", routing);
        }
        if (randomBoolean()) {
            String parent = randomAsciiOfLengthBetween(3, 10);
            indexRequest.parent(parent);
            expectedParams.put("parent", parent);
        }
        if (randomBoolean()) {
            String pipeline = randomAsciiOfLengthBetween(3, 10);
            indexRequest.setPipeline(pipeline);
            expectedParams.put("pipeline", pipeline);
        }
    }
    XContentType xContentType = randomFrom(XContentType.values());
    int nbFields = randomIntBetween(0, 10);
    try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) {
        builder.startObject();
        for (int i = 0; i < nbFields; i++) {
            builder.field("field_" + i, i);
        }
        builder.endObject();
        indexRequest.source(builder);
    }
    Request request = Request.index(indexRequest);
    if (indexRequest.opType() == DocWriteRequest.OpType.CREATE) {
        assertEquals("/" + index + "/" + type + "/" + id + "/_create", request.endpoint);
    } else if (id != null) {
        assertEquals("/" + index + "/" + type + "/" + id, request.endpoint);
    } else {
        assertEquals("/" + index + "/" + type, request.endpoint);
    }
    assertEquals(expectedParams, request.params);
    assertEquals(method, request.method);
    HttpEntity entity = request.entity;
    assertNotNull(entity);
    assertTrue(entity instanceof ByteArrayEntity);
    try (XContentParser parser = createParser(xContentType.xContent(), entity.getContent())) {
        assertEquals(nbFields, parser.map().size());
    }
}
Also used : XContentType(org.elasticsearch.common.xcontent.XContentType) HttpEntity(org.apache.http.HttpEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) HashMap(java.util.HashMap) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) WriteRequest(org.elasticsearch.action.support.WriteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) BulkShardRequest(org.elasticsearch.action.bulk.BulkShardRequest) GetRequest(org.elasticsearch.action.get.GetRequest) ReplicatedWriteRequest(org.elasticsearch.action.support.replication.ReplicatedWriteRequest) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) ReplicationRequest(org.elasticsearch.action.support.replication.ReplicationRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 98 with XContentParser

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

the class ShardSearchFailure method fromXContent.

public static ShardSearchFailure fromXContent(XContentParser parser) throws IOException {
    XContentParser.Token token;
    ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser::getTokenLocation);
    String currentFieldName = null;
    int shardId = -1;
    String indexName = null;
    String nodeId = null;
    ElasticsearchException exception = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token.isValue()) {
            if (SHARD_FIELD.equals(currentFieldName)) {
                shardId = parser.intValue();
            } else if (INDEX_FIELD.equals(currentFieldName)) {
                indexName = parser.text();
            } else if (NODE_FIELD.equals(currentFieldName)) {
                nodeId = parser.text();
            } else {
                throwUnknownField(currentFieldName, parser.getTokenLocation());
            }
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (REASON_FIELD.equals(currentFieldName)) {
                exception = ElasticsearchException.fromXContent(parser);
            } else {
                throwUnknownField(currentFieldName, parser.getTokenLocation());
            }
        } else {
            throwUnknownToken(token, parser.getTokenLocation());
        }
    }
    return new ShardSearchFailure(exception, new SearchShardTarget(nodeId, new ShardId(new Index(indexName, IndexMetaData.INDEX_UUID_NA_VALUE), shardId)));
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) SearchShardTarget(org.elasticsearch.search.SearchShardTarget) Index(org.elasticsearch.index.Index) ElasticsearchException(org.elasticsearch.ElasticsearchException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 99 with XContentParser

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

the class Setting method parseableStringToList.

private static List<String> parseableStringToList(String parsableString) {
    // EMPTY is safe here because we never call namedObject
    try (XContentParser xContentParser = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, parsableString)) {
        XContentParser.Token token = xContentParser.nextToken();
        if (token != XContentParser.Token.START_ARRAY) {
            throw new IllegalArgumentException("expected START_ARRAY but got " + token);
        }
        ArrayList<String> list = new ArrayList<>();
        while ((token = xContentParser.nextToken()) != XContentParser.Token.END_ARRAY) {
            if (token != XContentParser.Token.VALUE_STRING) {
                throw new IllegalArgumentException("expected VALUE_STRING but got " + token);
            }
            list.add(xContentParser.text());
        }
        return list;
    } catch (IOException e) {
        throw new IllegalArgumentException("failed to parse array", e);
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 100 with XContentParser

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

the class XContentSettingsLoader method load.

public Map<String, String> load(XContentParser jp) throws IOException {
    StringBuilder sb = new StringBuilder();
    Map<String, String> settings = new HashMap<>();
    List<String> path = new ArrayList<>();
    XContentParser.Token token = jp.nextToken();
    if (token == null) {
        return settings;
    }
    if (token != XContentParser.Token.START_OBJECT) {
        throw new ElasticsearchParseException("malformed, expected settings to start with 'object', instead was [{}]", token);
    }
    serializeObject(settings, sb, path, jp, null);
    // ensure we reached the end of the stream
    XContentParser.Token lastToken = null;
    try {
        while (!jp.isClosed() && (lastToken = jp.nextToken()) == null) ;
    } catch (Exception e) {
        throw new ElasticsearchParseException("malformed, expected end of settings but encountered additional content starting at line number: [{}], " + "column number: [{}]", e, jp.getTokenLocation().lineNumber, jp.getTokenLocation().columnNumber);
    }
    if (lastToken != null) {
        throw new ElasticsearchParseException("malformed, expected end of settings but encountered additional content starting at line number: [{}], " + "column number: [{}]", jp.getTokenLocation().lineNumber, jp.getTokenLocation().columnNumber);
    }
    return settings;
}
Also used : HashMap(java.util.HashMap) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) ArrayList(java.util.ArrayList) XContentParser(org.elasticsearch.common.xcontent.XContentParser) IOException(java.io.IOException) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException)

Aggregations

XContentParser (org.elasticsearch.common.xcontent.XContentParser)411 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)125 ParsingException (org.elasticsearch.common.ParsingException)109 IOException (java.io.IOException)69 BytesReference (org.elasticsearch.common.bytes.BytesReference)57 ArrayList (java.util.ArrayList)50 XContentType (org.elasticsearch.common.xcontent.XContentType)43 QueryParseContext (org.elasticsearch.index.query.QueryParseContext)43 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)42 HashMap (java.util.HashMap)28 Matchers.containsString (org.hamcrest.Matchers.containsString)23 Map (java.util.Map)22 List (java.util.List)17 Settings (org.elasticsearch.common.settings.Settings)17 ShardId (org.elasticsearch.index.shard.ShardId)16 Script (org.elasticsearch.script.Script)16 ToXContent (org.elasticsearch.common.xcontent.ToXContent)15 ElasticsearchException (org.elasticsearch.ElasticsearchException)14 NodeClient (org.elasticsearch.client.node.NodeClient)14 BaseRestHandler (org.elasticsearch.rest.BaseRestHandler)14