Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

fix for NUTCH-1403 contributed by aalbahem

Ameer Albahem598bbc40a3d3
diff --git a/build.xml b/build.xml
index 5d883e3df..9d9eb739b 100644
--- a/build.xml
+++ b/build.xml
@@ -226,6 +226,7 @@
<packageset dir="${plugins.dir}/scoring-opic/src/java"/>
<packageset dir="${plugins.dir}/scoring-orphan/src/java"/>
<packageset dir="${plugins.dir}/scoring-similarity/src/java"/>
+ <packageset dir="${plugins.dir}/scoring-metadata/src/java"/>
<packageset dir="${plugins.dir}/subcollection/src/java"/>
<packageset dir="${plugins.dir}/tld/src/java"/>
<packageset dir="${plugins.dir}/urlfilter-automaton/src/java"/>
@@ -724,6 +725,7 @@
<packageset dir="${plugins.dir}/scoring-opic/src/java"/>
<packageset dir="${plugins.dir}/scoring-orphan/src/java"/>
<packageset dir="${plugins.dir}/scoring-similarity/src/java"/>
+ <packageset dir="${plugins.dir}/scoring-metadata/src/java"/>
<packageset dir="${plugins.dir}/subcollection/src/java"/>
<packageset dir="${plugins.dir}/tld/src/java"/>
<packageset dir="${plugins.dir}/urlfilter-automaton/src/java"/>
@@ -1143,6 +1145,8 @@
<source path="${plugins.dir}/scoring-orphan/src/java"/>
<source path="${plugins.dir}/scoring-orphan/src/test"/>
<source path="${plugins.dir}/scoring-similarity/src/java/" />
+ <source path="${plugins.dir}/scoring-metadata/src/java/" />
+ <source path="${plugins.dir}/scoring-metadata/src/test" />
<source path="${plugins.dir}/subcollection/src/java/" />
<source path="${plugins.dir}/subcollection/src/test/" />
<source path="${plugins.dir}/tld/src/java/" />
diff --git a/src/plugin/build.xml b/src/plugin/build.xml
index 51c3fe781..35fdb3aae 100755
--- a/src/plugin/build.xml
+++ b/src/plugin/build.xml
@@ -84,6 +84,7 @@
<ant dir="scoring-opic" target="deploy"/>
<ant dir="scoring-orphan" target="deploy"/>
<ant dir="scoring-similarity" target="deploy"/>
+ <ant dir="scoring-metadata" target="deploy"/>
<ant dir="subcollection" target="deploy"/>
<ant dir="tld" target="deploy"/>
<ant dir="urlfilter-automaton" target="deploy"/>
@@ -143,6 +144,7 @@
<ant dir="protocol-httpclient" target="test"/>
<ant dir="protocol-okhttp" target="test"/>
<ant dir="scoring-orphan" target="test"/>
+ <ant dir="scoring-metadata" target="test"/>
<ant dir="subcollection" target="test"/>
<ant dir="urlfilter-automaton" target="test"/>
<ant dir="urlfilter-domain" target="test"/>
@@ -231,6 +233,7 @@
<ant dir="scoring-opic" target="clean"/>
<ant dir="scoring-orphan" target="clean"/>
<ant dir="scoring-similarity" target="clean"/>
+ <ant dir="scoring-metadata" target="clean"/>
<ant dir="subcollection" target="clean"/>
<ant dir="tld" target="clean"/>
<ant dir="urlfilter-automaton" target="clean"/>
diff --git a/src/plugin/scoring-metadata/build.xml b/src/plugin/scoring-metadata/build.xml
new file mode 100644
index 000000000..4f62ed1fa
--- /dev/null
+++ b/src/plugin/scoring-metadata/build.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<project name="scoring-metadata" default="jar-core">
+
+ <import file="../build-plugin.xml"/>
+</project>
diff --git a/src/plugin/scoring-metadata/ivy.xml b/src/plugin/scoring-metadata/ivy.xml
new file mode 100644
index 000000000..24d76063d
--- /dev/null
+++ b/src/plugin/scoring-metadata/ivy.xml
@@ -0,0 +1,41 @@
+<?xml version="1.0" ?>
+
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+
+<ivy-module version="1.0">
+ <info organisation="org.apache.nutch" module="${ant.project.name}">
+ <license name="Apache 2.0"/>
+ <ivyauthor name="Apache Nutch Team" url="http://nutch.apache.org"/>
+ <description>
+ Apache Nutch
+ </description>
+ </info>
+
+ <configurations>
+ <include file="../../../ivy/ivy-configurations.xml"/>
+ </configurations>
+
+ <publications>
+ <!--get the artifact from our module name-->
+ <artifact conf="master"/>
+ </publications>
+
+ <dependencies>
+ </dependencies>
+
+</ivy-module>
diff --git a/src/plugin/scoring-metadata/plugin.xml b/src/plugin/scoring-metadata/plugin.xml
new file mode 100644
index 000000000..ca47e3714
--- /dev/null
+++ b/src/plugin/scoring-metadata/plugin.xml
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<plugin
+ id="scoring-metadata"
+ name="Metadata Scoring Filter"
+ version="1.0.0"
+ provider-name="nutch">
+
+
+ <runtime>
+ <library name="scoring-metadata.jar">
+ <export name="*"/>
+ </library>
+ </runtime>
+
+ <requires>
+ <import plugin="nutch-extensionpoints"/>
+ </requires>
+
+ <extension id="org.apache.nutch.scoring.metadata"
+ name="Metadata Scoring Filter"
+ point="org.apache.nutch.scoring.ScoringFilter">
+ <implementation id="scoring-metadata"
+ class="org.apache.nutch.scoring.metadata.MetadataScoringFilter" />
+ </extension>
+</plugin>
diff --git a/src/plugin/scoring-metadata/pom.xml b/src/plugin/scoring-metadata/pom.xml
new file mode 100644
index 000000000..e6fa1552e
--- /dev/null
+++ b/src/plugin/scoring-metadata/pom.xml
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.nutch</groupId>
+ <artifactId>scoring-metadata</artifactId>
+ <version>1.0-SNAPSHOT</version>
+
+
+ <properties>
+ <nutch.version>1.15</nutch.version>
+ <!-- Considering src/app will have all your sourcr java code -->
+ <project.build.sourceDirectory>src/java</project.build.sourceDirectory>
+ <!-- Considering all your junit tests are in src/test/unit -->
+ <project.build.testSourceDirectory>src/test</project.build.testSourceDirectory>
+
+ </properties>
+
+
+ <dependencies>
+
+ <!-- https://mvnrepository.com/artifact/org.apache.nutch/nutch -->
+ <dependency>
+ <groupId>org.apache.nutch</groupId>
+ <artifactId>nutch</artifactId>
+ <version>${nutch.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.13-beta-3</version>
+ <scope>test</scope>
+ </dependency>
+
+ </dependencies>
+</project>
\ No newline at end of file
diff --git a/src/plugin/scoring-metadata/src/java/org/apache/nutch/scoring/metadata/MetadataScoringFilter.java b/src/plugin/scoring-metadata/src/java/org/apache/nutch/scoring/metadata/MetadataScoringFilter.java
new file mode 100644
index 000000000..f34729479
--- /dev/null
+++ b/src/plugin/scoring-metadata/src/java/org/apache/nutch/scoring/metadata/MetadataScoringFilter.java
@@ -0,0 +1,178 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nutch.scoring.metadata;
+
+import java.util.Collection;
+import java.util.Map.Entry;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.conf.Configured;
+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.parse.Parse;
+import org.apache.nutch.parse.ParseData;
+import org.apache.nutch.protocol.Content;
+import org.apache.nutch.scoring.ScoringFilter;
+import org.apache.nutch.scoring.ScoringFilterException;
+
+
+/**
+ * For documentation:
+ *
+ * {@link org.apache.nutch.scoring.metadata}
+ */
+public class MetadataScoringFilter extends Configured implements ScoringFilter {
+
+ public static final String METADATA_DATUM = "scoring.db.md";
+ public static final String METADATA_CONTENT = "scoring.content.md";
+ public static final String METADATA_PARSED = "scoring.parse.md";
+ private static String[] datumMetadata;
+ private static String[] contentMetadata;
+ private static String[] parseMetadata;
+ private Configuration conf;
+
+ /**
+ * This will take the metadata that you have listed in your "scoring.parse.md"
+ * property, and looks for them inside the parseData object. If they exist,
+ * this will be propagated into your 'targets' Collection's ["outlinks"]
+ * attributes.
+ *
+ * @see ScoringFilter#distributeScoreToOutlinks
+ */
+ public CrawlDatum distributeScoreToOutlinks(Text fromUrl,
+ ParseData parseData, Collection<Entry<Text, CrawlDatum>> targets,
+ CrawlDatum adjust, int allCount) throws ScoringFilterException {
+ if (parseMetadata == null || targets == null || parseData == null)
+ return adjust;
+
+ Iterator<Entry<Text, CrawlDatum>> targetIterator = targets.iterator();
+
+ while (targetIterator.hasNext()) {
+ Entry<Text, CrawlDatum> nextTarget = targetIterator.next();
+
+ for (String meta : parseMetadata) {
+ String metaFromParse = parseData.getMeta(meta);
+
+ if (metaFromParse == null)
+ continue;
+
+ nextTarget.getValue().getMetaData()
+ .put(new Text(meta), new Text(metaFromParse));
+ }
+ }
+ return adjust;
+ }
+
+ /**
+ * Takes the metadata, specified in your "scoring.db.md" property, from the
+ * datum object and injects it into the content. This is transfered to the
+ * parseData object.
+ *
+ * @see ScoringFilter#passScoreBeforeParsing
+ * @see MetadataScoringFilter#passScoreAfterParsing
+ */
+ public void passScoreBeforeParsing(Text url, CrawlDatum datum, Content content) {
+ if (datumMetadata == null || content == null || datum == null)
+ return;
+
+ for (String meta : datumMetadata) {
+ Text metaFromDatum = (Text) datum.getMetaData().get(new Text(meta));
+
+ if (metaFromDatum == null) {
+ continue;
+ }
+
+ content.getMetadata().set(meta, metaFromDatum.toString());
+ }
+ }
+
+ /**
+ * Takes the metadata, which was lumped inside the content, and replicates it
+ * within your parse data.
+ *
+ * @see MetadataScoringFilter#passScoreBeforeParsing
+ * @see ScoringFilter#passScoreAfterParsing
+ */
+ public void passScoreAfterParsing(Text url, Content content, Parse parse) {
+ if (contentMetadata == null || content == null || parse == null)
+ return;
+
+ for (String meta : contentMetadata) {
+ String metaFromContent = content.getMetadata().get(meta);
+
+ if (metaFromContent == null)
+ continue;
+
+ parse.getData().getParseMeta().set(meta, metaFromContent);
+ }
+ }
+
+ /** Boilerplate */
+ public float generatorSortValue(Text url, CrawlDatum datum, float initSort)
+ throws ScoringFilterException {
+ return initSort;
+ }
+
+ /** Boilerplate */
+ public float indexerScore(Text url, NutchDocument doc, CrawlDatum dbDatum,
+ CrawlDatum fetchDatum, Parse parse, Inlinks inlinks, float initScore)
+ throws ScoringFilterException {
+ return initScore;
+ }
+
+ /** Boilerplate */
+ public void initialScore(Text url, CrawlDatum datum)
+ throws ScoringFilterException {
+ return;
+ }
+
+ /** Boilerplate */
+ public void injectedScore(Text url, CrawlDatum datum)
+ throws ScoringFilterException {
+ return;
+ }
+
+ /** Boilerplate */
+ public void updateDbScore(Text url, CrawlDatum old, CrawlDatum datum,
+ List<CrawlDatum> inlinked) throws ScoringFilterException {
+ return;
+ }
+
+ /**
+ * handles conf assignment and pulls the value assignment from the
+ * "urlmeta.tags" property
+ */
+ public void setConf(Configuration conf) {
+ super.setConf(conf);
+
+ if (conf == null)
+ return;
+
+ datumMetadata = conf.getStrings(METADATA_DATUM);
+ contentMetadata = conf.getStrings(METADATA_CONTENT);
+ parseMetadata = conf.getStrings(METADATA_PARSED);
+ }
+
+ /** Boilerplate */
+ public Configuration getConf() {
+ return conf;
+ }
+}
diff --git a/src/plugin/scoring-metadata/src/java/org/apache/nutch/scoring/metadata/package.html b/src/plugin/scoring-metadata/src/java/org/apache/nutch/scoring/metadata/package.html
new file mode 100644
index 000000000..dc30ec2bf
--- /dev/null
+++ b/src/plugin/scoring-metadata/src/java/org/apache/nutch/scoring/metadata/package.html
@@ -0,0 +1,10 @@
+<html>
+ <body>
+ <p>
+ Metadata Scoring Plugin
+ </p>
+ <p>
+ Propagates Meta data, injected from in db, parse data or content from one stage to another.
+ </p>
+ </body>
+</html>
diff --git a/src/plugin/scoring-metadata/src/test/org/apache/nutch/scoring/metadata/MetadataScoringFilterTest.java b/src/plugin/scoring-metadata/src/test/org/apache/nutch/scoring/metadata/MetadataScoringFilterTest.java
new file mode 100644
index 000000000..45b2ef42e
--- /dev/null
+++ b/src/plugin/scoring-metadata/src/test/org/apache/nutch/scoring/metadata/MetadataScoringFilterTest.java
@@ -0,0 +1,113 @@
+package org.apache.nutch.scoring.metadata;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.io.Text;
+import org.apache.nutch.crawl.CrawlDatum;
+import org.apache.nutch.parse.*;
+import org.apache.nutch.protocol.Content;
+import org.apache.nutch.scoring.ScoringFilterException;
+import org.apache.nutch.util.NutchConfiguration;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.HashMap;
+
+public class MetadataScoringFilterTest {
+
+ @Before
+ public void setUp() throws Exception {
+
+ }
+
+ @Test
+ public void distributeScoreToOutlinks() throws ScoringFilterException {
+ Configuration conf = NutchConfiguration.create();
+ conf.set(MetadataScoringFilter.METADATA_PARSED,"parent,depth");
+
+ MetadataScoringFilter metadataScoringFilter = new MetadataScoringFilter();
+ metadataScoringFilter.setConf(conf);
+ CrawlDatum crawlDatum = new CrawlDatum();
+
+ Text from = new Text("https://nutch.apache.org/");
+ ParseData parseData = new ParseData();
+ String PARENT = "parent";
+ String DEPTH = "depth";
+
+ String parentMD = "https://nutch.apache.org/";
+ String depthMD = "1";
+ parseData.getParseMeta().add("parent",parentMD);
+ parseData.getParseMeta().add("depth",depthMD);
+
+ HashMap<Text,CrawlDatum> targets = new HashMap();
+ targets.put(new Text("https://nutch.apache.org/downloads.html"),new CrawlDatum());
+ targets.put(new Text("https://wiki.apache.org/nutch"),new CrawlDatum());
+
+ metadataScoringFilter.distributeScoreToOutlinks(from,parseData,targets.entrySet(),crawlDatum,2);
+
+ for (CrawlDatum outlink : targets.values()){
+ Text parent = (Text) outlink.getMetaData().get(new Text(PARENT));
+ Text depth = (Text) outlink.getMetaData().get(new Text(DEPTH));
+
+ Assert.assertEquals(parentMD,parent.toString());
+ Assert.assertEquals(depthMD,depth.toString());
+ }
+ }
+
+ @Test
+ public void passScoreBeforeParsing() {
+ Configuration conf = NutchConfiguration.create();
+ conf.set(MetadataScoringFilter.METADATA_DATUM,"parent,depth");
+
+ MetadataScoringFilter metadataScoringFilter = new MetadataScoringFilter();
+ metadataScoringFilter.setConf(conf);
+ CrawlDatum crawlDatum = new CrawlDatum();
+
+ Text from = new Text("https://nutch.apache.org/");
+
+ String PARENT = "parent";
+ String DEPTH = "depth";
+
+ String parentMD = "https://nutch.apache.org/";
+ String depthMD = "1";
+ crawlDatum.getMetaData().put(new Text(PARENT), new Text(parentMD));
+ crawlDatum.getMetaData().put(new Text(DEPTH), new Text(depthMD));
+ Content content = new Content();
+
+ metadataScoringFilter.passScoreBeforeParsing(from,crawlDatum,content);
+
+ Assert.assertEquals(parentMD,content.getMetadata().get(PARENT));
+ Assert.assertEquals(depthMD,content.getMetadata().get(DEPTH));
+ }
+
+ @Test
+ public void passScoreAfterParsing() {
+ Configuration conf = NutchConfiguration.create();
+ conf.set(MetadataScoringFilter.METADATA_DATUM,"parent,depth");
+ conf.set(MetadataScoringFilter.METADATA_CONTENT,"parent,depth");
+
+ MetadataScoringFilter metadataScoringFilter = new MetadataScoringFilter();
+ metadataScoringFilter.setConf(conf);
+ CrawlDatum crawlDatum = new CrawlDatum();
+
+ Text from = new Text("https://nutch.apache.org/");
+
+ String PARENT = "parent";
+ String DEPTH = "depth";
+
+ String parentMD = "https://nutch.apache.org/";
+ String depthMD = "1";
+ crawlDatum.getMetaData().put(new Text(PARENT), new Text(parentMD));
+ crawlDatum.getMetaData().put(new Text(DEPTH), new Text(depthMD));
+ Content content = new Content();
+ metadataScoringFilter.passScoreBeforeParsing(from,crawlDatum,content);
+
+ ParseData parseData = new ParseData(ParseStatus.STATUS_SUCCESS, null, null, content.getMetadata());
+ Parse parse = new ParseImpl(from.toString(),parseData);
+ metadataScoringFilter.passScoreAfterParsing(from,content,parse);
+
+
+ Assert.assertEquals(parentMD,parse.getData().getMeta(PARENT));
+ Assert.assertEquals(depthMD,parse.getData().getMeta(DEPTH));
+ }
+}