Search in sources :

Example 96 with XContentBuilder

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

the class CategoryContextMappingTests method testQueryContextParsingMixed.

public void testQueryContextParsingMixed() throws Exception {
    XContentBuilder builder = jsonBuilder().startArray().startObject().field("context", "context1").field("boost", 2).field("prefix", true).endObject().value("context2").endArray();
    XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes());
    CategoryContextMapping mapping = ContextBuilder.category("cat").build();
    List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(createParseContext(parser));
    assertThat(internalQueryContexts.size(), equalTo(2));
    assertThat(internalQueryContexts.get(0).context, equalTo("context1"));
    assertThat(internalQueryContexts.get(0).boost, equalTo(2));
    assertThat(internalQueryContexts.get(0).isPrefix, equalTo(true));
    assertThat(internalQueryContexts.get(1).context, equalTo("context2"));
    assertThat(internalQueryContexts.get(1).boost, equalTo(1));
    assertThat(internalQueryContexts.get(1).isPrefix, equalTo(false));
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping)

Example 97 with XContentBuilder

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

the class AbstractSuggestionBuilderTestCase method testFromXContent.

/**
     * creates random suggestion builder, renders it to xContent and back to new
     * instance that should be equal to original
     */
public void testFromXContent() throws IOException {
    for (int runs = 0; runs < NUMBER_OF_TESTBUILDERS; runs++) {
        SB suggestionBuilder = randomTestBuilder();
        XContentBuilder xContentBuilder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
        if (randomBoolean()) {
            xContentBuilder.prettyPrint();
        }
        xContentBuilder.startObject();
        suggestionBuilder.toXContent(xContentBuilder, ToXContent.EMPTY_PARAMS);
        xContentBuilder.endObject();
        XContentBuilder shuffled = shuffleXContent(xContentBuilder, shuffleProtectedFields());
        XContentParser parser = createParser(shuffled);
        // we need to skip the start object and the name, those will be parsed by outer SuggestBuilder
        parser.nextToken();
        SuggestionBuilder<?> secondSuggestionBuilder = SuggestionBuilder.fromXContent(parser);
        assertNotSame(suggestionBuilder, secondSuggestionBuilder);
        assertEquals(suggestionBuilder, secondSuggestionBuilder);
        assertEquals(suggestionBuilder.hashCode(), secondSuggestionBuilder.hashCode());
    }
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 98 with XContentBuilder

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

the class CompletionSuggestSearchIT method createIndexAndMappingAndSettings.

private void createIndexAndMappingAndSettings(Settings settings, CompletionMappingBuilder completionMappingBuilder) throws IOException {
    XContentBuilder mapping = jsonBuilder().startObject().startObject(TYPE).startObject("properties").startObject("test_field").field("type", "keyword").endObject().startObject("title").field("type", "keyword").endObject().startObject(FIELD).field("type", "completion").field("analyzer", completionMappingBuilder.indexAnalyzer).field("search_analyzer", completionMappingBuilder.searchAnalyzer).field("preserve_separators", completionMappingBuilder.preserveSeparators).field("preserve_position_increments", completionMappingBuilder.preservePositionIncrements);
    if (completionMappingBuilder.contextMappings != null) {
        mapping = mapping.startArray("contexts");
        for (Map.Entry<String, ContextMapping> contextMapping : completionMappingBuilder.contextMappings.entrySet()) {
            mapping = mapping.startObject().field("name", contextMapping.getValue().name()).field("type", contextMapping.getValue().type().name());
            switch(contextMapping.getValue().type()) {
                case CATEGORY:
                    mapping = mapping.field("path", ((CategoryContextMapping) contextMapping.getValue()).getFieldName());
                    break;
                case GEO:
                    mapping = mapping.field("path", ((GeoContextMapping) contextMapping.getValue()).getFieldName()).field("precision", ((GeoContextMapping) contextMapping.getValue()).getPrecision());
                    break;
            }
            mapping = mapping.endObject();
        }
        mapping = mapping.endArray();
    }
    mapping = mapping.endObject().endObject().endObject().endObject();
    assertAcked(client().admin().indices().prepareCreate(INDEX).setSettings(Settings.builder().put(indexSettings()).put(settings)).addMapping(TYPE, mapping).get());
}
Also used : GeoContextMapping(org.elasticsearch.search.suggest.completion.context.GeoContextMapping) ContextMapping(org.elasticsearch.search.suggest.completion.context.ContextMapping) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping) GeoContextMapping(org.elasticsearch.search.suggest.completion.context.GeoContextMapping) Matchers.containsString(org.hamcrest.Matchers.containsString) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping)

