Search in sources :

Example 41 with Text

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

the class HighlightFieldTests method createTestItem.

public static HighlightField createTestItem() {
    String name = frequently() ? randomAsciiOfLengthBetween(5, 20) : randomRealisticUnicodeOfCodepointLengthBetween(5, 20);
    Text[] fragments = null;
    if (frequently()) {
        int size = randomIntBetween(0, 5);
        fragments = new Text[size];
        for (int i = 0; i < size; i++) {
            fragments[i] = new Text(frequently() ? randomAsciiOfLengthBetween(10, 30) : randomRealisticUnicodeOfCodepointLengthBetween(10, 30));
        }
    }
    return new HighlightField(name, fragments);
}
Also used : Text(org.elasticsearch.common.text.Text)

Example 42 with Text

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

the class CustomHighlighter method highlight.

@Override
public HighlightField highlight(HighlighterContext highlighterContext) {
    SearchContextHighlight.Field field = highlighterContext.field;
    CacheEntry cacheEntry = (CacheEntry) highlighterContext.hitContext.cache().get("test-custom");
    final int docId = highlighterContext.hitContext.readerContext().docBase + highlighterContext.hitContext.docId();
    if (cacheEntry == null) {
        cacheEntry = new CacheEntry();
        highlighterContext.hitContext.cache().put("test-custom", cacheEntry);
        cacheEntry.docId = docId;
        cacheEntry.position = 1;
    } else {
        if (cacheEntry.docId == docId) {
            cacheEntry.position++;
        } else {
            cacheEntry.docId = docId;
            cacheEntry.position = 1;
        }
    }
    List<Text> responses = new ArrayList<>();
    responses.add(new Text(String.format(Locale.ENGLISH, "standard response for %s at position %s", field.field(), cacheEntry.position)));
    if (field.fieldOptions().options() != null) {
        for (Map.Entry<String, Object> entry : field.fieldOptions().options().entrySet()) {
            responses.add(new Text("field:" + entry.getKey() + ":" + entry.getValue()));
        }
    }
    return new HighlightField(highlighterContext.fieldName, responses.toArray(new Text[] {}));
}
Also used : ArrayList(java.util.ArrayList) SearchContextHighlight(org.elasticsearch.search.fetch.subphase.highlight.SearchContextHighlight) Text(org.elasticsearch.common.text.Text) HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) Map(java.util.Map)

Example 43 with Text

use of 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 44 with Text

use of 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 45 with Text

use of 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)

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)5 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 Entry (org.elasticsearch.search.suggest.Suggest.Suggestion.Entry)4