Search in sources :

Example 6 with ToXContent

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

the class NodeInfoStreamingTests method compareJsonOutput.

private void compareJsonOutput(ToXContent param1, ToXContent param2) throws IOException {
    if (param1 == null) {
        assertNull(param2);
        return;
    }
    ToXContent.Params params = ToXContent.EMPTY_PARAMS;
    XContentBuilder param1Builder = jsonBuilder();
    param1Builder.startObject();
    param1.toXContent(param1Builder, params);
    param1Builder.endObject();
    XContentBuilder param2Builder = jsonBuilder();
    param2Builder.startObject();
    param2.toXContent(param2Builder, params);
    param2Builder.endObject();
    assertThat(param1Builder.string(), equalTo(param2Builder.string()));
}
Also used : ToXContent(org.elasticsearch.common.xcontent.ToXContent) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 7 with ToXContent

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

the class RestClusterRerouteAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    ClusterRerouteRequest clusterRerouteRequest = createRequest(request);
    // by default, return everything but metadata
    final String metric = request.param("metric");
    if (metric == null) {
        request.params().put("metric", DEFAULT_METRICS);
    }
    return channel -> client.admin().cluster().reroute(clusterRerouteRequest, new AcknowledgedRestListener<ClusterRerouteResponse>(channel) {

        @Override
        protected void addCustomFields(XContentBuilder builder, ClusterRerouteResponse response) throws IOException {
            builder.startObject("state");
            settingsFilter.addFilterSettingParams(request);
            response.getState().toXContent(builder, request);
            builder.endObject();
            if (clusterRerouteRequest.explain()) {
                assert response.getExplanations() != null;
                response.getExplanations().toXContent(builder, ToXContent.EMPTY_PARAMS);
            }
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) SettingsFilter(org.elasticsearch.common.settings.SettingsFilter) ClusterRerouteRequest(org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteRequest) ToXContent(org.elasticsearch.common.xcontent.ToXContent) Set(java.util.Set) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) ClusterRerouteResponse(org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteResponse) Strings(org.elasticsearch.common.Strings) HashSet(java.util.HashSet) ClusterState(org.elasticsearch.cluster.ClusterState) Settings(org.elasticsearch.common.settings.Settings) AllocationCommands(org.elasticsearch.cluster.routing.allocation.command.AllocationCommands) ValueType(org.elasticsearch.common.xcontent.ObjectParser.ValueType) ObjectParser(org.elasticsearch.common.xcontent.ObjectParser) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) Requests(org.elasticsearch.client.Requests) ParseField(org.elasticsearch.common.ParseField) Collections(java.util.Collections) EnumSet(java.util.EnumSet) AcknowledgedRestListener(org.elasticsearch.rest.action.AcknowledgedRestListener) ClusterRerouteRequest(org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteRequest) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ClusterRerouteResponse(org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteResponse)

Example 8 with ToXContent

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

the class ContextCompletionSuggestSearchIT method testMultiContextBoosting.

@AwaitsFix(bugUrl = "multiple context boosting is broken, as a suggestion, contexts pair is treated as (num(context) entries)")
public void testMultiContextBoosting() throws Exception {
    LinkedHashMap<String, ContextMapping> map = new LinkedHashMap<>();
    map.put("cat", ContextBuilder.category("cat").field("cat").build());
    map.put("type", ContextBuilder.category("type").field("type").build());
    final CompletionMappingBuilder mapping = new CompletionMappingBuilder().context(map);
    createIndexAndMapping(mapping);
    int numDocs = 10;
    List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
    for (int i = 0; i < numDocs; i++) {
        XContentBuilder source = jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", i + 1).endObject().field("cat", "cat" + i % 2).field("type", "type" + i % 4).endObject();
        indexRequestBuilders.add(client().prepareIndex(INDEX, TYPE, "" + i).setSource(source));
    }
    indexRandom(true, indexRequestBuilders);
    // boost only on context cat
    CompletionSuggestionBuilder catBoostSuggest = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg");
    catBoostSuggest.contexts(Collections.singletonMap("cat", Arrays.asList(CategoryQueryContext.builder().setCategory("cat0").setBoost(3).build(), CategoryQueryContext.builder().setCategory("cat1").build())));
    assertSuggestions("foo", catBoostSuggest, "suggestion8", "suggestion6", "suggestion4", "suggestion9", "suggestion2");
    // boost only on context type
    CompletionSuggestionBuilder typeBoostSuggest = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg");
    typeBoostSuggest.contexts(Collections.singletonMap("type", Arrays.asList(CategoryQueryContext.builder().setCategory("type2").setBoost(2).build(), CategoryQueryContext.builder().setCategory("type1").setBoost(4).build())));
    assertSuggestions("foo", typeBoostSuggest, "suggestion9", "suggestion5", "suggestion6", "suggestion1", "suggestion2");
    // boost on both contexts
    CompletionSuggestionBuilder multiContextBoostSuggest = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg");
    // query context order should never matter
    Map<String, List<? extends ToXContent>> contextMap = new HashMap<>();
    contextMap.put("type", Arrays.asList(CategoryQueryContext.builder().setCategory("type2").setBoost(2).build(), CategoryQueryContext.builder().setCategory("type1").setBoost(4).build()));
    contextMap.put("cat", Arrays.asList(CategoryQueryContext.builder().setCategory("cat0").setBoost(3).build(), CategoryQueryContext.builder().setCategory("cat1").build()));
    multiContextBoostSuggest.contexts(contextMap);
    assertSuggestions("foo", multiContextBoostSuggest, "suggestion9", "suggestion6", "suggestion5", "suggestion2", "suggestion1");
}
Also used : ToXContent(org.elasticsearch.common.xcontent.ToXContent) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) GeoPoint(org.elasticsearch.common.geo.GeoPoint) LinkedHashMap(java.util.LinkedHashMap) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder) GeoContextMapping(org.elasticsearch.search.suggest.completion.context.GeoContextMapping) ContextMapping(org.elasticsearch.search.suggest.completion.context.ContextMapping) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping) CompletionMappingBuilder(org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder) ArrayList(java.util.ArrayList) List(java.util.List) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 9 with ToXContent

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

