Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

Some improvements based on revewier's feedback.

Yossi Tamari36bfac1122f5
diff --git a/build.xml b/build.xml
index f5b3f78c4..2455f77b6 100644
--- a/build.xml
+++ b/build.xml
@@ -1042,6 +1042,7 @@
<source path="${plugins.dir}/indexer-elastic/src/test/" />
<source path="${plugins.dir}/indexer-elastic-rest/src/java/"/>
<source path="${plugins.dir}/index-jexl-filter/src/java/" />
+ <source path="${plugins.dir}/index-jexl-filter/src/test/" />
<source path="${plugins.dir}/index-metadata/src/java/" />
<source path="${plugins.dir}/index-more/src/java/" />
<source path="${plugins.dir}/index-more/src/test/" />
diff --git a/conf/nutch-default.xml b/conf/nutch-default.xml
index cab7adf3b..2df8ff085 100644
--- a/conf/nutch-default.xml
+++ b/conf/nutch-default.xml
@@ -1573,6 +1573,14 @@ visit https://wiki.apache.org/nutch/SimilarityScoringFilter-->
<value></value>
<description> A JEXL expression. If it evaluates to false,
the document will not be indexed.
+ Available primitives in the JEXL context:
+ * status, fetchTime, modifiedTime, retries, interval, score, signature, url, text, title
+ Available objects in the JEXL context:
+ * httpStatus - contains majorCode, minorCode, message
+ * documentMeta, contentMeta, parseMeta - contain all the Metadata properties.
+ each property value is always an array of Strings (so if you expect one value, use [0])
+ * doc - contains all the NutchFields from the NutchDocument.
+ each property value is always an array of Objects.
</description>
</property>
diff --git a/src/plugin/index-jexl-filter/src/java/org/apache/nutch/indexer/filter/JexlIndexingFilter.java b/src/plugin/index-jexl-filter/src/java/org/apache/nutch/indexer/filter/JexlIndexingFilter.java
index a2d3ce134..233ead3b3 100644
--- a/src/plugin/index-jexl-filter/src/java/org/apache/nutch/indexer/filter/JexlIndexingFilter.java
+++ b/src/plugin/index-jexl-filter/src/java/org/apache/nutch/indexer/filter/JexlIndexingFilter.java
@@ -38,7 +38,6 @@ import org.apache.nutch.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-
/**
* An {@link org.apache.nutch.indexer.IndexingFilter} that allows filtering of
* documents based on a JEXL expression.
@@ -52,56 +51,64 @@ public class JexlIndexingFilter implements IndexingFilter {
private Expression expr;
@Override
- public NutchDocument filter(NutchDocument doc, Parse parse, Text url, CrawlDatum datum, Inlinks inlinks) throws IndexingException {
- if (expr != null) {
- // Create a context and add data
- JexlContext jcontext = new MapContext();
-
- jcontext.set("status", CrawlDatum.getStatusName(datum.getStatus()));
- jcontext.set("fetchTime", (long)(datum.getFetchTime()));
- jcontext.set("modifiedTime", (long)(datum.getModifiedTime()));
- jcontext.set("retries", datum.getRetriesSinceFetch());
- jcontext.set("interval", new Integer(datum.getFetchInterval()));
- jcontext.set("score", datum.getScore());
- jcontext.set("signature", StringUtil.toHexString(datum.getSignature()));
- jcontext.set("url", url.toString());
-
- jcontext.set("text", parse.getText());
- jcontext.set("title", parse.getData().getTitle());
-
- JexlContext httpStatusContext = new MapContext();
- httpStatusContext.set("majorCode", parse.getData().getStatus().getMajorCode());
- httpStatusContext.set("minorCode", parse.getData().getStatus().getMinorCode());
- httpStatusContext.set("message", parse.getData().getStatus().getMessage());
- jcontext.set("httpStatus", httpStatusContext);
-
- jcontext.set("documentMeta", metadataToContext(doc.getDocumentMeta()));
- jcontext.set("contentMeta", metadataToContext(parse.getData().getContentMeta()));
- jcontext.set("parseMeta", metadataToContext(parse.getData().getParseMeta()));
-
- JexlContext context = new MapContext();
- for (Entry<String, NutchField> entry : doc) {
- context.set(entry.getKey(), entry.getValue().getValues());
- }
- jcontext.set("doc", context);
-
- try {
- if (Boolean.TRUE.equals(expr.evaluate(jcontext))) {
- return doc;
- }
- } catch (Exception e) {
- LOG.warn("Failed in evaluating JEXL {}", expr.getExpression(), e);
- }
- return null;
- }
+ public NutchDocument filter(NutchDocument doc, Parse parse, Text url, CrawlDatum datum, Inlinks inlinks)
+ throws IndexingException {
+ // Create a context and add data
+ JexlContext jcontext = new MapContext();
+
+ jcontext.set("status", CrawlDatum.getStatusName(datum.getStatus()));
+ jcontext.set("fetchTime", (long) (datum.getFetchTime()));
+ jcontext.set("modifiedTime", (long) (datum.getModifiedTime()));
+ jcontext.set("retries", datum.getRetriesSinceFetch());
+ jcontext.set("interval", new Integer(datum.getFetchInterval()));
+ jcontext.set("score", datum.getScore());
+ jcontext.set("signature", StringUtil.toHexString(datum.getSignature()));
+ jcontext.set("url", url.toString());
+
+ jcontext.set("text", parse.getText());
+ jcontext.set("title", parse.getData().getTitle());
- return doc;
+ JexlContext httpStatusContext = new MapContext();
+ httpStatusContext.set("majorCode", parse.getData().getStatus().getMajorCode());
+ httpStatusContext.set("minorCode", parse.getData().getStatus().getMinorCode());
+ httpStatusContext.set("message", parse.getData().getStatus().getMessage());
+ jcontext.set("httpStatus", httpStatusContext);
+
+ jcontext.set("documentMeta", metadataToContext(doc.getDocumentMeta()));
+ jcontext.set("contentMeta", metadataToContext(parse.getData().getContentMeta()));
+ jcontext.set("parseMeta", metadataToContext(parse.getData().getParseMeta()));
+
+ JexlContext context = new MapContext();
+ for (Entry<String, NutchField> entry : doc) {
+ context.set(entry.getKey(), entry.getValue().getValues());
+ }
+ jcontext.set("doc", context);
+
+ try {
+ if (Boolean.TRUE.equals(expr.evaluate(jcontext))) {
+ return doc;
+ }
+ } catch (Exception e) {
+ LOG.warn("Failed evaluating JEXL {}", expr.getExpression(), e);
+ }
+ return null;
}
@Override
public void setConf(Configuration conf) {
this.conf = conf;
- expr = JexlUtil.parseExpression(conf.get("index.jexl.filter"));
+ String str = conf.get("index.jexl.filter");
+ if (str == null) {
+ LOG.warn(
+ "The property index.jexl.filter must have a value when index-jexl-filter is used. You can use 'true' or 'false' to index all/none");
+ throw new RuntimeException(
+ "The property index.jexl.filter must have a value when index-jexl-filter is used. You can use 'true' or 'false' to index all/none");
+ }
+ expr = JexlUtil.parseExpression(str);
+ if (expr == null) {
+ LOG.warn("Failed parsing JEXL from index.jexl.filter: {}", str);
+ throw new RuntimeException("Failed parsing JEXL from index.jexl.filter");
+ }
}
@Override
diff --git a/src/plugin/index-jexl-filter/src/test/org/apache/nutch/indexer/filter/TestJexlIndexingFilter.java b/src/plugin/index-jexl-filter/src/test/org/apache/nutch/indexer/filter/TestJexlIndexingFilter.java
new file mode 100644
index 000000000..a55670277
--- /dev/null
+++ b/src/plugin/index-jexl-filter/src/test/org/apache/nutch/indexer/filter/TestJexlIndexingFilter.java
@@ -0,0 +1,80 @@
+/*
+ * 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.indexer.filter;
+
+import org.apache.hadoop.conf.Configuration;
+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.metadata.Metadata;
+import org.apache.nutch.parse.Outlink;
+import org.apache.nutch.parse.ParseData;
+import org.apache.nutch.parse.ParseImpl;
+import org.apache.nutch.parse.ParseStatus;
+import org.apache.nutch.util.NutchConfiguration;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestJexlIndexingFilter {
+
+ @Test
+ public void testBasicIndexingFilter() throws Exception {
+ Configuration conf = NutchConfiguration.create();
+ conf.set("index.jexl.filter", "doc.lang[0]=='en'");
+
+ JexlIndexingFilter filter = new JexlIndexingFilter();
+ filter.setConf(conf);
+ Assert.assertNotNull(filter);
+
+ NutchDocument doc = new NutchDocument();
+
+ String title = "The Foo Page";
+ Outlink[] outlinks = new Outlink[] { new Outlink("http://foo.com/", "Foo") };
+ Metadata metaData = new Metadata();
+ metaData.add("Language", "en/us");
+ ParseData parseData = new ParseData(ParseStatus.STATUS_SUCCESS, title, outlinks, metaData);
+ ParseImpl parse = new ParseImpl("this is a sample foo bar page. hope you enjoy it.", parseData);
+
+ CrawlDatum crawlDatum = new CrawlDatum();
+ crawlDatum.setFetchTime(100L);
+
+ Inlinks inlinks = new Inlinks();
+
+ doc.add("lang", "en");
+
+ try {
+ NutchDocument result = filter.filter(doc, parse, new Text("http://nutch.apache.org/index.html"), crawlDatum, inlinks);
+ Assert.assertNotNull(result);
+ Assert.assertEquals(doc, result);
+ } catch (Exception e) {
+ e.printStackTrace();
+ Assert.fail(e.getMessage());
+ }
+
+ doc.removeField("lang");
+ doc.add("lang", "ru");
+
+ try {
+ NutchDocument result = filter.filter(doc, parse, new Text("http://nutch.apache.org/index.html"), crawlDatum, inlinks);
+ Assert.assertNull(result);
+ } catch (Exception e) {
+ e.printStackTrace();
+ Assert.fail(e.getMessage());
+ }
+ }
+}