Skip to content

Commit

Permalink
[7.x] Script: Fields API converter tests (#76900) (#76981)
Browse files Browse the repository at this point in the history
  • Loading branch information
stu-elastic committed Aug 26, 2021
1 parent 137bad2 commit 4b2600c
Show file tree
Hide file tree
Showing 4 changed files with 461 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,40 @@ setup:
- match: { hits.hits.1._id: d1 }
- match: { hits.hits.2._id: d2 }

---
"script fields api for dates":
- do:
indices.create:
index: test
body:
settings:
number_of_shards: 2
mappings:
properties:
dt:
type: date_nanos
- do:
index:
index: test
id: d1
body: {"dt": "2021-08-24T18:45:52.123456789Z" }
- do:
indices.refresh: {}
- do:
search:
rest_total_hits_as_int: true
index: test
body:
query: { "match_all": {} }
sort: [ { dt: asc } ]
script_fields:
date_field:
script:
source: "field('dt').getLong(100L)"
- match: { hits.total: 1 }
- match: { hits.hits.0._id: d1 }
- match: { hits.hits.0.fields.date_field.0: 1629830752123456789 }

---
"script score fields api":
- do:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.elasticsearch.common.geo.GeoUtils;
import org.elasticsearch.common.time.DateUtils;
import org.elasticsearch.geometry.utils.Geohash;
import org.elasticsearch.script.Converters;
import org.elasticsearch.script.Field;
import org.elasticsearch.script.FieldValues;
import org.elasticsearch.script.InvalidConversion;
Expand All @@ -25,7 +26,6 @@
import java.io.IOException;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.util.AbstractList;
import java.util.Arrays;
import java.util.Comparator;
Expand Down Expand Up @@ -240,11 +240,10 @@ void refreshArray() throws IOException {
@Override
public long getLongValue() {
throwIfEmpty();
Instant dt = dates[0].toInstant();
if (isNanos) {
return ChronoUnit.NANOS.between(java.time.Instant.EPOCH, dt);
return Converters.convertDateNanosToLong(dates[0]);
}
return dt.toEpochMilli();
return Converters.convertDateMillisToLong(dates[0]);
}

@Override
Expand Down Expand Up @@ -587,13 +586,13 @@ private static boolean[] grow(boolean[] array, int minSize) {
@Override
public long getLongValue() {
throwIfEmpty();
return values[0] ? 1L : 0L;
return Converters.convertBooleanToLong(values[0]);
}

@Override
public double getDoubleValue() {
throwIfEmpty();
return values[0] ? 1.0D : 0.0D;
return Converters.convertBooleanToDouble(values[0]);
}

@Override
Expand Down Expand Up @@ -675,12 +674,12 @@ public final String getValue() {

@Override
public long getLongValue() {
return Long.parseLong(get(0));
return Converters.convertStringToLong(get(0));
}

@Override
public double getDoubleValue() {
return Double.parseDouble(get(0));
return Converters.convertStringToDouble(get(0));
}

@Override
Expand Down

0 comments on commit 4b2600c

Please sign in to comment.