the class ContextCompletionSuggestSearchIT method testMultiContextFiltering.

public void testMultiContextFiltering() throws Exception {
    LinkedHashMap<String, ContextMapping> map = new LinkedHashMap<>();
    map.put("cat", ContextBuilder.category("cat").field("cat").build());
    map.put("type", ContextBuilder.category("type").field("type").build());
    final CompletionMappingBuilder mapping = new CompletionMappingBuilder().context(map);
    createIndexAndMapping(mapping);
    int numDocs = 10;
    List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
    for (int i = 0; i < numDocs; i++) {
        XContentBuilder source = jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", i + 1).endObject().field("cat", "cat" + i % 2).field("type", "type" + i % 4).endObject();
        indexRequestBuilders.add(client().prepareIndex(INDEX, TYPE, "" + i).setSource(source));
    }
    indexRandom(true, indexRequestBuilders);
    // filter only on context cat
    CompletionSuggestionBuilder catFilterSuggest = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg");
    catFilterSuggest.contexts(Collections.singletonMap("cat", Collections.singletonList(CategoryQueryContext.builder().setCategory("cat0").build())));
    assertSuggestions("foo", catFilterSuggest, "suggestion8", "suggestion6", "suggestion4", "suggestion2", "suggestion0");
    // filter only on context type
    CompletionSuggestionBuilder typeFilterSuggest = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg");
    typeFilterSuggest.contexts(Collections.singletonMap("type", Arrays.asList(CategoryQueryContext.builder().setCategory("type2").build(), CategoryQueryContext.builder().setCategory("type1").build())));
    assertSuggestions("foo", typeFilterSuggest, "suggestion9", "suggestion6", "suggestion5", "suggestion2", "suggestion1");
    CompletionSuggestionBuilder multiContextFilterSuggest = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg");
    // query context order should never matter
    Map<String, List<? extends ToXContent>> contextMap = new HashMap<>();
    contextMap.put("type", Collections.singletonList(CategoryQueryContext.builder().setCategory("type2").build()));
    contextMap.put("cat", Collections.singletonList(CategoryQueryContext.builder().setCategory("cat2").build()));
    multiContextFilterSuggest.contexts(contextMap);
    assertSuggestions("foo", multiContextFilterSuggest, "suggestion6", "suggestion2");
}
Also used : ToXContent(org.elasticsearch.common.xcontent.ToXContent) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) GeoPoint(org.elasticsearch.common.geo.GeoPoint) LinkedHashMap(java.util.LinkedHashMap) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder) GeoContextMapping(org.elasticsearch.search.suggest.completion.context.GeoContextMapping) ContextMapping(org.elasticsearch.search.suggest.completion.context.ContextMapping) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping) CompletionMappingBuilder(org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder) ArrayList(java.util.ArrayList) List(java.util.List) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 10 with ToXContent

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

the class StringsTests method testToStringToXContent.

public void testToStringToXContent() {
    final ToXContent toXContent;
    final boolean error;
    if (randomBoolean()) {
        if (randomBoolean()) {
            error = false;
            toXContent = (builder, params) -> builder.field("ok", "here").field("catastrophe", "");
        } else {
            error = true;
            toXContent = (builder, params) -> builder.startObject().field("ok", "here").field("catastrophe", "").endObject();
        }
    } else {
        if (randomBoolean()) {
            error = false;
            toXContent = (ToXContentObject) (builder, params) -> builder.startObject().field("ok", "here").field("catastrophe", "").endObject();
        } else {
            error = true;
            toXContent = (ToXContentObject) (builder, params) -> builder.field("ok", "here").field("catastrophe", "");
        }
    }
    String toString = Strings.toString(toXContent);
    if (error) {
        assertThat(toString, containsString("Error building toString out of XContent"));
    } else {
        assertThat(toString, containsString("\"ok\":\"here\""));
        assertThat(toString, containsString("\"catastrophe\":\"\""));
    }
}
Also used : ToXContent(org.elasticsearch.common.xcontent.ToXContent) ToXContentObject(org.elasticsearch.common.xcontent.ToXContentObject) ToXContent(org.elasticsearch.common.xcontent.ToXContent) ESTestCase(org.elasticsearch.test.ESTestCase) Matchers.containsString(org.hamcrest.Matchers.containsString) Sets(org.elasticsearch.common.util.set.Sets) Matchers.containsString(org.hamcrest.Matchers.containsString)

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