Search in sources :

Example 46 with Text

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

the class BaseXContentTestCase method testText.

public void testText() throws Exception {
    assertResult("{'text':null}", () -> builder().startObject().field("text", (Text) null).endObject());
    assertResult("{'text':''}", () -> builder().startObject().field("text", new Text("")).endObject());
    assertResult("{'text':'foo bar'}", () -> builder().startObject().field("text", new Text("foo bar")).endObject());
    final BytesReference random = new BytesArray(randomBytes());
    XContentBuilder builder = builder().startObject().field("text", new Text(random)).endObject();
    XContentParser parser = createParser(xcontentType().xContent(), builder.bytes());
    assertSame(parser.nextToken(), Token.START_OBJECT);
    assertSame(parser.nextToken(), Token.FIELD_NAME);
    assertEquals(parser.currentName(), "text");
    assertTrue(parser.nextToken().isValue());
    assertThat(parser.utf8Bytes().utf8ToString(), equalTo(random.utf8ToString()));
    assertSame(parser.nextToken(), Token.END_OBJECT);
    assertNull(parser.nextToken());
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) BytesArray(org.elasticsearch.common.bytes.BytesArray) Text(org.elasticsearch.common.text.Text)

Example 47 with Text

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

the class BaseXContentTestCase method testObjects.

