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);
}
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[] {}));
}
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());
}
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());
}
}
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());
}
Aggregations