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));
}
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());
}
}
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());
}
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);
}
}
}
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");
}
Aggregations