public void testObjects() throws Exception {
    Map<String, Object[]> objects = new HashMap<>();
    objects.put("{'objects':[false,true,false]}", new Object[] { false, true, false });
    objects.put("{'objects':[1,1,2,3,5,8,13]}", new Object[] { (byte) 1, (byte) 1, (byte) 2, (byte) 3, (byte) 5, (byte) 8, (byte) 13 });
    objects.put("{'objects':[1.0,1.0,2.0,3.0,5.0,8.0,13.0]}", new Object[] { 1.0d, 1.0d, 2.0d, 3.0d, 5.0d, 8.0d, 13.0d });
    objects.put("{'objects':[1.0,1.0,2.0,3.0,5.0,8.0,13.0]}", new Object[] { 1.0f, 1.0f, 2.0f, 3.0f, 5.0f, 8.0f, 13.0f });
    objects.put("{'objects':[{'lat':45.759429931640625,'lon':4.8394775390625}]}", new Object[] { GeoPoint.fromGeohash("u05kq4k") });
    objects.put("{'objects':[1,1,2,3,5,8,13]}", new Object[] { 1, 1, 2, 3, 5, 8, 13 });
    objects.put("{'objects':[1,1,2,3,5,8,13]}", new Object[] { 1L, 1L, 2L, 3L, 5L, 8L, 13L });
    objects.put("{'objects':[1,1,2,3,5,8]}", new Object[] { (short) 1, (short) 1, (short) 2, (short) 3, (short) 5, (short) 8 });
    objects.put("{'objects':['a','b','c']}", new Object[] { "a", "b", "c" });
    objects.put("{'objects':['a','b','c']}", new Object[] { new Text("a"), new Text(new BytesArray("b")), new Text("c") });
    objects.put("{'objects':null}", null);
    objects.put("{'objects':[null,null,null]}", new Object[] { null, null, null });
    objects.put("{'objects':['OPEN','CLOSE']}", IndexMetaData.State.values());
    objects.put("{'objects':[{'f1':'v1'},{'f2':'v2'}]}", new Object[] { singletonMap("f1", "v1"), singletonMap("f2", "v2") });
    objects.put("{'objects':[[1,2,3],[4,5]]}", new Object[] { Arrays.asList(1, 2, 3), Arrays.asList(4, 5) });
    final String paths = Constants.WINDOWS ? "{'objects':['a\\\\b\\\\c','d\\\\e']}" : "{'objects':['a/b/c','d/e']}";
    objects.put(paths, new Object[] { PathUtils.get("a", "b", "c"), PathUtils.get("d", "e") });
    final DateTimeFormatter formatter = XContentBuilder.DEFAULT_DATE_PRINTER;
    final Date d1 = new DateTime(2016, 1, 1, 0, 0, DateTimeZone.UTC).toDate();
    final Date d2 = new DateTime(2015, 1, 1, 0, 0, DateTimeZone.UTC).toDate();
    objects.put("{'objects':['" + formatter.print(d1.getTime()) + "','" + formatter.print(d2.getTime()) + "']}", new Object[] { d1, d2 });
    final DateTime dt1 = DateTime.now();
    final DateTime dt2 = new DateTime(2016, 12, 25, 7, 59, 42, 213, DateTimeZone.UTC);
    objects.put("{'objects':['" + formatter.print(dt1) + "','2016-12-25T07:59:42.213Z']}", new Object[] { dt1, dt2 });
    final Calendar c1 = new DateTime(2012, 7, 7, 10, 23, DateTimeZone.UTC).toCalendar(Locale.ROOT);
    final Calendar c2 = new DateTime(2014, 11, 16, 19, 36, DateTimeZone.UTC).toCalendar(Locale.ROOT);
    objects.put("{'objects':['2012-07-07T10:23:00.000Z','2014-11-16T19:36:00.000Z']}", new Object[] { c1, c2 });
    final ToXContent x1 = (builder, params) -> builder.startObject().field("f1", "v1").field("f2", 2).array("f3", 3, 4, 5).endObject();
    final ToXContent x2 = (builder, params) -> builder.startObject().field("f1", "v1").field("f2", x1).endObject();
    objects.put("{'objects':[{'f1':'v1','f2':2,'f3':[3,4,5]},{'f1':'v1','f2':{'f1':'v1','f2':2,'f3':[3,4,5]}}]}", new Object[] { x1, x2 });
    for (Map.Entry<String, Object[]> o : objects.entrySet()) {
        final String expected = o.getKey();
        assertResult(expected, () -> builder().startObject().field("objects", o.getValue()).endObject());
        assertResult(expected, () -> builder().startObject().field("objects").value(o.getValue()).endObject());
        assertResult(expected, () -> builder().startObject().field("objects").values(o.getValue()).endObject());
        assertResult(expected, () -> builder().startObject().array("objects", o.getValue()).endObject());
    }
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) DateTimeZone(org.joda.time.DateTimeZone) Arrays(java.util.Arrays) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) Date(java.util.Date) HashMap(java.util.HashMap) Strings(org.elasticsearch.common.Strings) ArrayList(java.util.ArrayList) BytesArray(org.elasticsearch.common.bytes.BytesArray) Calendar(java.util.Calendar) ByteArrayInputStream(java.io.ByteArrayInputStream) Text(org.elasticsearch.common.text.Text) Locale(java.util.Locale) Map(java.util.Map) GeoPoint(org.elasticsearch.common.geo.GeoPoint) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) BigInteger(java.math.BigInteger) Collections.singletonMap(java.util.Collections.singletonMap) ESTestCase(org.elasticsearch.test.ESTestCase) ParseField(org.elasticsearch.common.ParseField) JsonParseException(com.fasterxml.jackson.core.JsonParseException) Path(java.nio.file.Path) PathUtils(org.elasticsearch.common.io.PathUtils) ISODateTimeFormat(org.joda.time.format.ISODateTimeFormat) Collections.emptyMap(java.util.Collections.emptyMap) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) BytesRef(org.apache.lucene.util.BytesRef) Matchers.allOf(org.hamcrest.Matchers.allOf) DateTime(org.joda.time.DateTime) Matchers(org.hamcrest.Matchers) ReadableInstant(org.joda.time.ReadableInstant) IOException(java.io.IOException) BytesReference(org.elasticsearch.common.bytes.BytesReference) Matchers.startsWith(org.hamcrest.Matchers.startsWith) TimeUnit(java.util.concurrent.TimeUnit) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) Constants(org.apache.lucene.util.Constants) DistanceUnit(org.elasticsearch.common.unit.DistanceUnit) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) Token(org.elasticsearch.common.xcontent.XContentParser.Token) Matcher(org.hamcrest.Matcher) Instant(org.joda.time.Instant) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) BytesArray(org.elasticsearch.common.bytes.BytesArray) HashMap(java.util.HashMap) Calendar(java.util.Calendar) Text(org.elasticsearch.common.text.Text) Matchers.containsString(org.hamcrest.Matchers.containsString) Date(java.util.Date) DateTime(org.joda.time.DateTime) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) HashMap(java.util.HashMap) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) Collections.emptyMap(java.util.Collections.emptyMap)

Example 48 with Text

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

the class SearchHitTests method testNullSource.

public void testNullSource() throws Exception {
    SearchHit searchHit = new SearchHit(0, "_id", new Text("_type"), null);
    assertThat(searchHit.getSourceAsMap(), nullValue());
    assertThat(searchHit.getSourceRef(), nullValue());
    assertThat(searchHit.getSourceAsMap(), nullValue());
    assertThat(searchHit.getSourceAsString(), nullValue());
    assertThat(searchHit.getSourceAsMap(), nullValue());
    assertThat(searchHit.getSourceRef(), nullValue());
    assertThat(searchHit.getSourceAsString(), nullValue());
}
Also used : Text(org.elasticsearch.common.text.Text)

Example 49 with Text

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

the class InternalTopHitsTests method createTestInstance.

