Search in sources :

Example 71 with XContentType

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

the class XContentMapValuesTests method testIncludingObjectWithNestedIncludedObject.

@SuppressWarnings({ "unchecked" })
public void testIncludingObjectWithNestedIncludedObject() throws Exception {
    XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("obj1").startObject("obj2").endObject().endObject().endObject();
    Tuple<XContentType, Map<String, Object>> mapTuple = XContentHelper.convertToMap(builder.bytes(), true, builder.contentType());
    Map<String, Object> filteredSource = XContentMapValues.filter(mapTuple.v2(), new String[] { "*.obj2" }, Strings.EMPTY_ARRAY);
    assertThat(filteredSource.size(), equalTo(1));
    assertThat(filteredSource, hasKey("obj1"));
    assertThat(((Map) filteredSource.get("obj1")).size(), equalTo(1));
    assertThat(((Map<String, Object>) filteredSource.get("obj1")), hasKey("obj2"));
    assertThat(((Map) ((Map) filteredSource.get("obj1")).get("obj2")).size(), equalTo(0));
}
Also used : XContentType(org.elasticsearch.common.xcontent.XContentType) Collections.emptyMap(java.util.Collections.emptyMap) HashMap(java.util.HashMap) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 72 with XContentType

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

the class XContentMapValuesTests method testThatFilterIncludesEmptyObjectWhenUsingIncludes.

public void testThatFilterIncludesEmptyObjectWhenUsingIncludes() throws Exception {
    XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("obj").endObject().endObject();
    Tuple<XContentType, Map<String, Object>> mapTuple = XContentHelper.convertToMap(builder.bytes(), true, builder.contentType());
    Map<String, Object> filteredSource = XContentMapValues.filter(mapTuple.v2(), new String[] { "obj" }, Strings.EMPTY_ARRAY);
    assertThat(mapTuple.v2(), equalTo(filteredSource));
}
Also used : XContentType(org.elasticsearch.common.xcontent.XContentType) Collections.emptyMap(java.util.Collections.emptyMap) HashMap(java.util.HashMap) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 73 with XContentType

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

the class TemplateQueryBuilderTests method testUnknownField.

/**
     * Override superclass test since template query doesn't support boost and queryName, so
     * we need to mutate other existing field in the test query.
     */
@Override
public void testUnknownField() throws IOException {
    TemplateQueryBuilder testQuery = createTestQueryBuilder();
    XContentType xContentType = randomFrom(XContentType.JSON, XContentType.YAML);
    String testQueryAsString = toXContent(testQuery, xContentType).string();
    String queryAsString = testQueryAsString.replace("inline", "bogusField");
    try {
        parseQuery(createParser(xContentType.xContent(), queryAsString));
        fail("IllegalArgumentException expected");
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("[script] unknown field [bogusField], parser not found"));
    }
}
Also used : XContentType(org.elasticsearch.common.xcontent.XContentType) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 74 with XContentType

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

the class UpdateRequestTests method testToAndFromXContent.

