Search in sources :

Example 1 with NestedIdentity

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;
}
Also used : HashMap(java.util.HashMap) Text(org.elasticsearch.common.text.Text) HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) Index(org.elasticsearch.index.Index) ShardId(org.elasticsearch.index.shard.ShardId) ArrayList(java.util.ArrayList) List(java.util.List) NestedIdentity(org.elasticsearch.search.SearchHit.NestedIdentity)

Example 2 with NestedIdentity

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);
}
Also used : NestedIdentity(org.elasticsearch.search.SearchHit.NestedIdentity)

Example 3 with NestedIdentity

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());
}
Also used : NestedIdentity(org.elasticsearch.search.SearchHit.NestedIdentity) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 4 with NestedIdentity

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();
}
Also used : ArrayList(java.util.ArrayList) Supplier(java.util.function.Supplier) NestedIdentity(org.elasticsearch.search.SearchHit.NestedIdentity)

Example 5 with NestedIdentity

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());
}
Also used : XContentType(org.elasticsearch.common.xcontent.XContentType) NestedIdentity(org.elasticsearch.search.SearchHit.NestedIdentity) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

NestedIdentity (org.elasticsearch.search.SearchHit.NestedIdentity)6 ArrayList (java.util.ArrayList)2 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Supplier (java.util.function.Supplier)1 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)1 StreamInput (org.elasticsearch.common.io.stream.StreamInput)1 Text (org.elasticsearch.common.text.Text)1 XContentParser (org.elasticsearch.common.xcontent.XContentParser)1 XContentType (org.elasticsearch.common.xcontent.XContentType)1 Index (org.elasticsearch.index.Index)1 ShardId (org.elasticsearch.index.shard.ShardId)1 HighlightField (org.elasticsearch.search.fetch.subphase.highlight.HighlightField)1