Skip to content

Commit

Permalink
[7.7][ML] Anomaly detection jobs should allow missing values for geo …
Browse files Browse the repository at this point in the history
…fields (#57300) (#57341)

Allows geo fields (`geo_point`, `geo_shape`) to have missing values.
Fixes a bug where such missing values would result in an error.

Closes #57299

Backport of #57300
  • Loading branch information
dimitris-athanasiou authored May 29, 2020
1 parent 61c2ea0 commit cf4543e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
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]"));
}
}

0 comments on commit cf4543e

Please sign in to comment.