[ML] Lock down extraction method when possible (#43104) (#43140)

This commit is contained in:
Dimitris Athanasiou 2019-06-12 14:07:17 +03:00 committed by GitHub
parent e5a4a2272b
commit b28e006f7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 19 deletions

View File

@ -69,12 +69,12 @@ public abstract class ExtractedField {
return new TimeField(name, extractionMethod); return new TimeField(name, extractionMethod);
} }
public static ExtractedField newGeoShapeField(String alias, String name, ExtractionMethod extractionMethod) { public static ExtractedField newGeoShapeField(String alias, String name) {
return new GeoShapeField(alias, name, extractionMethod); return new GeoShapeField(alias, name);
} }
public static ExtractedField newGeoPointField(String alias, String name, ExtractionMethod extractionMethod) { public static ExtractedField newGeoPointField(String alias, String name) {
return new GeoPointField(alias, name, extractionMethod); return new GeoPointField(alias, name);
} }
public static ExtractedField newField(String name, ExtractionMethod extractionMethod) { public static ExtractedField newField(String name, ExtractionMethod extractionMethod) {
@ -87,7 +87,7 @@ public abstract class ExtractedField {
case SCRIPT_FIELD: case SCRIPT_FIELD:
return new FromFields(alias, name, extractionMethod); return new FromFields(alias, name, extractionMethod);
case SOURCE: case SOURCE:
return new FromSource(alias, name, extractionMethod); return new FromSource(alias, name);
default: default:
throw new IllegalArgumentException("Invalid extraction method [" + extractionMethod + "]"); throw new IllegalArgumentException("Invalid extraction method [" + extractionMethod + "]");
} }
@ -113,8 +113,8 @@ public abstract class ExtractedField {
private static class GeoShapeField extends FromSource { private static class GeoShapeField extends FromSource {
private static final WellKnownText wkt = new WellKnownText(); private static final WellKnownText wkt = new WellKnownText();
GeoShapeField(String alias, String name, ExtractionMethod extractionMethod) { GeoShapeField(String alias, String name) {
super(alias, name, extractionMethod); super(alias, name);
} }
@Override @Override
@ -170,11 +170,8 @@ public abstract class ExtractedField {
private static class GeoPointField extends FromFields { private static class GeoPointField extends FromFields {
GeoPointField(String alias, String name, ExtractionMethod extractionMethod) { GeoPointField(String alias, String name) {
super(alias, name, extractionMethod); super(alias, name, ExtractionMethod.DOC_VALUE);
if (extractionMethod != ExtractionMethod.DOC_VALUE) {
throw new IllegalArgumentException("cannot use [geo_point] field with disabled doc values");
}
} }
@Override @Override
@ -232,8 +229,8 @@ public abstract class ExtractedField {
private String[] namePath; private String[] namePath;
FromSource(String alias, String name, ExtractionMethod extractionMethod) { FromSource(String alias, String name) {
super(alias, name, extractionMethod); super(alias, name, ExtractionMethod.SOURCE);
namePath = name.split("\\."); namePath = name.split("\\.");
} }

View File

@ -87,12 +87,17 @@ public class ExtractedFields {
: ExtractedField.ExtractionMethod.SOURCE; : ExtractedField.ExtractionMethod.SOURCE;
} }
} }
if (isFieldOfType(field, "geo_point")) { if (isFieldOfType(field, "geo_point")) {
return ExtractedField.newGeoPointField(field, internalField, method); if (method != ExtractedField.ExtractionMethod.DOC_VALUE) {
throw new IllegalArgumentException("cannot use [geo_point] field with disabled doc values");
}
return ExtractedField.newGeoPointField(field, internalField);
} }
if (isFieldOfType(field, "geo_shape")) { if (isFieldOfType(field, "geo_shape")) {
return ExtractedField.newGeoShapeField(field, internalField, method); return ExtractedField.newGeoShapeField(field, internalField);
} }
return ExtractedField.newField(field, internalField, method); return ExtractedField.newField(field, internalField, method);
} }

View File

@ -68,7 +68,7 @@ public class ExtractedFieldTests extends ESTestCase {
String[] expected = new String[] {lat + "," + lon}; String[] expected = new String[] {lat + "," + lon};
// doc_value field // doc_value field
ExtractedField geo = ExtractedField.newGeoPointField("geo", "geo", ExtractedField.ExtractionMethod.DOC_VALUE); ExtractedField geo = ExtractedField.newGeoPointField("geo", "geo");
SearchHit hit = new SearchHitBuilder(42).addField("geo", lat + ", " + lon).build(); SearchHit hit = new SearchHitBuilder(42).addField("geo", lat + ", " + lon).build();
assertThat(geo.value(hit), equalTo(expected)); assertThat(geo.value(hit), equalTo(expected));
} }
@ -81,12 +81,12 @@ public class ExtractedFieldTests extends ESTestCase {
SearchHit hit = new SearchHitBuilder(42) SearchHit hit = new SearchHitBuilder(42)
.setSource("{\"geo\":{\"type\":\"point\", \"coordinates\": [" + lon + ", " + lat + "]}}") .setSource("{\"geo\":{\"type\":\"point\", \"coordinates\": [" + lon + ", " + lat + "]}}")
.build(); .build();
ExtractedField geo = ExtractedField.newGeoShapeField("geo", "geo", ExtractedField.ExtractionMethod.SOURCE); ExtractedField geo = ExtractedField.newGeoShapeField("geo", "geo");
assertThat(geo.value(hit), equalTo(expected)); assertThat(geo.value(hit), equalTo(expected));
// WKT format // WKT format
hit = new SearchHitBuilder(42).setSource("{\"geo\":\"POINT ("+ lon + " " + lat + ")\"}").build(); hit = new SearchHitBuilder(42).setSource("{\"geo\":\"POINT ("+ lon + " " + lat + ")\"}").build();
geo = ExtractedField.newGeoShapeField("geo", "geo", ExtractedField.ExtractionMethod.SOURCE); geo = ExtractedField.newGeoShapeField("geo", "geo");
assertThat(geo.value(hit), equalTo(expected)); assertThat(geo.value(hit), equalTo(expected));
} }