Skip to content

Commit e7cd5c9

Browse files
authored
Add postal_code support to the City and Enterprise databases (#114193)
1 parent 7753c52 commit e7cd5c9

File tree

5 files changed

+28
-9
lines changed

5 files changed

+28
-9
lines changed

modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/Database.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ enum Database {
4040
Property.REGION_NAME,
4141
Property.CITY_NAME,
4242
Property.TIMEZONE,
43-
Property.LOCATION
43+
Property.LOCATION,
44+
Property.POSTAL_CODE
4445
),
4546
Set.of(
4647
Property.COUNTRY_ISO_CODE,
@@ -108,7 +109,8 @@ enum Database {
108109
Property.MOBILE_COUNTRY_CODE,
109110
Property.MOBILE_NETWORK_CODE,
110111
Property.USER_TYPE,
111-
Property.CONNECTION_TYPE
112+
Property.CONNECTION_TYPE,
113+
Property.POSTAL_CODE
112114
),
113115
Set.of(
114116
Property.COUNTRY_ISO_CODE,
@@ -228,7 +230,8 @@ enum Property {
228230
MOBILE_NETWORK_CODE,
229231
CONNECTION_TYPE,
230232
USER_TYPE,
231-
TYPE;
233+
TYPE,
234+
POSTAL_CODE;
232235

233236
/**
234237
* Parses a string representation of a property into an actual Property instance. Not all properties that exist are

modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/MaxmindIpDataLookups.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.maxmind.geoip2.model.IspResponse;
2424
import com.maxmind.geoip2.record.Continent;
2525
import com.maxmind.geoip2.record.Location;
26+
import com.maxmind.geoip2.record.Postal;
2627
import com.maxmind.geoip2.record.Subdivision;
2728

2829
import org.elasticsearch.common.network.InetAddresses;
@@ -139,6 +140,7 @@ protected Map<String, Object> transform(final CityResponse response) {
139140
Location location = response.getLocation();
140141
Continent continent = response.getContinent();
141142
Subdivision subdivision = response.getMostSpecificSubdivision();
143+
Postal postal = response.getPostal();
142144

143145
Map<String, Object> data = new HashMap<>();
144146
for (Database.Property property : this.properties) {
@@ -206,6 +208,11 @@ protected Map<String, Object> transform(final CityResponse response) {
206208
data.put("location", locationObject);
207209
}
208210
}
211+
case POSTAL_CODE -> {
212+
if (postal != null && postal.getCode() != null) {
213+
data.put("postal_code", postal.getCode());
214+
}
215+
}
209216
}
210217
}
211218
return data;
@@ -324,6 +331,7 @@ protected Map<String, Object> transform(final EnterpriseResponse response) {
324331
Location location = response.getLocation();
325332
Continent continent = response.getContinent();
326333
Subdivision subdivision = response.getMostSpecificSubdivision();
334+
Postal postal = response.getPostal();
327335

328336
Long asn = response.getTraits().getAutonomousSystemNumber();
329337
String organizationName = response.getTraits().getAutonomousSystemOrganization();
@@ -413,6 +421,11 @@ protected Map<String, Object> transform(final EnterpriseResponse response) {
413421
data.put("location", locationObject);
414422
}
415423
}
424+
case POSTAL_CODE -> {
425+
if (postal != null && postal.getCode() != null) {
426+
data.put("postal_code", postal.getCode());
427+
}
428+
}
416429
case ASN -> {
417430
if (asn != null) {
418431
data.put("asn", asn);

modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpProcessorFactoryTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@ public void testBuildIllegalFieldOption() {
274274
e.getMessage(),
275275
equalTo(
276276
"[properties] illegal property value [invalid]. valid values are [IP, COUNTRY_ISO_CODE, "
277-
+ "COUNTRY_NAME, CONTINENT_CODE, CONTINENT_NAME, REGION_ISO_CODE, REGION_NAME, CITY_NAME, TIMEZONE, LOCATION]"
277+
+ "COUNTRY_NAME, CONTINENT_CODE, CONTINENT_NAME, REGION_ISO_CODE, REGION_NAME, CITY_NAME, TIMEZONE, "
278+
+ "LOCATION, POSTAL_CODE]"
278279
)
279280
);
280281

modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpProcessorTests.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ public void testCity_withIpV6() throws Exception {
222222
@SuppressWarnings("unchecked")
223223
Map<String, Object> geoData = (Map<String, Object>) ingestDocument.getSourceAndMetadata().get("target_field");
224224
assertThat(geoData, notNullValue());
225-
assertThat(geoData.size(), equalTo(10));
225+
assertThat(geoData.size(), equalTo(11));
226226
assertThat(geoData.get("ip"), equalTo(ip));
227227
assertThat(geoData.get("country_iso_code"), equalTo("US"));
228228
assertThat(geoData.get("country_name"), equalTo("United States"));
@@ -233,6 +233,7 @@ public void testCity_withIpV6() throws Exception {
233233
assertThat(geoData.get("city_name"), equalTo("Homestead"));
234234
assertThat(geoData.get("timezone"), equalTo("America/New_York"));
235235
assertThat(geoData.get("location"), equalTo(Map.of("lat", 25.4573d, "lon", -80.4572d)));
236+
assertThat(geoData.get("postal_code"), equalTo("33035"));
236237
}
237238

238239
public void testCityWithMissingLocation() throws Exception {
@@ -470,7 +471,7 @@ public void testEnterprise() throws Exception {
470471
@SuppressWarnings("unchecked")
471472
Map<String, Object> geoData = (Map<String, Object>) ingestDocument.getSourceAndMetadata().get("target_field");
472473
assertThat(geoData, notNullValue());
473-
assertThat(geoData.size(), equalTo(24));
474+
assertThat(geoData.size(), equalTo(25));
474475
assertThat(geoData.get("ip"), equalTo(ip));
475476
assertThat(geoData.get("country_iso_code"), equalTo("US"));
476477
assertThat(geoData.get("country_name"), equalTo("United States"));
@@ -481,6 +482,7 @@ public void testEnterprise() throws Exception {
481482
assertThat(geoData.get("city_name"), equalTo("Chatham"));
482483
assertThat(geoData.get("timezone"), equalTo("America/New_York"));
483484
assertThat(geoData.get("location"), equalTo(Map.of("lat", 42.3478, "lon", -73.5549)));
485+
assertThat(geoData.get("postal_code"), equalTo("12037"));
484486
assertThat(geoData.get("asn"), equalTo(14671L));
485487
assertThat(geoData.get("organization_name"), equalTo("FairPoint Communications"));
486488
assertThat(geoData.get("network"), equalTo("74.209.16.0/20"));

modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/MaxMindSupportTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ public class MaxMindSupportTests extends ESTestCase {
8484
"location.longitude",
8585
"location.timeZone",
8686
"mostSpecificSubdivision.isoCode",
87-
"mostSpecificSubdivision.name"
87+
"mostSpecificSubdivision.name",
88+
"postal.code"
8889
);
8990
private static final Set<String> CITY_UNSUPPORTED_FIELDS = Set.of(
9091
"city.confidence",
@@ -109,7 +110,6 @@ public class MaxMindSupportTests extends ESTestCase {
109110
"mostSpecificSubdivision.confidence",
110111
"mostSpecificSubdivision.geoNameId",
111112
"mostSpecificSubdivision.names",
112-
"postal.code",
113113
"postal.confidence",
114114
"registeredCountry.confidence",
115115
"registeredCountry.geoNameId",
@@ -223,6 +223,7 @@ public class MaxMindSupportTests extends ESTestCase {
223223
"location.timeZone",
224224
"mostSpecificSubdivision.isoCode",
225225
"mostSpecificSubdivision.name",
226+
"postal.code",
226227
"traits.anonymous",
227228
"traits.anonymousVpn",
228229
"traits.autonomousSystemNumber",
@@ -263,7 +264,6 @@ public class MaxMindSupportTests extends ESTestCase {
263264
"mostSpecificSubdivision.confidence",
264265
"mostSpecificSubdivision.geoNameId",
265266
"mostSpecificSubdivision.names",
266-
"postal.code",
267267
"postal.confidence",
268268
"registeredCountry.confidence",
269269
"registeredCountry.geoNameId",

0 commit comments

Comments
 (0)