Skip to content

Commit e57cc4f

Browse files
authored
Add region ISO code to GeoIP Ingest plugin (#31669) (#33276)
1 parent 1dc0e93 commit e57cc4f

File tree

5 files changed

+27
-11
lines changed

5 files changed

+27
-11
lines changed

docs/plugins/ingest-geoip.asciidoc

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ include::install_remove.asciidoc[]
2626
| `field` | yes | - | The field to get the ip address from for the geographical lookup.
2727
| `target_field` | no | geoip | The field that will hold the geographical information looked up from the Maxmind database.
2828
| `database_file` | no | GeoLite2-City.mmdb | The database filename in the geoip config directory. The ingest-geoip plugin ships with the GeoLite2-City.mmdb, GeoLite2-Country.mmdb and GeoLite2-ASN.mmdb files.
29-
| `properties` | no | [`continent_name`, `country_iso_code`, `region_name`, `city_name`, `location`] * | Controls what properties are added to the `target_field` based on the geoip lookup.
29+
| `properties` | no | [`continent_name`, `country_iso_code`, `region_iso_code`, `region_name`, `city_name`, `location`] * | Controls what properties are added to the `target_field` based on the geoip lookup.
3030
| `ignore_missing` | no | `false` | If `true` and `field` does not exist, the processor quietly exits without modifying the document
3131
|======
3232

3333
*Depends on what is available in `database_field`:
3434

3535
* If the GeoLite2 City database is used, then the following fields may be added under the `target_field`: `ip`,
36-
`country_iso_code`, `country_name`, `continent_name`, `region_name`, `city_name`, `timezone`, `latitude`, `longitude`
36+
`country_iso_code`, `country_name`, `continent_name`, `region_iso_code`, `region_name`, `city_name`, `timezone`, `latitude`, `longitude`
3737
and `location`. The fields actually added depend on what has been found and which properties were configured in `properties`.
3838
* If the GeoLite2 Country database is used, then the following fields may be added under the `target_field`: `ip`,
3939
`country_iso_code`, `country_name` and `continent_name`. The fields actually added depend on what has been found and which properties

plugins/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpProcessor.java

+15-3
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,16 @@ private Map<String, Object> retrieveCityGeoData(InetAddress ipAddress) {
185185
geoData.put("continent_name", continentName);
186186
}
187187
break;
188+
case REGION_ISO_CODE:
189+
// ISO 3166-2 code for country subdivisions.
190+
// See iso.org/iso-3166-country-codes.html
191+
String countryIso = country.getIsoCode();
192+
String subdivisionIso = subdivision.getIsoCode();
193+
if (countryIso != null && subdivisionIso != null) {
194+
String regionIsoCode = countryIso + "-" + subdivisionIso;
195+
geoData.put("region_iso_code", regionIsoCode);
196+
}
197+
break;
188198
case REGION_NAME:
189199
String subdivisionName = subdivision.getName();
190200
if (subdivisionName != null) {
@@ -300,8 +310,8 @@ private Map<String, Object> retrieveAsnGeoData(InetAddress ipAddress) {
300310

301311
public static final class Factory implements Processor.Factory {
302312
static final Set<Property> DEFAULT_CITY_PROPERTIES = EnumSet.of(
303-
Property.CONTINENT_NAME, Property.COUNTRY_ISO_CODE, Property.REGION_NAME,
304-
Property.CITY_NAME, Property.LOCATION
313+
Property.CONTINENT_NAME, Property.COUNTRY_ISO_CODE, Property.REGION_ISO_CODE,
314+
Property.REGION_NAME, Property.CITY_NAME, Property.LOCATION
305315
);
306316
static final Set<Property> DEFAULT_COUNTRY_PROPERTIES = EnumSet.of(
307317
Property.CONTINENT_NAME, Property.COUNTRY_ISO_CODE
@@ -377,6 +387,7 @@ enum Property {
377387
COUNTRY_ISO_CODE,
378388
COUNTRY_NAME,
379389
CONTINENT_NAME,
390+
REGION_ISO_CODE,
380391
REGION_NAME,
381392
CITY_NAME,
382393
TIMEZONE,
@@ -386,7 +397,8 @@ enum Property {
386397

387398
static final EnumSet<Property> ALL_CITY_PROPERTIES = EnumSet.of(
388399
Property.IP, Property.COUNTRY_ISO_CODE, Property.COUNTRY_NAME, Property.CONTINENT_NAME,
389-
Property.REGION_NAME, Property.CITY_NAME, Property.TIMEZONE, Property.LOCATION
400+
Property.REGION_ISO_CODE, Property.REGION_NAME, Property.CITY_NAME, Property.TIMEZONE,
401+
Property.LOCATION
390402
);
391403
static final EnumSet<Property> ALL_COUNTRY_PROPERTIES = EnumSet.of(
392404
Property.IP, Property.CONTINENT_NAME, Property.COUNTRY_NAME, Property.COUNTRY_ISO_CODE

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ public void testBuildIllegalFieldOption() throws Exception {
284284
config1.put("properties", Collections.singletonList("invalid"));
285285
Exception e = expectThrows(ElasticsearchParseException.class, () -> factory.create(null, null, config1));
286286
assertThat(e.getMessage(), equalTo("[properties] illegal property value [invalid]. valid values are [IP, COUNTRY_ISO_CODE, " +
287-
"COUNTRY_NAME, CONTINENT_NAME, REGION_NAME, CITY_NAME, TIMEZONE, LOCATION]"));
287+
"COUNTRY_NAME, CONTINENT_NAME, REGION_ISO_CODE, REGION_NAME, CITY_NAME, TIMEZONE, LOCATION]"));
288288

289289
Map<String, Object> config2 = new HashMap<>();
290290
config2.put("field", "_field");

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,12 @@ public void testCity_withIpV6() throws Exception {
117117
assertThat(ingestDocument.getSourceAndMetadata().get("source_field"), equalTo(address));
118118
@SuppressWarnings("unchecked")
119119
Map<String, Object> geoData = (Map<String, Object>) ingestDocument.getSourceAndMetadata().get("target_field");
120-
assertThat(geoData.size(), equalTo(8));
120+
assertThat(geoData.size(), equalTo(9));
121121
assertThat(geoData.get("ip"), equalTo(address));
122122
assertThat(geoData.get("country_iso_code"), equalTo("US"));
123123
assertThat(geoData.get("country_name"), equalTo("United States"));
124124
assertThat(geoData.get("continent_name"), equalTo("North America"));
125+
assertThat(geoData.get("region_iso_code"), equalTo("US-FL"));
125126
assertThat(geoData.get("region_name"), equalTo("Florida"));
126127
assertThat(geoData.get("city_name"), equalTo("Hollywood"));
127128
assertThat(geoData.get("timezone"), equalTo("America/New_York"));

plugins/ingest-geoip/src/test/resources/rest-api-spec/test/ingest_geoip/20_geoip_processor.yml

+7-4
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@
3030
type: test
3131
id: 1
3232
- match: { _source.field1: "128.101.101.101" }
33-
- length: { _source.geoip: 5 }
33+
- length: { _source.geoip: 6 }
3434
- match: { _source.geoip.city_name: "Minneapolis" }
3535
- match: { _source.geoip.country_iso_code: "US" }
3636
- match: { _source.geoip.location.lon: -93.2166 }
3737
- match: { _source.geoip.location.lat: 44.9759 }
38+
- match: { _source.geoip.region_iso_code: "US-MN" }
3839
- match: { _source.geoip.region_name: "Minnesota" }
3940
- match: { _source.geoip.continent_name: "North America" }
4041

@@ -54,7 +55,7 @@
5455
{
5556
"geoip" : {
5657
"field" : "field1",
57-
"properties" : ["city_name", "country_iso_code", "ip", "location", "timezone", "country_name", "region_name", "continent_name"]
58+
"properties" : ["city_name", "country_iso_code", "ip", "location", "timezone", "country_name", "region_iso_code", "region_name", "continent_name"]
5859
}
5960
}
6061
]
@@ -75,14 +76,15 @@
7576
type: test
7677
id: 1
7778
- match: { _source.field1: "128.101.101.101" }
78-
- length: { _source.geoip: 8 }
79+
- length: { _source.geoip: 9 }
7980
- match: { _source.geoip.city_name: "Minneapolis" }
8081
- match: { _source.geoip.country_iso_code: "US" }
8182
- match: { _source.geoip.ip: "128.101.101.101" }
8283
- match: { _source.geoip.location.lon: -93.2166 }
8384
- match: { _source.geoip.location.lat: 44.9759 }
8485
- match: { _source.geoip.timezone: "America/Chicago" }
8586
- match: { _source.geoip.country_name: "United States" }
87+
- match: { _source.geoip.region_iso_code: "US-MN" }
8688
- match: { _source.geoip.region_name: "Minnesota" }
8789
- match: { _source.geoip.continent_name: "North America" }
8890

@@ -188,11 +190,12 @@
188190
type: test
189191
id: 2
190192
- match: { _source.field1: "128.101.101.101" }
191-
- length: { _source.geoip: 5 }
193+
- length: { _source.geoip: 6 }
192194
- match: { _source.geoip.city_name: "Minneapolis" }
193195
- match: { _source.geoip.country_iso_code: "US" }
194196
- match: { _source.geoip.location.lon: -93.2166 }
195197
- match: { _source.geoip.location.lat: 44.9759 }
198+
- match: { _source.geoip.region_iso_code: "US-MN" }
196199
- match: { _source.geoip.region_name: "Minnesota" }
197200
- match: { _source.geoip.continent_name: "North America" }
198201

0 commit comments

Comments
 (0)