use of org.elasticsearch.common.xcontent.XContentBuilder in project elasticsearch by elastic.
the class GetTermVectorsIT method testExistingFieldButNotInDocNPE.
public void testExistingFieldButNotInDocNPE() throws Exception {
XContentBuilder mapping = jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("existingfield").field("type", "text").field("term_vector", "with_positions_offsets_payloads").endObject().endObject().endObject().endObject();
assertAcked(prepareCreate("test").addAlias(new Alias("alias")).addMapping("type1", mapping));
// when indexing a field that simply has a question mark, the term vectors will be null
client().prepareIndex("test", "type1", "0").setSource("anotherexistingfield", 1).execute().actionGet();
refresh();
ActionFuture<TermVectorsResponse> termVectors = client().termVectors(new TermVectorsRequest(indexOrAlias(), "type1", "0").selectedFields(randomBoolean() ? new String[] { "existingfield" } : null).termStatistics(true).fieldStatistics(true));
// lets see if the null term vectors are caught...
TermVectorsResponse actionGet = termVectors.actionGet();
assertThat(actionGet, notNullValue());
assertThat(actionGet.isExists(), equalTo(true));
assertThat(actionGet.getIndex(), equalTo("test"));
assertThat(actionGet.getFields().terms("existingfield"), nullValue());
}
use of org.elasticsearch.common.xcontent.XContentBuilder in project elasticsearch by elastic.
the class GetTermVectorsIT method testPerFieldAnalyzer.
public void testPerFieldAnalyzer() throws IOException {
int numFields = 25;
// setup mapping and document source
Set<String> withTermVectors = new HashSet<>();
XContentBuilder mapping = jsonBuilder().startObject().startObject("type1").startObject("properties");
XContentBuilder source = jsonBuilder().startObject();
for (int i = 0; i < numFields; i++) {
String fieldName = "field" + i;
if (randomBoolean()) {
withTermVectors.add(fieldName);
}
mapping.startObject(fieldName).field("type", "text").field("term_vector", withTermVectors.contains(fieldName) ? "yes" : "no").endObject();
source.field(fieldName, "some text here");
}
source.endObject();
mapping.endObject().endObject().endObject();
// setup indices with mapping
Settings.Builder settings = Settings.builder().put(indexSettings()).put("index.analysis.analyzer", "standard");
assertAcked(prepareCreate("test").addAlias(new Alias("alias")).setSettings(settings).addMapping("type1", mapping));
ensureGreen();
// index a single document with prepared source
client().prepareIndex("test", "type1", "0").setSource(source).get();
refresh();
// create random per_field_analyzer and selected fields
Map<String, String> perFieldAnalyzer = new HashMap<>();
Set<String> selectedFields = new HashSet<>();
for (int i = 0; i < numFields; i++) {
if (randomBoolean()) {
perFieldAnalyzer.put("field" + i, "keyword");
}
if (randomBoolean()) {
perFieldAnalyzer.put("non_existing" + i, "keyword");
}
if (randomBoolean()) {
selectedFields.add("field" + i);
}
if (randomBoolean()) {
selectedFields.add("non_existing" + i);
}
}
// selected fields not specified
TermVectorsResponse response = client().prepareTermVectors(indexOrAlias(), "type1", "0").setPerFieldAnalyzer(perFieldAnalyzer).get();
// should return all fields that have terms vectors, some with overridden analyzer
checkAnalyzedFields(response.getFields(), withTermVectors, perFieldAnalyzer);
// selected fields specified including some not in the mapping
response = client().prepareTermVectors(indexOrAlias(), "type1", "0").setSelectedFields(selectedFields.toArray(Strings.EMPTY_ARRAY)).setPerFieldAnalyzer(perFieldAnalyzer).get();
// should return only the specified valid fields, with some with overridden analyzer
checkAnalyzedFields(response.getFields(), selectedFields, perFieldAnalyzer);
}
use of org.elasticsearch.common.xcontent.XContentBuilder in project elasticsearch by elastic.
the class MetaDataTests method testXContentWithIndexGraveyard.
public void testXContentWithIndexGraveyard() throws IOException {
final IndexGraveyard graveyard = IndexGraveyardTests.createRandom();
final MetaData originalMeta = MetaData.builder().indexGraveyard(graveyard).build();
final XContentBuilder builder = JsonXContent.contentBuilder();
builder.startObject();
originalMeta.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes());
final MetaData fromXContentMeta = MetaData.fromXContent(parser);
assertThat(fromXContentMeta.indexGraveyard(), equalTo(originalMeta.indexGraveyard()));
}
use of org.elasticsearch.common.xcontent.XContentBuilder in project elasticsearch by elastic.
the class BytesRestResponseTests method testNoErrorFromXContent.
public void testNoErrorFromXContent() throws IOException {
IllegalStateException e = expectThrows(IllegalStateException.class, () -> {
try (XContentBuilder builder = XContentBuilder.builder(randomFrom(XContentType.values()).xContent())) {
builder.startObject();
builder.field("status", randomFrom(RestStatus.values()).getStatus());
builder.endObject();
try (XContentParser parser = createParser(builder.contentType().xContent(), builder.bytes())) {
BytesRestResponse.errorFromXContent(parser);
}
}
});
assertEquals("Failed to parse elasticsearch status exception: no exception was found", e.getMessage());
}
use of org.elasticsearch.common.xcontent.XContentBuilder in project elasticsearch by elastic.
the class RepositoryDataTests method testXContent.
public void testXContent() throws IOException {
RepositoryData repositoryData = generateRandomRepoData();
XContentBuilder builder = JsonXContent.contentBuilder();
repositoryData.snapshotsToXContent(builder, ToXContent.EMPTY_PARAMS);
XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes());
long gen = (long) randomIntBetween(0, 500);
RepositoryData fromXContent = RepositoryData.snapshotsFromXContent(parser, gen);
assertEquals(repositoryData, fromXContent);
assertEquals(gen, fromXContent.getGenId());
}
Aggregations