After reviewing the painless source (thanks for the link @nik9000 ) I put together the following reference for emit()
.
emit()
Type | Function | Multiple Calls |
---|---|---|
boolean |
emit(boolean v) |
NO |
date |
emit(long v) |
NO |
double |
emit(double v) |
YES |
geo_point |
emit(double lat, double lon) |
NO |
ip |
emit(String v) |
YES |
keyword |
emit(String v) |
YES |
long |
emit(long v) |
YES |
Emitting geo_point
values
For most runtime field types document fields of the same type can be used directly as parameters. For example:
emit(doc['host.name'].value)
However for geo_point fields, emit doesn't accept a geo_point
object, rather latitude and longitude must be passed as two separate parameters. For example:
def geopoint = doc['location.coordinates'].value;
def lat = geopoint.lat;
def lon = geopoint.lon;
emit(lat, lon)
Calling emit()
multiple times in a script
For some field types emit() may be called multiple times in a script. Doing so will result in an array of values up to the maximum number of values allowed. The table above indicates which values support multiple calls.
The following is an example of how this is enforced by the Painless language, is as follows - note the call to checkMaxSize()
:
public final void emit(long v) {
checkMaxSize(count);
if (values.length < count + 1) {
values = ArrayUtil.grow(values, count + 1);
}
values[count++] = v;
}
NOTE! The maximum number of values that can be emitted is 100. This is constant and cannot be overridden (
public static final int MAX_VALUES = 100;
).
String values are not limited by the number of values, rather by the number of characters. This is enforced by the Painless language as follows - note the check for chars > MAX_CHARS
:
public final void emit(String v) {
checkMaxSize(results.size());
chars += v.length();
if (chars > MAX_CHARS) {
throw new IllegalArgumentException(
String.format(
Locale.ROOT,
"Runtime field [%s] is emitting [%s] characters while the maximum number of values allowed is [%s]",
fieldName,
chars,
MAX_CHARS
)
);
}
results.add(v);
}
NOTE! The maximum number of characters that can be emitted is 1048576. This is constant and cannot be overridden (
public static final long MAX_CHARS = 1024 * 1024;
).