public void testToAndFromXContent() throws IOException {
    UpdateRequest updateRequest = new UpdateRequest();
    updateRequest.detectNoop(randomBoolean());
    if (randomBoolean()) {
        XContentType xContentType = randomFrom(XContentType.values());
        BytesReference source = RandomObjects.randomSource(random(), xContentType);
        updateRequest.doc(new IndexRequest().source(source, xContentType));
        updateRequest.docAsUpsert(randomBoolean());
    } else {
        ScriptType scriptType = randomFrom(ScriptType.values());
        String scriptLang = (scriptType != ScriptType.STORED) ? randomAsciiOfLength(10) : null;
        String scriptIdOrCode = randomAsciiOfLength(10);
        int nbScriptParams = randomIntBetween(0, 5);
        Map<String, Object> scriptParams = new HashMap<>(nbScriptParams);
        for (int i = 0; i < nbScriptParams; i++) {
            scriptParams.put(randomAsciiOfLength(5), randomAsciiOfLength(5));
        }
        updateRequest.script(new Script(scriptType, scriptLang, scriptIdOrCode, scriptParams));
        updateRequest.scriptedUpsert(randomBoolean());
    }
    if (randomBoolean()) {
        XContentType xContentType = randomFrom(XContentType.values());
        BytesReference source = RandomObjects.randomSource(random(), xContentType);
        updateRequest.upsert(new IndexRequest().source(source, xContentType));
    }
    if (randomBoolean()) {
        String[] fields = new String[randomIntBetween(0, 5)];
        for (int i = 0; i < fields.length; i++) {
            fields[i] = randomAsciiOfLength(5);
        }
        updateRequest.fields(fields);
    }
    if (randomBoolean()) {
        if (randomBoolean()) {
            updateRequest.fetchSource(randomBoolean());
        } else {
            String[] includes = new String[randomIntBetween(0, 5)];
            for (int i = 0; i < includes.length; i++) {
                includes[i] = randomAsciiOfLength(5);
            }
            String[] excludes = new String[randomIntBetween(0, 5)];
            for (int i = 0; i < excludes.length; i++) {
                excludes[i] = randomAsciiOfLength(5);
            }
            if (randomBoolean()) {
                updateRequest.fetchSource(includes, excludes);
            }
        }
    }
    XContentType xContentType = randomFrom(XContentType.values());
    boolean humanReadable = randomBoolean();
    BytesReference originalBytes = XContentHelper.toXContent(updateRequest, xContentType, humanReadable);
    if (randomBoolean()) {
        try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
            originalBytes = shuffleXContent(parser, randomBoolean()).bytes();
        }
    }
    UpdateRequest parsedUpdateRequest = new UpdateRequest();
    try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
        parsedUpdateRequest.fromXContent(parser);
        assertNull(parser.nextToken());
    }
    assertEquals(updateRequest.detectNoop(), parsedUpdateRequest.detectNoop());
    assertEquals(updateRequest.docAsUpsert(), parsedUpdateRequest.docAsUpsert());
    assertEquals(updateRequest.docAsUpsert(), parsedUpdateRequest.docAsUpsert());
    assertEquals(updateRequest.script(), parsedUpdateRequest.script());
    assertEquals(updateRequest.scriptedUpsert(), parsedUpdateRequest.scriptedUpsert());
    assertArrayEquals(updateRequest.fields(), parsedUpdateRequest.fields());
    assertEquals(updateRequest.fetchSource(), parsedUpdateRequest.fetchSource());
    BytesReference finalBytes = toXContent(parsedUpdateRequest, xContentType, humanReadable);
    assertToXContentEquivalent(originalBytes, finalBytes, xContentType);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) ScriptType(org.elasticsearch.script.ScriptType) Script(org.elasticsearch.script.Script) HashMap(java.util.HashMap) IndexRequest(org.elasticsearch.action.index.IndexRequest) XContentType(org.elasticsearch.common.xcontent.XContentType) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 75 with XContentType

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

the class UpdateResponseTests method testToAndFromXContent.

public void testToAndFromXContent() throws IOException {
    final XContentType xContentType = randomFrom(XContentType.values());
    final Tuple<UpdateResponse, UpdateResponse> tuple = randomUpdateResponse(xContentType);
    UpdateResponse updateResponse = tuple.v1();
    UpdateResponse expectedUpdateResponse = tuple.v2();
    boolean humanReadable = randomBoolean();
    BytesReference updateResponseBytes = toXContent(updateResponse, xContentType, humanReadable);
    // Shuffle the XContent fields
    if (randomBoolean()) {
        try (XContentParser parser = createParser(xContentType.xContent(), updateResponseBytes)) {
            updateResponseBytes = shuffleXContent(parser, randomBoolean()).bytes();
        }
    }
    // Parse the XContent bytes to obtain a parsed UpdateResponse
    UpdateResponse parsedUpdateResponse;
    try (XContentParser parser = createParser(xContentType.xContent(), updateResponseBytes)) {
        parsedUpdateResponse = UpdateResponse.fromXContent(parser);
        assertNull(parser.nextToken());
    }
    // We can't use equals() to compare the original and the parsed delete response
    // because the random delete response can contain shard failures with exceptions,
    // and those exceptions are not parsed back with the same types.
    assertUpdateResponse(expectedUpdateResponse, parsedUpdateResponse);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

XContentType (org.elasticsearch.common.xcontent.XContentType)86 BytesReference (org.elasticsearch.common.bytes.BytesReference)50 XContentParser (org.elasticsearch.common.xcontent.XContentParser)45 Map (java.util.Map)20 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)20 HashMap (java.util.HashMap)14 IndexRequest (org.elasticsearch.action.index.IndexRequest)11 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)10 IOException (java.io.IOException)9 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)8 ArrayList (java.util.ArrayList)6 Collections.emptyMap (java.util.Collections.emptyMap)6 List (java.util.List)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