@Override
protected InternalTopHits createTestInstance(String name, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
    int from = 0;
    int requestedSize = between(1, 40);
    int actualSize = between(0, requestedSize);
    float maxScore = Float.MIN_VALUE;
    ScoreDoc[] scoreDocs = new ScoreDoc[actualSize];
    SearchHit[] hits = new SearchHit[actualSize];
    Set<Integer> usedDocIds = new HashSet<>();
    for (int i = 0; i < actualSize; i++) {
        float score = randomFloat();
        maxScore = max(maxScore, score);
        int docId = randomValueOtherThanMany(usedDocIds::contains, () -> between(0, IndexWriter.MAX_DOCS));
        usedDocIds.add(docId);
        Map<String, SearchHitField> searchHitFields = new HashMap<>();
        if (testInstancesLookSortedByField) {
            Object[] fields = new Object[testInstancesSortFields.length];
            for (int f = 0; f < testInstancesSortFields.length; f++) {
                fields[f] = randomOfType(testInstancesSortFields[f].getType());
            }
            scoreDocs[i] = new FieldDoc(docId, score, fields);
        } else {
            scoreDocs[i] = new ScoreDoc(docId, score);
        }
        hits[i] = new SearchHit(docId, Integer.toString(i), new Text("test"), searchHitFields);
        hits[i].score(score);
    }
    int totalHits = between(actualSize, 500000);
    SearchHits searchHits = new SearchHits(hits, totalHits, maxScore);
    TopDocs topDocs;
    Arrays.sort(scoreDocs, scoreDocComparator());
    if (testInstancesLookSortedByField) {
        topDocs = new TopFieldDocs(totalHits, scoreDocs, testInstancesSortFields, maxScore);
    } else {
        topDocs = new TopDocs(totalHits, scoreDocs, maxScore);
    }
    return new InternalTopHits(name, from, requestedSize, topDocs, searchHits, pipelineAggregators, metaData);
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) FieldDoc(org.apache.lucene.search.FieldDoc) HashMap(java.util.HashMap) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) Text(org.elasticsearch.common.text.Text) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) SearchHitField(org.elasticsearch.search.SearchHitField) SearchHits(org.elasticsearch.search.SearchHits) HashSet(java.util.HashSet)

Example 50 with Text

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

the class ElasticSourcePTest method givenMultipleResults_when_runProcessor_then_useScrollIdInFollowupScrollRequest.

@Test
public void givenMultipleResults_when_runProcessor_then_useScrollIdInFollowupScrollRequest() throws Exception {
    SearchHit hit = new SearchHit(0, "id-0", new Text("ignored"), emptyMap(), emptyMap());
    hit.sourceRef(new BytesArray(HIT_SOURCE));
    when(response.getHits()).thenReturn(new SearchHits(new SearchHit[] { hit }, new TotalHits(3, EQUAL_TO), Float.NaN));
    SearchResponse response2 = mock(SearchResponse.class);
    SearchHit hit2 = new SearchHit(1, "id-1", new Text("ignored"), emptyMap(), emptyMap());
    hit2.sourceRef(new BytesArray(HIT_SOURCE2));
    when(response2.getHits()).thenReturn(new SearchHits(new SearchHit[] { hit2 }, new TotalHits(3, EQUAL_TO), Float.NaN));
    SearchResponse response3 = mock(SearchResponse.class);
    when(response3.getHits()).thenReturn(new SearchHits(new SearchHit[] {}, new TotalHits(3, EQUAL_TO), Float.NaN));
    when(mockClient.scroll(any(), any())).thenReturn(response2, response3);
    TestSupport testSupport = runProcessor();
    testSupport.expectOutput(newArrayList(HIT_SOURCE, HIT_SOURCE2));
    ArgumentCaptor<SearchScrollRequest> captor = forClass(SearchScrollRequest.class);
    verify(mockClient, times(2)).scroll(captor.capture(), any());
    SearchScrollRequest request = captor.getValue();
    assertThat(request.scrollId()).isEqualTo(SCROLL_ID);
    assertThat(request.scroll().keepAlive().getStringRep()).isEqualTo(KEEP_ALIVE);
}
Also used : TotalHits(org.apache.lucene.search.TotalHits) BytesArray(org.elasticsearch.common.bytes.BytesArray) SearchHit(org.elasticsearch.search.SearchHit) TestSupport(com.hazelcast.jet.core.test.TestSupport) Text(org.elasticsearch.common.text.Text) SearchHits(org.elasticsearch.search.SearchHits) SearchScrollRequest(org.elasticsearch.action.search.SearchScrollRequest) SearchResponse(org.elasticsearch.action.search.SearchResponse) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

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