Skip to content

Commit a16b080

Browse files
committed
Runtime field: test currentTimeMillis
Adds a test for `runtime_script` who's script contains `currentTimeMillis`.
1 parent 8ffa4d9 commit a16b080

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/mapper/ScriptDateMappedFieldTypeTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.apache.lucene.index.SortedNumericDocValues;
1414
import org.apache.lucene.search.Collector;
1515
import org.apache.lucene.search.Explanation;
16+
import org.apache.lucene.search.FieldDoc;
1617
import org.apache.lucene.search.IndexSearcher;
1718
import org.apache.lucene.search.LeafCollector;
1819
import org.apache.lucene.search.MatchAllDocsQuery;
@@ -152,6 +153,9 @@ public void testSort() throws IOException {
152153
assertThat(readSource(reader, docs.scoreDocs[0].doc), equalTo("{\"timestamp\": [1595432181351]}"));
153154
assertThat(readSource(reader, docs.scoreDocs[1].doc), equalTo("{\"timestamp\": [1595432181354]}"));
154155
assertThat(readSource(reader, docs.scoreDocs[2].doc), equalTo("{\"timestamp\": [1595432181356]}"));
156+
assertThat((Long) (((FieldDoc) docs.scoreDocs[0]).fields[0]), equalTo(1595432181351L));
157+
assertThat((Long) (((FieldDoc) docs.scoreDocs[1]).fields[0]), equalTo(1595432181354L));
158+
assertThat((Long) (((FieldDoc) docs.scoreDocs[2]).fields[0]), equalTo(1595432181356L));
155159
}
156160
}
157161
}

x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/mapper/ScriptLongMappedFieldTypeTests.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.apache.lucene.index.RandomIndexWriter;
1313
import org.apache.lucene.index.SortedNumericDocValues;
1414
import org.apache.lucene.search.Collector;
15+
import org.apache.lucene.search.FieldDoc;
1516
import org.apache.lucene.search.IndexSearcher;
1617
import org.apache.lucene.search.LeafCollector;
1718
import org.apache.lucene.search.MatchAllDocsQuery;
@@ -52,6 +53,7 @@
5253

5354
import static java.util.Collections.emptyMap;
5455
import static org.hamcrest.Matchers.equalTo;
56+
import static org.hamcrest.Matchers.greaterThan;
5557

5658
public class ScriptLongMappedFieldTypeTests extends AbstractNonTextScriptMappedFieldTypeTestCase {
5759
public void testFormat() throws IOException {
@@ -117,6 +119,30 @@ public void testSort() throws IOException {
117119
}
118120
}
119121

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+
120146
@Override
121147
public void testUsedInScript() throws IOException {
122148
try (Directory directory = newDirectory(); RandomIndexWriter iw = new RandomIndexWriter(random(), directory)) {
@@ -290,6 +316,17 @@ public void execute() {
290316
}
291317
}
292318
};
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+
};
293330
default:
294331
throw new IllegalArgumentException("unsupported script [" + code + "]");
295332
}

x-pack/plugin/src/test/resources/rest-api-spec/test/runtime_fields/20_long.yml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ setup:
4949
t /= 10;
5050
}
5151
}
52+
# Test now
53+
millis_ago:
54+
type: runtime_script
55+
runtime_type: date
56+
script:
57+
source: |
58+
for (def dt : doc['timestamp']) {
59+
emitValue(System.currentTimeMillis() - dt.toInstant().toEpochMilli());
60+
}
5261
5362
- do:
5463
bulk:
@@ -90,7 +99,12 @@ setup:
9099
index: sensor
91100
body:
92101
sort: timestamp
93-
docvalue_fields: [voltage_times_ten, voltage_times_ten_from_source, temperature_digits]
102+
docvalue_fields:
103+
- voltage_times_ten
104+
- voltage_times_ten_from_source
105+
- temperature_digits
106+
- field: millis_ago
107+
format: epoch_millis
94108
- match: {hits.total.value: 6}
95109
- match: {hits.hits.0.fields.voltage_times_ten: [40] }
96110
- match: {hits.hits.0.fields.voltage_times_ten_from_source: [40] }
@@ -101,6 +115,8 @@ setup:
101115
- match: {hits.hits.3.fields.voltage_times_ten: [51] }
102116
- match: {hits.hits.4.fields.voltage_times_ten: [58] }
103117
- match: {hits.hits.5.fields.voltage_times_ten: [52] }
118+
# We can't check the value because it is constantly increasing. If `gt` worked on strings we could do it, but it doens't.
119+
- is_true: hits.hits.0.fields.millis_ago.0
104120

105121
---
106122
"terms agg":
@@ -223,3 +239,4 @@ setup:
223239
- match: { aggregations.to-users.users.hits.hits.2._index: test }
224240
- match: { aggregations.to-users.users.hits.hits.2._nested.field: users }
225241
- match: { aggregations.to-users.users.hits.hits.2._nested.offset: 1 }
242+

0 commit comments

Comments
 (0)