Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

Updated Arbitrary Indexer that passes all indexer constructor args to user's POJO instance.

Joe Gilvaryd2adde295010
diff --git a/conf/nutch-default.xml b/conf/nutch-default.xml
index a8a953f75..e79e34aa4 100644
--- a/conf/nutch-default.xml
+++ b/conf/nutch-default.xml
@@ -2342,6 +2342,16 @@ CAUTION: Set the parser.timeout to -1 or a bigger value than 30, when using this
<value></value>
</property>
+<property>
+ <name>index.arbitrary.all.fields.access</name>
+ <description>Whether to pass all indexing filter fields to constructor
+ for the user POJO. These are the NutchDocument doc, Parse parse,
+ url (as org.apache.hadoop.io.Text), CrawlDatum datum, and
+ Inlinks inlinks objects. Default value is false to preserve
+ compatibility with previous index-arbitrary behavior.</description>
+ <value></value>
+</property>
+
<!-- parse-metatags plugin properties -->
<property>
<name>metatags.names</name>
diff --git a/src/plugin/index-arbitrary/src/java/org/apache/nutch/indexer/arbitrary/ArbitraryIndexingFilter.java b/src/plugin/index-arbitrary/src/java/org/apache/nutch/indexer/arbitrary/ArbitraryIndexingFilter.java
index 6fd6a3ac7..4b6996511 100644
--- a/src/plugin/index-arbitrary/src/java/org/apache/nutch/indexer/arbitrary/ArbitraryIndexingFilter.java
+++ b/src/plugin/index-arbitrary/src/java/org/apache/nutch/indexer/arbitrary/ArbitraryIndexingFilter.java
@@ -118,7 +118,13 @@ public class ArbitraryIndexingFilter implements IndexingFilter {
* NutchDocument fieldName if this is set to true. Default behavior is to
* add the value from calling methodName to existing values for fieldName. */
private boolean overwrite = false;
-
+
+ /** Optional flag to determine whether to pass all inputs to the filter
+ * method in the plugin to the POJO class. Default is false to preserve
+ * backward compatibility with earlier plugin. Note that the url is still
+ * included in the POJO constructor. */
+ private boolean allFieldsAccess = false;
+
/** Hadoop Configuration object to pass these values into the plugin. */
private Configuration conf;
@@ -180,7 +186,16 @@ public class ArbitraryIndexingFilter implements IndexingFilter {
} else {
theMethod = theClass.getMethod(methodName);
}
- theConstructor = theClass.getDeclaredConstructor(String[].class);
+ if (allFieldsAccess) {
+ theConstructor = theClass.getDeclaredConstructor(String[].class,
+ NutchDocument.class,
+ Parse.class,
+ Text.class,
+ CrawlDatum.class,
+ Inlinks.class);
+ } else {
+ theConstructor = theClass.getDeclaredConstructor(String[].class);
+ }
} catch (Exception e) {
LOG.error("Exception preparing reflection tasks. className was {}",
String.valueOf(className));
@@ -189,9 +204,20 @@ public class ArbitraryIndexingFilter implements IndexingFilter {
}
try {
constrArgs = new String[userConstrArgs.length + 1];
- constrArgs[0] = url.toString();
System.arraycopy(userConstrArgs,0,constrArgs,1,userConstrArgs.length);
- instance = theConstructor.newInstance(new Object[]{constrArgs});
+ if (allFieldsAccess) {
+ instance = theConstructor.newInstance(constrArgs,
+ doc,
+ parse,
+ url,
+ datum,
+ inlinks);
+ } else {
+ constrArgs[0] = url.toString();
+ //System.arraycopy(userConstrArgs,0,constrArgs,1,userConstrArgs.length);
+ instance = theConstructor.newInstance(new Object[]{constrArgs});
+ }
+
if (methodArgs.length > 0) {
result = theMethod.invoke(instance, new Object[]{methodArgs});
} else {
@@ -201,10 +227,10 @@ public class ArbitraryIndexingFilter implements IndexingFilter {
LOG.error("Exception in reflection trying to instantiate/invoke. "
+ "url was {} & className was {}",
String.valueOf(url), String.valueOf(className));
- if (constrArgs.length > 0) {
+ if (constrArgs.length > 1) {
LOG.error("constrArgs[1] was {}", String.valueOf(constrArgs[1]));
}
- LOG.error("methodName was {}", String.valueOf(className));
+ LOG.error("methodName was {}", String.valueOf(methodName));
if (methodArgs.length > 0) {
LOG.error("methodArgs[0] was {}", String.valueOf(methodArgs[0]));
}
@@ -243,6 +269,8 @@ public class ArbitraryIndexingFilter implements IndexingFilter {
this.conf = conf;
arbitraryAddsCount = conf.getInt("index.arbitrary.function.count",1);
LOG.info("Will process the first {} fieldName defs in config.", String.valueOf(arbitraryAddsCount));
+ allFieldsAccess = conf.getBoolean("index.arbitrary.all.fields.access",false);
+ LOG.info("POJO classes will " + (allFieldsAccess ? "" : "not ") + "have access to all filter constructor args.");
}
/**
diff --git a/src/plugin/index-arbitrary/src/test/org/apache/nutch/indexer/arbitrary/PopularityGauge.java b/src/plugin/index-arbitrary/src/test/org/apache/nutch/indexer/arbitrary/PopularityGauge.java
new file mode 100644
index 000000000..6abb5cf7f
--- /dev/null
+++ b/src/plugin/index-arbitrary/src/test/org/apache/nutch/indexer/arbitrary/PopularityGauge.java
@@ -0,0 +1,69 @@
+package org.apache.nutch.indexer.arbitrary;
+
+import org.apache.nutch.parse.Parse;
+import org.apache.nutch.indexer.NutchDocument;
+import org.apache.nutch.crawl.CrawlDatum;
+import org.apache.nutch.crawl.Inlinks;
+import org.apache.hadoop.io.Text;
+
+import java.io.PrintStream;
+import java.util.Set;
+import java.util.HashSet;
+import java.util.Arrays;
+
+public class PopularityGauge {
+
+ private static PrintStream out = System.out;
+ private String words;
+ private NutchDocument doc;
+ private Parse parse;
+ private CrawlDatum datum;
+ private Inlinks inlinks;
+ private double popularityBoost;
+
+ public PopularityGauge(String args[],
+ NutchDocument docIn,
+ Parse parseIn,
+ Text urlIn,
+ CrawlDatum datumIn,
+ Inlinks inlinksIn){
+ super();
+ doc = docIn;
+ inlinks = inlinksIn;
+ datum = datumIn;
+ }
+
+ public double getPopularityBoost() {
+ popularityBoost = (Double) doc.getFieldValue("popularityBoost");
+ if (datum.getStatus() == CrawlDatum.STATUS_FETCH_SUCCESS) {
+ Set anchorSet = new HashSet<>(Arrays.asList(inlinks.getAnchors()));
+ if(anchorSet.contains("dinosaur")){
+ popularityBoost = popularityBoost + 0.50;
+ } else {
+ popularityBoost = popularityBoost - 0.20;
+ }
+ if (anchorSet.contains("baseball")) {
+ popularityBoost = popularityBoost + 0.25;
+ } else {
+ popularityBoost = popularityBoost - 0.15;
+ }
+ if (anchorSet.contains("source code")) {
+ popularityBoost = popularityBoost + 0.25;
+ } else {
+ popularityBoost = popularityBoost - 0.15;
+ }
+ }
+ return popularityBoost;
+ }
+
+ public static void main(String[] args,
+ NutchDocument doc,
+ Parse parse,
+ Text url,
+ CrawlDatum datum,
+ Inlinks inlinks) {
+
+ PopularityGauge popGauge = new PopularityGauge(args,doc,parse,url,datum,inlinks);
+ out.println(popGauge.getPopularityBoost());
+ }
+}
diff --git a/src/plugin/index-arbitrary/src/test/org/apache/nutch/indexer/arbitrary/TestArbitraryIndexingFilter.java b/src/plugin/index-arbitrary/src/test/org/apache/nutch/indexer/arbitrary/TestArbitraryIndexingFilter.java
index b8c92e168..e62027270 100644
--- a/src/plugin/index-arbitrary/src/test/org/apache/nutch/indexer/arbitrary/TestArbitraryIndexingFilter.java
+++ b/src/plugin/index-arbitrary/src/test/org/apache/nutch/indexer/arbitrary/TestArbitraryIndexingFilter.java
@@ -19,6 +19,7 @@ package org.apache.nutch.indexer.arbitrary;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.Text;
import org.apache.nutch.crawl.CrawlDatum;
+import org.apache.nutch.crawl.Inlink;
import org.apache.nutch.crawl.Inlinks;
import org.apache.nutch.indexer.NutchDocument;
import org.apache.nutch.parse.ParseImpl;
@@ -32,7 +33,9 @@ import org.junit.Test;
* value, supplement an existing field with an arbitrary value, and overwrite
* an existing field with an arbitrary value where it takes the arbitrary value
* from some POJO outside the normal Nutch codebase.
- *
+ * Further tests July 2025 to demonstrate conditionally setting values for
+ * arbitrary fields based on existing crawl data at the time filter() is
+ * called for indexing.
* @author Joe Gilvary
*/
@@ -273,4 +276,64 @@ public class TestArbitraryIndexingFilter {
Assert.assertTrue("field philosopher does not have new value 'last added value'", doc.getField("philosopher")
.getValues().contains("last added value"));
}
+
+
+ /**
+ * Test adding field with arbitrary content from POJO based on
+ * calculations using info already fetched and parsed during crawl.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testAddingNewCalculatedField() throws Exception {
+ conf = NutchConfiguration.create();
+ conf.set("index.arbitrary.function.count","1");
+ conf.set("index.arbitrary.all.fields.access","true");
+ conf.set("index.arbitrary.constructorArgs.0","");
+ conf.set("index.arbitrary.fieldName.0","popularityBoost");
+ conf.set("index.arbitrary.className.0","org.apache.nutch.indexer.arbitrary.PopularityGauge");
+ conf.set("index.arbitrary.methodName.0","getPopularityBoost");
+ conf.set("index.arbitrary.overwrite.0","true");
+
+ filter = new ArbitraryIndexingFilter();
+ Assert.assertNotNull("No filter exists for testAddingCalculatedNewField",filter);
+
+ filter.setConf(conf);
+ doc = new NutchDocument();
+
+ Double boostVal = new Double("1.0");
+ doc.add("popularityBoost", boostVal);
+ Assert.assertFalse("doc is empty", doc.getFieldNames().isEmpty());
+ Assert.assertTrue("test if doc has new field with arbitrary value", doc.getField("popularityBoost")
+ .getValues().contains(boostVal));
+
+ try {
+ filter.filter(doc, parse, url, crawlDatum, inlinks);
+ } catch (Exception e) {
+ e.printStackTrace();
+ Assert.fail(e.getMessage());
+ }
+
+ Assert.assertNotNull(doc);
+ Assert.assertFalse("doc is empty", doc.getFieldNames().isEmpty());
+ Assert.assertTrue("test if unfetched doc has nonzero value in popularityBoost", doc.getField("popularityBoost")
+ .getValues().contains(1.0));
+
+ inlinks.add(new Inlink("https://www.TeamSauropod.com/BullyForBrontosaurus","dinosaur"));
+ inlinks.add(new Inlink("https://github.com/apache","source code"));
+ inlinks.add(new Inlink("https://BanDH.com","baseball"));
+
+ crawlDatum.setStatus(CrawlDatum.STATUS_FETCH_SUCCESS);
+
+ try {
+ filter.filter(doc, parse, url, crawlDatum, inlinks);
+ } catch (Exception e) {
+ e.printStackTrace();
+ Assert.fail(e.getMessage());
+ }
+
+
+ Assert.assertTrue("test if successfully fetched doc has expected value in popularityBoost", doc.getField("popularityBoost")
+ .getValues().contains(2.0));
+ }
}