|
12 | 12 | import org.apache.lucene.index.RandomIndexWriter; |
13 | 13 | import org.apache.lucene.index.SortedNumericDocValues; |
14 | 14 | import org.apache.lucene.search.Collector; |
| 15 | +import org.apache.lucene.search.FieldDoc; |
15 | 16 | import org.apache.lucene.search.IndexSearcher; |
16 | 17 | import org.apache.lucene.search.LeafCollector; |
17 | 18 | import org.apache.lucene.search.MatchAllDocsQuery; |
|
52 | 53 |
|
53 | 54 | import static java.util.Collections.emptyMap; |
54 | 55 | import static org.hamcrest.Matchers.equalTo; |
| 56 | +import static org.hamcrest.Matchers.greaterThan; |
55 | 57 |
|
56 | 58 | public class ScriptLongMappedFieldTypeTests extends AbstractNonTextScriptMappedFieldTypeTestCase { |
57 | 59 | public void testFormat() throws IOException { |
@@ -117,6 +119,30 @@ public void testSort() throws IOException { |
117 | 119 | } |
118 | 120 | } |
119 | 121 |
|
| 122 | + public void testNow() throws IOException { |
| 123 | + try (Directory directory = newDirectory(); RandomIndexWriter iw = new RandomIndexWriter(random(), directory)) { |
| 124 | + iw.addDocument(List.of(new StoredField("_source", new BytesRef("{\"timestamp\": [1595432181354]}")))); |
| 125 | + iw.addDocument(List.of(new StoredField("_source", new BytesRef("{\"timestamp\": [1595432181351]}")))); |
| 126 | + iw.addDocument(List.of(new StoredField("_source", new BytesRef("{\"timestamp\": [1595432181356]}")))); |
| 127 | + try (DirectoryReader reader = iw.getReader()) { |
| 128 | + IndexSearcher searcher = newSearcher(reader); |
| 129 | + ScriptLongFieldData ifd = build("millis_ago", Map.of()).fielddataBuilder("test", mockContext()::lookup) |
| 130 | + .build(null, null, null); |
| 131 | + SortField sf = ifd.sortField(null, MultiValueMode.MIN, null, false); |
| 132 | + TopFieldDocs docs = searcher.search(new MatchAllDocsQuery(), 3, new Sort(sf)); |
| 133 | + assertThat(readSource(reader, docs.scoreDocs[0].doc), equalTo("{\"timestamp\": [1595432181356]}")); |
| 134 | + assertThat(readSource(reader, docs.scoreDocs[1].doc), equalTo("{\"timestamp\": [1595432181354]}")); |
| 135 | + assertThat(readSource(reader, docs.scoreDocs[2].doc), equalTo("{\"timestamp\": [1595432181351]}")); |
| 136 | + long t1 = (Long) (((FieldDoc) docs.scoreDocs[0]).fields[0]); |
| 137 | + assertThat(t1, greaterThan(3638011399L)); |
| 138 | + long t2 = (Long) (((FieldDoc) docs.scoreDocs[1]).fields[0]); |
| 139 | + long t3 = (Long) (((FieldDoc) docs.scoreDocs[2]).fields[0]); |
| 140 | + assertThat(t2, equalTo(t1 + 2)); |
| 141 | + assertThat(t3, equalTo(t1 + 5)); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
120 | 146 | @Override |
121 | 147 | public void testUsedInScript() throws IOException { |
122 | 148 | try (Directory directory = newDirectory(); RandomIndexWriter iw = new RandomIndexWriter(random(), directory)) { |
@@ -290,6 +316,17 @@ public void execute() { |
290 | 316 | } |
291 | 317 | } |
292 | 318 | }; |
| 319 | + case "millis_ago": |
| 320 | + // Painless actually call System.currentTimeMillis. We could mock the time but this works fine too. |
| 321 | + long now = System.currentTimeMillis(); |
| 322 | + return (params, lookup) -> (ctx) -> new LongScriptFieldScript(params, lookup, ctx) { |
| 323 | + @Override |
| 324 | + public void execute() { |
| 325 | + for (Object timestamp : (List<?>) getSource().get("timestamp")) { |
| 326 | + emitValue(now - ((Number) timestamp).longValue()); |
| 327 | + } |
| 328 | + } |
| 329 | + }; |
293 | 330 | default: |
294 | 331 | throw new IllegalArgumentException("unsupported script [" + code + "]"); |
295 | 332 | } |
|
0 commit comments