Example 99 with XContentBuilder

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

the class SortBuilderTests method testRandomSortBuilders.

/**
     * test random syntax variations
     */
public void testRandomSortBuilders() throws IOException {
    for (int runs = 0; runs < NUMBER_OF_RUNS; runs++) {
        List<SortBuilder<?>> testBuilders = randomSortBuilderList();
        XContentBuilder xContentBuilder = XContentFactory.jsonBuilder();
        xContentBuilder.startObject();
        if (testBuilders.size() > 1) {
            xContentBuilder.startArray("sort");
        } else {
            xContentBuilder.field("sort");
        }
        for (SortBuilder<?> builder : testBuilders) {
            if (builder instanceof ScoreSortBuilder || builder instanceof FieldSortBuilder) {
                switch(randomIntBetween(0, 2)) {
                    case 0:
                        if (builder instanceof ScoreSortBuilder) {
                            xContentBuilder.value("_score");
                        } else {
                            xContentBuilder.value(((FieldSortBuilder) builder).getFieldName());
                        }
                        break;
                    case 1:
                        xContentBuilder.startObject();
                        if (builder instanceof ScoreSortBuilder) {
                            xContentBuilder.field("_score");
                        } else {
                            xContentBuilder.field(((FieldSortBuilder) builder).getFieldName());
                        }
                        xContentBuilder.value(builder.order());
                        xContentBuilder.endObject();
                        break;
                    case 2:
                        builder.toXContent(xContentBuilder, ToXContent.EMPTY_PARAMS);
                        break;
                }
            } else {
                builder.toXContent(xContentBuilder, ToXContent.EMPTY_PARAMS);
            }
        }
        if (testBuilders.size() > 1) {
            xContentBuilder.endArray();
        }
        xContentBuilder.endObject();
        List<SortBuilder<?>> parsedSort = parseSort(xContentBuilder.string());
        assertEquals(testBuilders.size(), parsedSort.size());
        Iterator<SortBuilder<?>> iterator = testBuilders.iterator();
        for (SortBuilder<?> parsedBuilder : parsedSort) {
            assertEquals(iterator.next(), parsedBuilder);
        }
    }
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 100 with XContentBuilder

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

the class GeoDistanceSortBuilderTests method testGeoDistanceSortDeprecatedSortModeException.

public void testGeoDistanceSortDeprecatedSortModeException() throws Exception {
    XContentBuilder sortBuilder = jsonBuilder();
    sortBuilder.startObject();
    sortBuilder.startArray("location");
    sortBuilder.startArray().value(1.2).value(3).endArray().startArray().value(5).value(6).endArray();
    sortBuilder.endArray();
    sortBuilder.field("order", "desc");
    sortBuilder.field("unit", "km");
    sortBuilder.field("sort_mode", "max");
    sortBuilder.endObject();
    parse(sortBuilder);
    assertWarnings("Deprecated field [sort_mode] used, expected [mode] instead");
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Aggregations

XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)618 IOException (java.io.IOException)126 XContentParser (org.elasticsearch.common.xcontent.XContentParser)122 Settings (org.elasticsearch.common.settings.Settings)60 ArrayList (java.util.ArrayList)59 SearchResponse (org.elasticsearch.action.search.SearchResponse)56 Matchers.containsString (org.hamcrest.Matchers.containsString)53 HashMap (java.util.HashMap)47 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)43 Map (java.util.Map)41 RestRequest (org.elasticsearch.rest.RestRequest)37 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)36 BytesRestResponse (org.elasticsearch.rest.BytesRestResponse)35 Test (org.junit.Test)34 RestController (org.elasticsearch.rest.RestController)33 NodeClient (org.elasticsearch.client.node.NodeClient)32 BaseRestHandler (org.elasticsearch.rest.BaseRestHandler)32 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)32 GeoPoint (org.elasticsearch.common.geo.GeoPoint)31 CrateUnitTest (io.crate.test.integration.CrateUnitTest)28