Search in sources :

Example 16 with Text

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.text.Text 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 17 with Text

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.text.Text in project elasticsearch by elastic.

the class SearchHitTests method testSerializeShardTarget.

public void testSerializeShardTarget() throws Exception {
    SearchShardTarget target = new SearchShardTarget("_node_id", new Index("_index", "_na_"), 0);
    Map<String, SearchHits> innerHits = new HashMap<>();
    SearchHit innerHit1 = new SearchHit(0, "_id", new Text("_type"), null);
    innerHit1.shard(target);
    SearchHit innerInnerHit2 = new SearchHit(0, "_id", new Text("_type"), null);
    innerInnerHit2.shard(target);
    innerHits.put("1", new SearchHits(new SearchHit[] { innerInnerHit2 }, 1, 1f));
    innerHit1.setInnerHits(innerHits);
    SearchHit innerHit2 = new SearchHit(0, "_id", new Text("_type"), null);
    innerHit2.shard(target);
    SearchHit innerHit3 = new SearchHit(0, "_id", new Text("_type"), null);
    innerHit3.shard(target);
    innerHits = new HashMap<>();
    SearchHit hit1 = new SearchHit(0, "_id", new Text("_type"), null);
    innerHits.put("1", new SearchHits(new SearchHit[] { innerHit1, innerHit2 }, 1, 1f));
    innerHits.put("2", new SearchHits(new SearchHit[] { innerHit3 }, 1, 1f));
    hit1.shard(target);
    hit1.setInnerHits(innerHits);
    SearchHit hit2 = new SearchHit(0, "_id", new Text("_type"), null);
    hit2.shard(target);
    SearchHits hits = new SearchHits(new SearchHit[] { hit1, hit2 }, 2, 1f);
    BytesStreamOutput output = new BytesStreamOutput();
    hits.writeTo(output);
    InputStream input = output.bytes().streamInput();
    SearchHits results = SearchHits.readSearchHits(new InputStreamStreamInput(input));
    assertThat(results.getAt(0).getShard(), equalTo(target));
    assertThat(results.getAt(0).getInnerHits().get("1").getAt(0).getShard(), notNullValue());
    assertThat(results.getAt(0).getInnerHits().get("1").getAt(0).getInnerHits().get("1").getAt(0).getShard(), notNullValue());
    assertThat(results.getAt(0).getInnerHits().get("1").getAt(1).getShard(), notNullValue());
    assertThat(results.getAt(0).getInnerHits().get("2").getAt(0).getShard(), notNullValue());
    assertThat(results.getAt(1).getShard(), equalTo(target));
}
Also used : HashMap(java.util.HashMap) InputStream(java.io.InputStream) Index(org.elasticsearch.index.Index) Text(org.elasticsearch.common.text.Text) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) InputStreamStreamInput(org.elasticsearch.common.io.stream.InputStreamStreamInput)

Example 18 with Text

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.text.Text in project elasticsearch by elastic.

the class SearchHitTests method testToXContent.

public void testToXContent() throws IOException {
    SearchHit searchHit = new SearchHit(1, "id1", new Text("type"), Collections.emptyMap());
    searchHit.score(1.5f);
    XContentBuilder builder = JsonXContent.contentBuilder();
    searchHit.toXContent(builder, ToXContent.EMPTY_PARAMS);
    assertEquals("{\"_type\":\"type\",\"_id\":\"id1\",\"_score\":1.5}", builder.string());
}
Also used : Text(org.elasticsearch.common.text.Text) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 19 with Text

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.text.Text in project elasticsearch by elastic.

the class SearchHitsTests method testToXContent.

public void testToXContent() throws IOException {
    SearchHit[] hits = new SearchHit[] { new SearchHit(1, "id1", new Text("type"), Collections.emptyMap()), new SearchHit(2, "id2", new Text("type"), Collections.emptyMap()) };
    long totalHits = 1000;
    float maxScore = 1.5f;
    SearchHits searchHits = new SearchHits(hits, totalHits, maxScore);
    XContentBuilder builder = JsonXContent.contentBuilder();
    builder.startObject();
    searchHits.toXContent(builder, ToXContent.EMPTY_PARAMS);
    builder.endObject();
    assertEquals("{\"hits\":{\"total\":1000,\"max_score\":1.5," + "\"hits\":[{\"_type\":\"type\",\"_id\":\"id1\",\"_score\":\"-Infinity\"}," + "{\"_type\":\"type\",\"_id\":\"id2\",\"_score\":\"-Infinity\"}]}}", builder.string());
}
Also used : Text(org.elasticsearch.common.text.Text) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 20 with Text

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.text.Text in project elasticsearch by elastic.

the class HighlightFieldTests method mutate.

private static HighlightField mutate(HighlightField original) {
    Text[] fragments = original.getFragments();
    if (randomBoolean()) {
        return new HighlightField(original.getName() + "_suffix", fragments);
    } else {
        if (fragments == null) {
            fragments = new Text[] { new Text("field") };
        } else {
            fragments = Arrays.copyOf(fragments, fragments.length + 1);
            fragments[fragments.length - 1] = new Text("something new");
        }
        return new HighlightField(original.getName(), fragments);
    }
}
Also used : Text(org.elasticsearch.common.text.Text)

Aggregations

Text (org.elasticsearch.common.text.Text)50 SearchHit (org.elasticsearch.search.SearchHit)13 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)12 Map (java.util.Map)10 SearchHits (org.elasticsearch.search.SearchHits)10 IOException (java.io.IOException)9 BytesReference (org.elasticsearch.common.bytes.BytesReference)9 BytesArray (org.elasticsearch.common.bytes.BytesArray)7 CompletionSuggestion (org.elasticsearch.search.suggest.completion.CompletionSuggestion)7 List (java.util.List)6 Test (org.junit.Test)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)5 SearchHitField (org.elasticsearch.search.SearchHitField)5 HighlightField (org.elasticsearch.search.fetch.subphase.highlight.HighlightField)5 Option (org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 BytesRef (org.apache.lucene.util.BytesRef)4 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)4 InternalSearchResponse (org.elasticsearch.search.internal.InternalSearchResponse)4