Search in sources :

Example 11 with ToXContent

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

the class CompletionSuggestionBuilder method contexts.

/**
     * Sets query contexts for completion
     * @param queryContexts named query contexts
     *                      see {@link org.elasticsearch.search.suggest.completion.context.CategoryQueryContext}
     *                      and {@link org.elasticsearch.search.suggest.completion.context.GeoQueryContext}
     */
public CompletionSuggestionBuilder contexts(Map<String, List<? extends ToXContent>> queryContexts) {
    Objects.requireNonNull(queryContexts, "contexts must not be null");
    try {
        XContentBuilder contentBuilder = XContentFactory.jsonBuilder();
        contentBuilder.startObject();
        for (Map.Entry<String, List<? extends ToXContent>> contextEntry : queryContexts.entrySet()) {
            contentBuilder.startArray(contextEntry.getKey());
            for (ToXContent queryContext : contextEntry.getValue()) {
                queryContext.toXContent(contentBuilder, EMPTY_PARAMS);
            }
            contentBuilder.endArray();
        }
        contentBuilder.endObject();
        return contexts(contentBuilder);
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }
}
Also used : ToXContent(org.elasticsearch.common.xcontent.ToXContent) List(java.util.List) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 12 with ToXContent

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

the class XContentHelperTests method testToXContent.

public void testToXContent() throws IOException {
    final XContentType xContentType = randomFrom(XContentType.values());
    final ToXContent toXContent;
    final boolean error;
    if (randomBoolean()) {
        if (randomBoolean()) {
            error = false;
            toXContent = (builder, params) -> builder.field("field", "value");
        } else {
            error = true;
            toXContent = (builder, params) -> builder.startObject().field("field", "value").endObject();
        }
    } else {
        if (randomBoolean()) {
            error = false;
            toXContent = (ToXContentObject) (builder, params) -> builder.startObject().field("field", "value").endObject();
        } else {
            error = true;
            toXContent = (ToXContentObject) (builder, params) -> builder.field("field", "value");
        }
    }
    if (error) {
        expectThrows(IOException.class, () -> XContentHelper.toXContent(toXContent, xContentType, randomBoolean()));
    } else {
        BytesReference bytes = XContentHelper.toXContent(toXContent, xContentType, randomBoolean());
        try (XContentParser parser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, bytes)) {
            assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
            assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
            assertTrue(parser.nextToken().isValue());
            assertEquals("value", parser.text());
            assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
            assertNull(parser.nextToken());
        }
    }
}
Also used : ToXContent(org.elasticsearch.common.xcontent.ToXContent) Arrays(java.util.Arrays) XContentType(org.elasticsearch.common.xcontent.XContentType) ToXContent(org.elasticsearch.common.xcontent.ToXContent) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) HashMap(java.util.HashMap) BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentHelper(org.elasticsearch.common.xcontent.XContentHelper) XContentParser(org.elasticsearch.common.xcontent.XContentParser) List(java.util.List) Map(java.util.Map) ToXContentObject(org.elasticsearch.common.xcontent.ToXContentObject) ESTestCase(org.elasticsearch.test.ESTestCase) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 13 with ToXContent

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

the class DynamicMappingTests method serialize.

private String serialize(ToXContent mapper) throws Exception {
    XContentBuilder builder = XContentFactory.jsonBuilder().startObject();
    mapper.toXContent(builder, new ToXContent.MapParams(emptyMap()));
    return builder.endObject().string();
}
Also used : ToXContent(org.elasticsearch.common.xcontent.ToXContent) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 14 with ToXContent

use of org.elasticsearch.common.xcontent.ToXContent in project crate by crate.

the class GetIndexTemplatesResponse method toXContent.

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    params = new ToXContent.DelegatingMapParams(singletonMap("reduce_mappings", "true"), params);
    builder.startObject();
    for (IndexTemplateMetadata indexTemplateMetadata : getIndexTemplates()) {
        IndexTemplateMetadata.Builder.toXContent(indexTemplateMetadata, builder, params);
    }
    builder.endObject();
    return builder;
}
Also used : ToXContent(org.elasticsearch.common.xcontent.ToXContent) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata)

Example 15 with ToXContent

use of org.elasticsearch.common.xcontent.ToXContent in project crate by crate.

the class Mapping method toString.

@Override
public String toString() {
    try {
        XContentBuilder builder = XContentFactory.jsonBuilder().startObject();
        toXContent(builder, new ToXContent.MapParams(emptyMap()));
        return Strings.toString(builder.endObject());
    } catch (IOException bogus) {
        throw new UncheckedIOException(bogus);
    }
}
Also used : ToXContent(org.elasticsearch.common.xcontent.ToXContent) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Aggregations

ToXContent (org.elasticsearch.common.xcontent.ToXContent)15 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)9 IOException (java.io.IOException)6 HashMap (java.util.HashMap)4 List (java.util.List)4 BytesReference (org.elasticsearch.common.bytes.BytesReference)3 XContentParser (org.elasticsearch.common.xcontent.XContentParser)3 UncheckedIOException (java.io.UncheckedIOException)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)2 NodeClient (org.elasticsearch.client.node.NodeClient)2 GeoPoint (org.elasticsearch.common.geo.GeoPoint)2 Settings (org.elasticsearch.common.settings.Settings)2 ToXContentObject (org.elasticsearch.common.xcontent.ToXContentObject)2 XContentType (org.elasticsearch.common.xcontent.XContentType)2 CompletionMappingBuilder (org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder)2 Suggestion (org.elasticsearch.search.suggest.Suggest.Suggestion)2 CompletionSuggestion (org.elasticsearch.search.suggest.completion.CompletionSuggestion)2