diff --git a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/MaxmindIpDataLookups.java b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/MaxmindIpDataLookups.java index 8bc74c0e4aac4..061da0c5d1036 100644 --- a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/MaxmindIpDataLookups.java +++ b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/MaxmindIpDataLookups.java @@ -209,9 +209,9 @@ protected Map transform(final CityResponse response) { switch (property) { case IP -> data.put("ip", response.getTraits().getIpAddress()); case COUNTRY_IN_EUROPEAN_UNION -> { - if (country.getIsoCode() != null) { - // isInEuropeanUnion is a boolean so it can't be null. But it really only makes sense if we have a country - data.put("country_in_european_union", country.isInEuropeanUnion()); + Boolean isInEuropeanUnion = isInEuropeanUnion(country); + if (isInEuropeanUnion != null) { + data.put("country_in_european_union", isInEuropeanUnion); } } case COUNTRY_ISO_CODE -> { @@ -239,12 +239,8 @@ protected Map transform(final CityResponse response) { } } case REGION_ISO_CODE -> { - // ISO 3166-2 code for country subdivisions. - // See iso.org/iso-3166-country-codes.html - String countryIso = country.getIsoCode(); - String subdivisionIso = subdivision.getIsoCode(); - if (countryIso != null && subdivisionIso != null) { - String regionIsoCode = countryIso + "-" + subdivisionIso; + String regionIsoCode = regionIsoCode(country, subdivision); + if (regionIsoCode != null) { data.put("region_iso_code", regionIsoCode); } } @@ -270,7 +266,7 @@ protected Map transform(final CityResponse response) { Double latitude = location.getLatitude(); Double longitude = location.getLongitude(); if (latitude != null && longitude != null) { - Map locationObject = new HashMap<>(); + Map locationObject = HashMap.newHashMap(2); locationObject.put("lat", latitude); locationObject.put("lon", longitude); data.put("location", locationObject); @@ -288,9 +284,9 @@ protected Map transform(final CityResponse response) { } } case REGISTERED_COUNTRY_IN_EUROPEAN_UNION -> { - if (registeredCountry.getIsoCode() != null) { - // isInEuropeanUnion is a boolean so it can't be null. But it really only makes sense if we have a country - data.put("registered_country_in_european_union", registeredCountry.isInEuropeanUnion()); + Boolean isInEuropeanUnion = isInEuropeanUnion(registeredCountry); + if (isInEuropeanUnion != null) { + data.put("registered_country_in_european_union", isInEuropeanUnion); } } case REGISTERED_COUNTRY_ISO_CODE -> { @@ -353,9 +349,9 @@ protected Map transform(final CountryResponse response) { switch (property) { case IP -> data.put("ip", response.getTraits().getIpAddress()); case COUNTRY_IN_EUROPEAN_UNION -> { - if (country.getIsoCode() != null) { - // isInEuropeanUnion is a boolean so it can't be null. But it really only makes sense if we have a country - data.put("country_in_european_union", country.isInEuropeanUnion()); + Boolean isInEuropeanUnion = isInEuropeanUnion(country); + if (isInEuropeanUnion != null) { + data.put("country_in_european_union", isInEuropeanUnion); } } case COUNTRY_ISO_CODE -> { @@ -383,9 +379,9 @@ protected Map transform(final CountryResponse response) { } } case REGISTERED_COUNTRY_IN_EUROPEAN_UNION -> { - if (registeredCountry.getIsoCode() != null) { - // isInEuropeanUnion is a boolean so it can't be null. But it really only makes sense if we have a country - data.put("registered_country_in_european_union", registeredCountry.isInEuropeanUnion()); + Boolean isInEuropeanUnion = isInEuropeanUnion(registeredCountry); + if (isInEuropeanUnion != null) { + data.put("registered_country_in_european_union", isInEuropeanUnion); } } case REGISTERED_COUNTRY_ISO_CODE -> { @@ -480,9 +476,9 @@ protected Map transform(final EnterpriseResponse response) { } } case COUNTRY_IN_EUROPEAN_UNION -> { - if (country.getIsoCode() != null) { - // isInEuropeanUnion is a boolean so it can't be null. But it really only makes sense if we have a country - data.put("country_in_european_union", country.isInEuropeanUnion()); + Boolean isInEuropeanUnion = isInEuropeanUnion(country); + if (isInEuropeanUnion != null) { + data.put("country_in_european_union", isInEuropeanUnion); } } case COUNTRY_ISO_CODE -> { @@ -510,12 +506,8 @@ protected Map transform(final EnterpriseResponse response) { } } case REGION_ISO_CODE -> { - // ISO 3166-2 code for country subdivisions. - // See iso.org/iso-3166-country-codes.html - String countryIso = country.getIsoCode(); - String subdivisionIso = subdivision.getIsoCode(); - if (countryIso != null && subdivisionIso != null) { - String regionIsoCode = countryIso + "-" + subdivisionIso; + String regionIsoCode = regionIsoCode(country, subdivision); + if (regionIsoCode != null) { data.put("region_iso_code", regionIsoCode); } } @@ -547,7 +539,7 @@ protected Map transform(final EnterpriseResponse response) { Double latitude = location.getLatitude(); Double longitude = location.getLongitude(); if (latitude != null && longitude != null) { - Map locationObject = new HashMap<>(); + Map locationObject = HashMap.newHashMap(2); locationObject.put("lat", latitude); locationObject.put("lon", longitude); data.put("location", locationObject); @@ -639,9 +631,9 @@ protected Map transform(final EnterpriseResponse response) { } } case REGISTERED_COUNTRY_IN_EUROPEAN_UNION -> { - if (registeredCountry.getIsoCode() != null) { - // isInEuropeanUnion is a boolean so it can't be null. But it really only makes sense if we have a country - data.put("registered_country_in_european_union", registeredCountry.isInEuropeanUnion()); + Boolean isInEuropeanUnion = isInEuropeanUnion(registeredCountry); + if (isInEuropeanUnion != null) { + data.put("registered_country_in_european_union", isInEuropeanUnion); } } case REGISTERED_COUNTRY_ISO_CODE -> { @@ -776,4 +768,23 @@ private RESPONSE lookup(final Reader reader, final String ipAddress) throws IOEx */ protected abstract Map transform(RESPONSE response); } + + @Nullable + private static Boolean isInEuropeanUnion(com.maxmind.geoip2.record.Country country) { + // isInEuropeanUnion is a lowercase-b boolean so it cannot be null, but it really only makes sense for us to return a value + // for this if there's actually a real country here, as opposed to an empty null-object country, so we check for an iso code first + return (country.getIsoCode() == null) ? null : country.isInEuropeanUnion(); + } + + @Nullable + private static String regionIsoCode(final com.maxmind.geoip2.record.Country country, final Subdivision subdivision) { + // ISO 3166-2 code for country subdivisions, see https://www.iso.org/iso-3166-country-codes.html + final String countryIso = country.getIsoCode(); + final String subdivisionIso = subdivision.getIsoCode(); + if (countryIso != null && subdivisionIso != null) { + return countryIso + "-" + subdivisionIso; + } else { + return null; + } + } }