Apache Nutch is an extensible and scalable web crawler
Add a new IndexingFilter that uses JEXL to decide whether to index a document.
Yossi Tamari62f6d9f924c6
diff --git a/build.xml b/build.xmlindex 2d33f96ac..f5b3f78c4 100644--- a/build.xml+++ b/build.xml@@ -1041,6 +1041,7 @@<source path="${plugins.dir}/indexer-elastic/src/java/" /><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-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.xmlindex c406907c5..cab7adf3b 100644--- a/conf/nutch-default.xml+++ b/conf/nutch-default.xml@@ -1566,6 +1566,16 @@ visit https://wiki.apache.org/nutch/SimilarityScoringFilter--></description></property>+<!-- index-jexl-filter plugin properties -->++<property>+ <name>index.jexl.filter</name>+ <value></value>+ <description> A JEXL expression. If it evaluates to false,+ the document will not be indexed.+ </description>+</property>+<!-- index-static plugin properties --><property>diff --git a/src/plugin/build.xml b/src/plugin/build.xmlindex 9074654de..7ea63ce8a 100755--- a/src/plugin/build.xml+++ b/src/plugin/build.xml@@ -32,6 +32,7 @@<ant dir="index-basic" target="deploy"/><ant dir="index-anchor" target="deploy"/><ant dir="index-geoip" target="deploy"/>+ <ant dir="index-jexl-filter" target="deploy"/><ant dir="index-more" target="deploy"/><ant dir="index-replace" target="deploy"/><ant dir="index-static" target="deploy"/>@@ -153,6 +154,7 @@<ant dir="index-basic" target="clean"/><ant dir="index-anchor" target="clean"/><ant dir="index-geoip" target="clean"/>+ <ant dir="index-jexl-filter" target="clean"/><ant dir="index-more" target="clean"/><ant dir="index-static" target="clean"/><ant dir="index-replace" target="clean"/>diff --git a/src/plugin/index-jexl-filter/build.xml b/src/plugin/index-jexl-filter/build.xmlnew file mode 100644index 000000000..7aa7be24d--- /dev/null+++ b/src/plugin/index-jexl-filter/build.xml@@ -0,0 +1,22 @@+<?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="index-jexl-filter" default="jar-core">++ <import file="../build-plugin.xml" />++ </project>diff --git a/src/plugin/index-jexl-filter/ivy.xml b/src/plugin/index-jexl-filter/ivy.xmlnew file mode 100644index 000000000..0a363f774--- /dev/null+++ b/src/plugin/index-jexl-filter/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="${nutch.root}/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/index-jexl-filter/plugin.xml b/src/plugin/index-jexl-filter/plugin.xmlnew file mode 100644index 000000000..9bb543820--- /dev/null+++ b/src/plugin/index-jexl-filter/plugin.xml@@ -0,0 +1,37 @@+<?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="index-jexl-filter"+ name="Filter indexed documents according to a JEXL expression"+ version="1.0.0"+ provider-name="nutch.org">++ <runtime>+ <library name="index-jexl-filter.jar">+ <export name="*"/>+ </library>+ </runtime>++ <extension id="org.apache.nutch.indexer.filter"+ name="Nutch JEXL filter"+ point="org.apache.nutch.indexer.IndexingFilter">+ <implementation id="JexlIndexingFilter"+ class="org.apache.nutch.indexer.filter.JexlIndexingFilter"/>+ </extension>++</plugin>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.javanew file mode 100644index 000000000..a2d3ce134--- /dev/null+++ b/src/plugin/index-jexl-filter/src/java/org/apache/nutch/indexer/filter/JexlIndexingFilter.java@@ -0,0 +1,119 @@+/*+ * 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 java.lang.invoke.MethodHandles;+import java.util.Map.Entry;++import org.apache.commons.jexl2.Expression;+import org.apache.commons.jexl2.JexlContext;+import org.apache.commons.jexl2.MapContext;+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.IndexingException;+import org.apache.nutch.indexer.IndexingFilter;+import org.apache.nutch.indexer.NutchDocument;+import org.apache.nutch.indexer.NutchField;+import org.apache.nutch.metadata.Metadata;+import org.apache.nutch.parse.Parse;+import org.apache.nutch.util.JexlUtil;+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.+ *+ */+public class JexlIndexingFilter implements IndexingFilter {++ private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());++ private Configuration conf;+ 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;+ }++ return doc;+ }++ @Override+ public void setConf(Configuration conf) {+ this.conf = conf;+ expr = JexlUtil.parseExpression(conf.get("index.jexl.filter"));+ }++ @Override+ public Configuration getConf() {+ return this.conf;+ }++ private JexlContext metadataToContext(Metadata metadata) {+ JexlContext context = new MapContext();+ for (String name : metadata.names()) {+ context.set(name, metadata.getValues(name));+ }+ return context;+ }+}