Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.7][ML] Anomaly detection jobs should allow missing values for geo … #57341

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ public GeoPointField(String name) {
@Override
public Object[] value(SearchHit hit) {
Object[] value = super.value(hit);
if (value.length != 1) {
if (value.length == 0) {
return value;
}
if (value.length > 1) {
throw new IllegalStateException("Unexpected values for a geo_point field: " + Arrays.toString(value));
}

if (value[0] instanceof String) {
value[0] = handleString((String) value[0]);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ public GeoShapeField(String name) {
@Override
public Object[] value(SearchHit hit) {
Object[] value = super.value(hit);
if (value.length != 1) {
if (value.length == 0) {
return value;
}
if (value.length > 1) {
throw new IllegalStateException("Unexpected values for a geo_shape field: " + Arrays.toString(value));
}

if (value[0] instanceof String) {
value[0] = handleString((String) value[0]);
} else if (value[0] instanceof Map<?, ?>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.ml.test.SearchHitBuilder;

import java.util.Arrays;

import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -36,4 +38,21 @@ public void testGivenGeoPoint() {
assertThat(geo.isMultiField(), is(false));
expectThrows(UnsupportedOperationException.class, () -> geo.getParentField());
}

public void testMissing() {
SearchHit hit = new SearchHitBuilder(42).addField("a_keyword", "bar").build();

ExtractedField geo = new GeoPointField("missing");

assertThat(geo.value(hit), equalTo(new Object[0]));
}

public void testArray() {
SearchHit hit = new SearchHitBuilder(42).addField("geo", Arrays.asList(1, 2)).build();

ExtractedField geo = new GeoPointField("geo");

IllegalStateException e = expectThrows(IllegalStateException.class, () -> geo.value(hit));
assertThat(e.getMessage(), equalTo("Unexpected values for a geo_point field: [1, 2]"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,21 @@ public void testWKTFormat() {
assertThat(geo.isMultiField(), is(false));
expectThrows(UnsupportedOperationException.class, () -> geo.getParentField());
}

public void testMissing() {
SearchHit hit = new SearchHitBuilder(42).addField("a_keyword", "bar").build();

ExtractedField geo = new GeoShapeField("missing");

assertThat(geo.value(hit), equalTo(new Object[0]));
}

public void testArray() {
SearchHit hit = new SearchHitBuilder(42).setSource("{\"geo\":[1,2]}").build();

ExtractedField geo = new GeoShapeField("geo");

IllegalStateException e = expectThrows(IllegalStateException.class, () -> geo.value(hit));
assertThat(e.getMessage(), equalTo("Unexpected values for a geo_shape field: [1, 2]"));
}
}