use of org.elasticsearch.search.SearchHit.NestedIdentity in project elasticsearch by elastic.
the class SearchHitTests method createTestItem.
public static SearchHit createTestItem(boolean withOptionalInnerHits) {
int internalId = randomInt();
String uid = randomAsciiOfLength(10);
Text type = new Text(randomAsciiOfLengthBetween(5, 10));
NestedIdentity nestedIdentity = null;
if (randomBoolean()) {
nestedIdentity = NestedIdentityTests.createTestItem(randomIntBetween(0, 2));
}
Map<String, SearchHitField> fields = new HashMap<>();
if (randomBoolean()) {
int size = randomIntBetween(0, 10);
for (int i = 0; i < size; i++) {
Tuple<List<Object>, List<Object>> values = RandomObjects.randomStoredFieldValues(random(), XContentType.JSON);
if (randomBoolean()) {
String metaField = randomFrom(META_FIELDS);
fields.put(metaField, new SearchHitField(metaField, values.v1()));
} else {
String fieldName = randomAsciiOfLengthBetween(5, 10);
fields.put(fieldName, new SearchHitField(fieldName, values.v1()));
}
}
}
SearchHit hit = new SearchHit(internalId, uid, type, nestedIdentity, fields);
if (frequently()) {
if (rarely()) {
hit.score(Float.NaN);
} else {
hit.score(randomFloat());
}
}
if (frequently()) {
hit.sourceRef(RandomObjects.randomSource(random()));
}
if (randomBoolean()) {
hit.version(randomLong());
}
if (randomBoolean()) {
hit.sortValues(SearchSortValuesTests.createTestItem());
}
if (randomBoolean()) {
int size = randomIntBetween(0, 5);
Map<String, HighlightField> highlightFields = new HashMap<>(size);
for (int i = 0; i < size; i++) {
highlightFields.put(randomAsciiOfLength(5), HighlightFieldTests.createTestItem());
}
hit.highlightFields(highlightFields);
}
if (randomBoolean()) {
int size = randomIntBetween(0, 5);
String[] matchedQueries = new String[size];
for (int i = 0; i < size; i++) {
matchedQueries[i] = randomAsciiOfLength(5);
}
hit.matchedQueries(matchedQueries);
}
if (randomBoolean()) {
hit.explanation(createExplanation(randomIntBetween(0, 5)));
}
if (withOptionalInnerHits) {
int innerHitsSize = randomIntBetween(0, 3);
Map<String, SearchHits> innerHits = new HashMap<>(innerHitsSize);
for (int i = 0; i < innerHitsSize; i++) {
innerHits.put(randomAsciiOfLength(5), SearchHitsTests.createTestItem());
}
hit.setInnerHits(innerHits);
}
if (randomBoolean()) {
hit.shard(new SearchShardTarget(randomAsciiOfLengthBetween(5, 10), new ShardId(new Index(randomAsciiOfLengthBetween(5, 10), randomAsciiOfLengthBetween(5, 10)), randomInt())));
}
return hit;
}
use of org.elasticsearch.search.SearchHit.NestedIdentity in project elasticsearch by elastic.
the class NestedIdentityTests method createTestItem.
public static NestedIdentity createTestItem(int depth) {
String field = frequently() ? randomAsciiOfLengthBetween(1, 20) : randomRealisticUnicodeOfCodepointLengthBetween(1, 20);
int offset = randomInt(10);
NestedIdentity child = null;
if (depth > 0) {
child = createTestItem(depth - 1);
}
return new NestedIdentity(field, offset, child);
}
use of org.elasticsearch.search.SearchHit.NestedIdentity in project elasticsearch by elastic.
the class NestedIdentityTests method testToXContent.
public void testToXContent() throws IOException {
NestedIdentity nestedIdentity = new NestedIdentity("foo", 5, null);
XContentBuilder builder = JsonXContent.contentBuilder();
builder.prettyPrint();
builder.startObject();
nestedIdentity.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
assertEquals("{\n" + " \"_nested\" : {\n" + " \"field\" : \"foo\",\n" + " \"offset\" : 5\n" + " }\n" + "}", builder.string());
nestedIdentity = new NestedIdentity("foo", 5, new NestedIdentity("bar", 3, null));
builder = JsonXContent.contentBuilder();
builder.prettyPrint();
builder.startObject();
nestedIdentity.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
assertEquals("{\n" + " \"_nested\" : {\n" + " \"field\" : \"foo\",\n" + " \"offset\" : 5,\n" + " \"_nested\" : {\n" + " \"field\" : \"bar\",\n" + " \"offset\" : 3\n" + " }\n" + " }\n" + "}", builder.string());
}
use of org.elasticsearch.search.SearchHit.NestedIdentity in project elasticsearch by elastic.
the class NestedIdentityTests method mutate.
private static NestedIdentity mutate(NestedIdentity original) {
if (original == null) {
return createTestItem(0);
}
List<Supplier<NestedIdentity>> mutations = new ArrayList<>();
int offset = original.getOffset();
NestedIdentity child = (NestedIdentity) original.getChild();
String fieldName = original.getField().string();
mutations.add(() -> new NestedIdentity(original.getField().string() + "_prefix", offset, child));
mutations.add(() -> new NestedIdentity(fieldName, offset + 1, child));
mutations.add(() -> new NestedIdentity(fieldName, offset, mutate(child)));
return randomFrom(mutations).get();
}
use of org.elasticsearch.search.SearchHit.NestedIdentity in project elasticsearch by elastic.
the class NestedIdentityTests method testFromXContent.
public void testFromXContent() throws IOException {
NestedIdentity nestedIdentity = createTestItem(randomInt(3));
XContentType xcontentType = randomFrom(XContentType.values());
XContentBuilder builder = XContentFactory.contentBuilder(xcontentType);
if (randomBoolean()) {
builder.prettyPrint();
}
builder = nestedIdentity.innerToXContent(builder, ToXContent.EMPTY_PARAMS);
XContentParser parser = createParser(builder);
NestedIdentity parsedNestedIdentity = NestedIdentity.fromXContent(parser);
assertEquals(nestedIdentity, parsedNestedIdentity);
assertNull(parser.nextToken());
}
Aggregations