Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-3064 Upgrade index-geoip to GeoIP2 5.0.2

lewismcf36a83656613
diff --git a/conf/nutch-default.xml b/conf/nutch-default.xml
index 9b9461b69..6e4fd5823 100644
--- a/conf/nutch-default.xml
+++ b/conf/nutch-default.xml
@@ -2278,6 +2278,17 @@ CAUTION: Set the parser.timeout to -1 or a bigger value than 30, when using this
</description>
</property>
+<property>
+ <name>index.geoip.db.country</name>
+ <value></value>
+ <description>
+ GeoIP2/GeoLite2 Country database file (MMDB format).
+ Provides country, continent, and represented country information.
+ This is a lighter-weight alternative to the City database when only
+ country-level information is needed.
+ </description>
+</property>
+
<property>
<name>index.geoip.db.domain</name>
<value></value>
diff --git a/src/plugin/index-geoip/src/java/org/apache/nutch/indexer/geoip/GeoIPDocumentCreator.java b/src/plugin/index-geoip/src/java/org/apache/nutch/indexer/geoip/GeoIPDocumentCreator.java
index 2a7777bad..e7da55f95 100644
--- a/src/plugin/index-geoip/src/java/org/apache/nutch/indexer/geoip/GeoIPDocumentCreator.java
+++ b/src/plugin/index-geoip/src/java/org/apache/nutch/indexer/geoip/GeoIPDocumentCreator.java
@@ -22,6 +22,7 @@ import java.net.InetAddress;
import java.util.Optional;
import org.apache.nutch.indexer.NutchDocument;
+import org.apache.nutch.indexer.NutchField;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -32,6 +33,7 @@ import com.maxmind.geoip2.model.AnonymousIpResponse;
import com.maxmind.geoip2.model.AsnResponse;
import com.maxmind.geoip2.model.CityResponse;
import com.maxmind.geoip2.model.ConnectionTypeResponse;
+import com.maxmind.geoip2.model.CountryResponse;
import com.maxmind.geoip2.model.DomainResponse;
import com.maxmind.geoip2.model.InsightsResponse;
import com.maxmind.geoip2.model.IspResponse;
@@ -56,7 +58,14 @@ public class GeoIPDocumentCreator {
private static final Logger LOG = LoggerFactory
.getLogger(MethodHandles.lookup().lookupClass());
- private static final String NETWORK_ADDRESS = "networkAddress";
+ // Database-specific network address field names
+ static final String ANONYMOUS_NETWORK_ADDRESS = "anonymousNetworkAddress";
+ static final String ASN_NETWORK_ADDRESS = "asnNetworkAddress";
+ static final String CITY_NETWORK_ADDRESS = "cityNetworkAddress";
+ static final String CONNECTION_NETWORK_ADDRESS = "connectionNetworkAddress";
+ static final String COUNTRY_NETWORK_ADDRESS = "countryNetworkAddress";
+ static final String DOMAIN_NETWORK_ADDRESS = "domainNetworkAddress";
+ static final String INSIGHTS_NETWORK_ADDRESS = "insightsNetworkAddress";
private GeoIPDocumentCreator() {}
@@ -73,6 +82,23 @@ public class GeoIPDocumentCreator {
}
}
+ /**
+ * Add field to document only if the field doesn't already contain this exact value.
+ * Use for fields like "ip" that should not have duplicate values.
+ * @param doc the {@link NutchDocument} to augment
+ * @param name the name of the target field
+ * @param value the value to associate with the target field
+ */
+ static void addIfNotDuplicate(NutchDocument doc, String name, Object value) {
+ if (value == null) {
+ return;
+ }
+ NutchField field = doc.getField(name);
+ if (field == null || !field.getValues().contains(value)) {
+ doc.add(name, value);
+ }
+ }
+
/**
* Populate a {@link org.apache.nutch.indexer.NutchDocument} based on lookup
* of IP in Anonymous IP database.
@@ -88,8 +114,8 @@ public class GeoIPDocumentCreator {
Optional<AnonymousIpResponse> opt = reader.tryAnonymousIp(InetAddress.getByName(serverIp));
if (opt.isPresent()) {
AnonymousIpResponse response = opt.get();
- addIfNotNull(doc, "ip", response.getIpAddress());
- addIfNotNull(doc, NETWORK_ADDRESS, response.getNetwork().toString());
+ addIfNotDuplicate(doc, "ip", response.getIpAddress());
+ addIfNotNull(doc, ANONYMOUS_NETWORK_ADDRESS, response.getNetwork().toString());
addIfNotNull(doc, "isAnonymous", response.isAnonymous());
addIfNotNull(doc, "isAnonymousVpn", response.isAnonymousVpn());
addIfNotNull(doc, "isHostingProxy", response.isHostingProvider());
@@ -117,8 +143,8 @@ public class GeoIPDocumentCreator {
Optional<AsnResponse> opt = reader.tryAsn(InetAddress.getByName(serverIp));
if (opt.isPresent()) {
AsnResponse response = opt.get();
- addIfNotNull(doc, "ip", response.getIpAddress());
- addIfNotNull(doc, NETWORK_ADDRESS, response.getNetwork().toString());
+ addIfNotDuplicate(doc, "ip", response.getIpAddress());
+ addIfNotNull(doc, ASN_NETWORK_ADDRESS, response.getNetwork().toString());
addIfNotNull(doc, "autonomousSystemNumber", response.getAutonomousSystemNumber());
addIfNotNull(doc, "autonomousSystemOrganization", response.getAutonomousSystemOrganization());
} else {
@@ -139,17 +165,17 @@ public class GeoIPDocumentCreator {
*/
public static NutchDocument createDocFromCityDb(String serverIp,
NutchDocument doc, DatabaseReader reader) throws IOException, GeoIp2Exception {
- addIfNotNull(doc, "ip", serverIp);
+ addIfNotDuplicate(doc, "ip", serverIp);
Optional<CityResponse> opt = reader.tryCity(InetAddress.getByName(serverIp));
if (opt.isPresent()) {
- processDocument(doc, opt.get());
+ processCityDocument(doc, opt.get());
} else {
LOG.debug("'{}' IP address not found in City DB.", serverIp);
}
return doc;
}
- private static NutchDocument processDocument(NutchDocument doc, CityResponse response) {
+ private static NutchDocument processCityDocument(NutchDocument doc, CityResponse response) {
City city = response.getCity();
addIfNotNull(doc, "cityName", city.getName());
addIfNotNull(doc, "cityConfidence", city.getConfidence());
@@ -205,7 +231,7 @@ public class GeoIPDocumentCreator {
addIfNotNull(doc, "mobileCountryCode", traits.getMobileCountryCode());
addIfNotNull(doc, "mobileNetworkCode", traits.getMobileNetworkCode());
if (traits.getNetwork() != null) {
- addIfNotNull(doc, NETWORK_ADDRESS, traits.getNetwork().toString());
+ addIfNotNull(doc, CITY_NETWORK_ADDRESS, traits.getNetwork().toString());
}
addIfNotNull(doc, "organization", traits.getOrganization());
addIfNotNull(doc, "staticIpScore", traits.getStaticIpScore());
@@ -238,12 +264,12 @@ public class GeoIPDocumentCreator {
.getByName(serverIp));
if (opt.isPresent()) {
ConnectionTypeResponse response = opt.get();
- addIfNotNull(doc, "ip", response.getIpAddress());
+ addIfNotDuplicate(doc, "ip", response.getIpAddress());
if (response.getConnectionType() != null) {
addIfNotNull(doc, "connectionType", response.getConnectionType().toString());
}
if (response.getNetwork() != null) {
- addIfNotNull(doc, NETWORK_ADDRESS, response.getNetwork().toString());
+ addIfNotNull(doc, CONNECTION_NETWORK_ADDRESS, response.getNetwork().toString());
}
} else {
LOG.debug("'{}' IP address not found in Connection DB.", serverIp);
@@ -251,6 +277,57 @@ public class GeoIPDocumentCreator {
return doc;
}
+ /**
+ * Populate a {@link org.apache.nutch.indexer.NutchDocument} based on lookup
+ * of IP in Country database. This is a lighter-weight alternative to the City
+ * database when only country-level information is needed.
+ * @param serverIp the server IP address to lookup
+ * @param doc NutchDocument to populate
+ * @param reader instantiated DatabaseReader object
+ * @return populated NutchDocument
+ * @throws IOException if an error occurs performing the Db lookup
+ * @throws GeoIp2Exception generic GeoIp2 exception
+ */
+ public static NutchDocument createDocFromCountryDb(String serverIp,
+ NutchDocument doc, DatabaseReader reader) throws IOException, GeoIp2Exception {
+ Optional<CountryResponse> opt = reader.tryCountry(InetAddress.getByName(serverIp));
+ if (opt.isPresent()) {
+ CountryResponse response = opt.get();
+ addIfNotDuplicate(doc, "ip", serverIp);
+
+ Continent continent = response.getContinent();
+ addIfNotDuplicate(doc, "continentCode", continent.getCode());
+ addIfNotDuplicate(doc, "continentGeoNameId", continent.getGeoNameId());
+ addIfNotDuplicate(doc, "continentName", continent.getName());
+
+ Country country = response.getRegisteredCountry();
+ addIfNotDuplicate(doc, "countryIsoCode", country.getIsoCode());
+ addIfNotDuplicate(doc, "countryName", country.getName());
+ addIfNotDuplicate(doc, "countryConfidence", country.getConfidence());
+ addIfNotDuplicate(doc, "countryGeoNameId", country.getGeoNameId());
+ addIfNotDuplicate(doc, "countryInEuropeanUnion", country.isInEuropeanUnion());
+
+ RepresentedCountry rCountry = response.getRepresentedCountry();
+ addIfNotDuplicate(doc, "countryType", rCountry.getType());
+
+ Traits traits = response.getTraits();
+ if (traits.getNetwork() != null) {
+ addIfNotNull(doc, COUNTRY_NETWORK_ADDRESS, traits.getNetwork().toString());
+ }
+ addIfNotDuplicate(doc, "isAnonymous", traits.isAnonymous());
+ addIfNotDuplicate(doc, "isAnonymousVpn", traits.isAnonymousVpn());
+ addIfNotDuplicate(doc, "isAnycast", traits.isAnycast());
+ addIfNotDuplicate(doc, "isHostingProvider", traits.isHostingProvider());
+ addIfNotDuplicate(doc, "isLegitimateProxy", traits.isLegitimateProxy());
+ addIfNotDuplicate(doc, "isPublicProxy", traits.isPublicProxy());
+ addIfNotDuplicate(doc, "isResidentialProxy", traits.isResidentialProxy());
+ addIfNotDuplicate(doc, "isTorExitNode", traits.isTorExitNode());
+ } else {
+ LOG.debug("'{}' IP address not found in Country DB.", serverIp);
+ }
+ return doc;
+ }
+
/**
* Populate a {@link org.apache.nutch.indexer.NutchDocument} based on lookup
* of IP in Domain database.
@@ -266,9 +343,9 @@ public class GeoIPDocumentCreator {
Optional<DomainResponse> opt = reader.tryDomain(InetAddress.getByName(serverIp));
if (opt.isPresent()) {
DomainResponse response = opt.get();
- addIfNotNull(doc, "ip", response.getIpAddress());
+ addIfNotDuplicate(doc, "ip", response.getIpAddress());
addIfNotNull(doc, "domain", response.getDomain());
- addIfNotNull(doc, NETWORK_ADDRESS, response.getNetwork().toString());
+ addIfNotNull(doc, DOMAIN_NETWORK_ADDRESS, response.getNetwork().toString());
} else {
LOG.debug("'{}' IP address not found in Domain DB.", serverIp);
}
@@ -287,7 +364,7 @@ public class GeoIPDocumentCreator {
*/
public static NutchDocument createDocFromInsightsService(String serverIp,
NutchDocument doc, WebServiceClient client) throws IOException, GeoIp2Exception {
- addIfNotNull(doc, "ip", serverIp);
+ addIfNotDuplicate(doc, "ip", serverIp);
return processInsightsDocument(doc, client.insights(InetAddress.getByName(serverIp)));
}
@@ -347,7 +424,7 @@ public class GeoIPDocumentCreator {
addIfNotNull(doc, "mobileCountryCode", traits.getMobileCountryCode());
addIfNotNull(doc, "mobileNetworkCode", traits.getMobileNetworkCode());
if (traits.getNetwork() != null) {
- addIfNotNull(doc, NETWORK_ADDRESS, traits.getNetwork().toString());
+ addIfNotNull(doc, INSIGHTS_NETWORK_ADDRESS, traits.getNetwork().toString());
}
addIfNotNull(doc, "organization", traits.getOrganization());
addIfNotNull(doc, "staticIpScore", traits.getStaticIpScore());
@@ -379,7 +456,7 @@ public class GeoIPDocumentCreator {
Optional<IspResponse> opt = reader.tryIsp(InetAddress.getByName(serverIp));
if (opt.isPresent()) {
IspResponse response = opt.get();
- addIfNotNull(doc, "ip", response.getIpAddress());
+ addIfNotDuplicate(doc, "ip", response.getIpAddress());
addIfNotNull(doc, "autonSystemNum", response.getAutonomousSystemNumber());
addIfNotNull(doc, "autonSystemOrg", response.getAutonomousSystemOrganization());
addIfNotNull(doc, "isp", response.getIsp());
diff --git a/src/plugin/index-geoip/src/java/org/apache/nutch/indexer/geoip/GeoIPIndexingFilter.java b/src/plugin/index-geoip/src/java/org/apache/nutch/indexer/geoip/GeoIPIndexingFilter.java
index 316446b89..ce50b71cc 100644
--- a/src/plugin/index-geoip/src/java/org/apache/nutch/indexer/geoip/GeoIPIndexingFilter.java
+++ b/src/plugin/index-geoip/src/java/org/apache/nutch/indexer/geoip/GeoIPIndexingFilter.java
@@ -60,6 +60,7 @@ import com.maxmind.geoip2.exception.GeoIp2Exception;
* <li><code>index.geoip.db.asn</code> - ASN database</li>
* <li><code>index.geoip.db.city</code> - City database</li>
* <li><code>index.geoip.db.connection</code> - Connection Type database</li>
+ * <li><code>index.geoip.db.country</code> - Country database</li>
* <li><code>index.geoip.db.domain</code> - Domain database</li>
* <li><code>index.geoip.db.isp</code> - ISP database</li>
* </ul>
@@ -81,6 +82,7 @@ public class GeoIPIndexingFilter implements IndexingFilter, Closeable {
ASN("asn"),
CITY("city"),
CONNECTION("connection"),
+ COUNTRY("country"),
DOMAIN("domain"),
ISP("isp");
@@ -226,6 +228,8 @@ public class GeoIPIndexingFilter implements IndexingFilter, Closeable {
return GeoIPDocumentCreator.createDocFromCityDb(serverIp, doc, reader);
case CONNECTION:
return GeoIPDocumentCreator.createDocFromConnectionDb(serverIp, doc, reader);
+ case COUNTRY:
+ return GeoIPDocumentCreator.createDocFromCountryDb(serverIp, doc, reader);
case DOMAIN:
return GeoIPDocumentCreator.createDocFromDomainDb(serverIp, doc, reader);
case ISP:
diff --git a/src/plugin/index-geoip/src/test/org/apache/nutch/indexer/geoip/TestGeoIPIndexingFilter.java b/src/plugin/index-geoip/src/test/org/apache/nutch/indexer/geoip/TestGeoIPIndexingFilter.java
index f7181e285..cd75559ee 100644
--- a/src/plugin/index-geoip/src/test/org/apache/nutch/indexer/geoip/TestGeoIPIndexingFilter.java
+++ b/src/plugin/index-geoip/src/test/org/apache/nutch/indexer/geoip/TestGeoIPIndexingFilter.java
@@ -18,6 +18,7 @@ package org.apache.nutch.indexer.geoip;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
@@ -26,6 +27,7 @@ import org.apache.hadoop.io.Text;
import org.apache.nutch.crawl.CrawlDatum;
import org.apache.nutch.crawl.Inlinks;
import org.apache.nutch.indexer.NutchDocument;
+import org.apache.nutch.indexer.NutchField;
import org.apache.nutch.parse.ParseData;
import org.apache.nutch.parse.ParseImpl;
import org.apache.nutch.util.NutchConfiguration;
@@ -163,7 +165,7 @@ public class TestGeoIPIndexingFilter {
assertNotNull(doc);
assertEquals("Cable/DSL", doc.getFieldValue("connectionType"));
assertNotNull(doc.getFieldValue("ip"));
- assertNotNull(doc.getFieldValue("networkAddress"));
+ assertNotNull(doc.getFieldValue(GeoIPDocumentCreator.CONNECTION_NETWORK_ADDRESS));
}
/**
@@ -275,4 +277,154 @@ public class TestGeoIPIndexingFilter {
fail(e.getMessage());
}
}
+
+ /**
+ * Test addIfNotDuplicate method adds value when field doesn't exist.
+ */
+ @Test
+ public void testAddIfNotDuplicateNewField() {
+ NutchDocument testDoc = new NutchDocument();
+ GeoIPDocumentCreator.addIfNotDuplicate(testDoc, "ip", "192.168.1.1");
+
+ assertNotNull(testDoc.getFieldValue("ip"));
+ assertEquals("192.168.1.1", testDoc.getFieldValue("ip"));
+ assertEquals(1, testDoc.getField("ip").getValues().size());
+ }
+
+ /**
+ * Test addIfNotDuplicate method doesn't add duplicate values.
+ */
+ @Test
+ public void testAddIfNotDuplicatePreventsDuplicates() {
+ NutchDocument testDoc = new NutchDocument();
+
+ // Add the same IP three times (simulating multiple database lookups)
+ GeoIPDocumentCreator.addIfNotDuplicate(testDoc, "ip", "192.168.1.1");
+ GeoIPDocumentCreator.addIfNotDuplicate(testDoc, "ip", "192.168.1.1");
+ GeoIPDocumentCreator.addIfNotDuplicate(testDoc, "ip", "192.168.1.1");
+
+ // Should only have one value
+ NutchField field = testDoc.getField("ip");
+ assertNotNull(field);
+ assertEquals(1, field.getValues().size());
+ assertEquals("192.168.1.1", field.getValues().get(0));
+ }
+
+ /**
+ * Test addIfNotDuplicate method allows different values.
+ */
+ @Test
+ public void testAddIfNotDuplicateAllowsDifferentValues() {
+ NutchDocument testDoc = new NutchDocument();
+
+ // Add different IPs (this shouldn't happen in practice but tests the logic)
+ GeoIPDocumentCreator.addIfNotDuplicate(testDoc, "ip", "192.168.1.1");
+ GeoIPDocumentCreator.addIfNotDuplicate(testDoc, "ip", "192.168.1.2");
+
+ // Should have two different values
+ NutchField field = testDoc.getField("ip");
+ assertNotNull(field);
+ assertEquals(2, field.getValues().size());
+ assertTrue(field.getValues().contains("192.168.1.1"));
+ assertTrue(field.getValues().contains("192.168.1.2"));
+ }
+
+ /**
+ * Test addIfNotDuplicate method handles null values gracefully.
+ */
+ @Test
+ public void testAddIfNotDuplicateNullValue() {
+ NutchDocument testDoc = new NutchDocument();
+
+ // Adding null should not create a field
+ GeoIPDocumentCreator.addIfNotDuplicate(testDoc, "ip", null);
+
+ assertNull(testDoc.getField("ip"));
+ }
+
+ /**
+ * Test that database-specific network address field names are used correctly.
+ * Connection Type database should use connectionNetworkAddress field.
+ */
+ @Test
+ public void testConnectionDatabaseSpecificNetworkAddressField() {
+ conf.set("index.geoip.db.connection", "GeoIP2-Connection-Type-Test.mmdb");
+ filter = new GeoIPIndexingFilter();
+ filter.setConf(conf);
+ parseImpl.getData().getContentMeta().add("_ip_", "1.0.0.1");
+ try {
+ filter.filter(doc, parseImpl, text, crawlDatum, inlinks);
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ }
+ assertNotNull(doc);
+ // Verify that database-specific network address field is used
+ assertNotNull(doc.getFieldValue(GeoIPDocumentCreator.CONNECTION_NETWORK_ADDRESS));
+ // Verify that the old generic field is NOT used
+ assertNull(doc.getField("networkAddress"));
+ }
+
+ /**
+ * Test that multiple databases use their own specific network address fields
+ * and that IP address is not duplicated.
+ */
+ @Test
+ public void testMultipleDatabasesUseSpecificNetworkAddressFields() {
+ // Configure both City and Connection Type databases
+ conf.set("index.geoip.db.city", "GeoIP2-City-Test.mmdb");
+ conf.set("index.geoip.db.connection", "GeoIP2-Connection-Type-Test.mmdb");
+ filter = new GeoIPIndexingFilter();
+ filter.setConf(conf);
+
+ // Use IP 1.0.0.1 which exists in Connection Type test database
+ parseImpl.getData().getContentMeta().add("_ip_", "1.0.0.1");
+ try {
+ filter.filter(doc, parseImpl, text, crawlDatum, inlinks);
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ }
+ assertNotNull(doc);
+
+ // Verify connection type data is present with specific network address field
+ assertEquals("Cable/DSL", doc.getFieldValue("connectionType"));
+ assertNotNull(doc.getFieldValue(GeoIPDocumentCreator.CONNECTION_NETWORK_ADDRESS));
+
+ // Verify the generic networkAddress field is NOT used
+ assertNull(doc.getField("networkAddress"));
+
+ // Verify IP field exists and has only one value (not duplicated)
+ NutchField ipField = doc.getField("ip");
+ assertNotNull(ipField);
+ assertEquals(1, ipField.getValues().size(),
+ "IP field should have exactly one value, not duplicated across databases");
+ }
+
+ /**
+ * Test that Country database configuration property is correctly recognized.
+ * Note: This test verifies the configuration works. A full test would require
+ * a GeoIP2-Country-Test.mmdb file which is not currently available in the
+ * test resources. The City database can be used for country-level lookups
+ * in production as it contains the same country data.
+ */
+ @Test
+ public void testCountryDatabaseConfigProperty() {
+ // Verify the country property is recognized (won't load without actual file)
+ conf.set("index.geoip.db.country", "nonexistent-country-test.mmdb");
+ filter = new GeoIPIndexingFilter();
+ filter.setConf(conf);
+ // Configuration should be accepted without error (file missing is logged as warning)
+ assertEquals("nonexistent-country-test.mmdb",
+ filter.getConf().get("index.geoip.db.country"));
+ }
+
+ /**
+ * Test Country database specific network address field constant is defined.
+ */
+ @Test
+ public void testCountryNetworkAddressFieldConstant() {
+ // Verify the COUNTRY_NETWORK_ADDRESS constant is properly defined
+ assertEquals("countryNetworkAddress", GeoIPDocumentCreator.COUNTRY_NETWORK_ADDRESS);
+ }
}
diff --git a/src/plugin/indexer-solr/schema.xml b/src/plugin/indexer-solr/schema.xml
index a30e9287d..17f77534c 100644
--- a/src/plugin/indexer-solr/schema.xml
+++ b/src/plugin/indexer-solr/schema.xml
@@ -355,7 +355,15 @@
<!-- fields for index-geoip plugin -->
<!-- Common fields -->
<field name="ip" type="string" stored="true" indexed="true"/>
- <field name="networkAddress" type="string" stored="true" indexed="true"/>
+
+ <!-- Database-specific network address fields -->
+ <field name="anonymousNetworkAddress" type="string" stored="true" indexed="true"/>
+ <field name="asnNetworkAddress" type="string" stored="true" indexed="true"/>
+ <field name="cityNetworkAddress" type="string" stored="true" indexed="true"/>
+ <field name="connectionNetworkAddress" type="string" stored="true" indexed="true"/>
+ <field name="countryNetworkAddress" type="string" stored="true" indexed="true"/>
+ <field name="domainNetworkAddress" type="string" stored="true" indexed="true"/>
+ <field name="insightsNetworkAddress" type="string" stored="true" indexed="true"/>
<!-- City database fields -->
<field name="cityName" type="string" stored="true" indexed="true"/>