Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

Fixed per reviewers' comments. Changed the package name to be more specific, added package-info.java, added to more build targets.

pipldeva985e3011694
diff --git a/build.xml b/build.xml
index 5503961a7..56be49533 100644
--- a/build.xml
+++ b/build.xml
@@ -179,6 +179,7 @@
<packageset dir="${plugins.dir}/index-anchor/src/java"/>
<packageset dir="${plugins.dir}/index-basic/src/java"/>
<packageset dir="${plugins.dir}/index-geoip/src/java"/>
+ <packageset dir="${plugins.dir}/index-jexl-filter/src/java"/>
<packageset dir="${plugins.dir}/index-links/src/java"/>
<packageset dir="${plugins.dir}/index-metadata/src/java"/>
<packageset dir="${plugins.dir}/index-more/src/java"/>
@@ -630,6 +631,7 @@
<packageset dir="${plugins.dir}/index-anchor/src/java"/>
<packageset dir="${plugins.dir}/index-basic/src/java"/>
<packageset dir="${plugins.dir}/index-geoip/src/java"/>
+ <packageset dir="${plugins.dir}/index-jexl-filter/src/java"/>
<packageset dir="${plugins.dir}/index-links/src/java"/>
<packageset dir="${plugins.dir}/index-metadata/src/java"/>
<packageset dir="${plugins.dir}/index-more/src/java"/>
@@ -1040,10 +1042,10 @@
<source path="${plugins.dir}/index-basic/src/java/" />
<source path="${plugins.dir}/index-basic/src/test/" />
<source path="${plugins.dir}/index-geoip/src/java/" />
- <source path="${plugins.dir}/index-links/src/java/" />
- <source path="${plugins.dir}/index-links/src/test/" />
<source path="${plugins.dir}/index-jexl-filter/src/java/" />
<source path="${plugins.dir}/index-jexl-filter/src/test/" />
+ <source path="${plugins.dir}/index-links/src/java/" />
+ <source path="${plugins.dir}/index-links/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/default.properties b/default.properties
index 6b7a6ab79..c057518d8 100644
--- a/default.properties
+++ b/default.properties
@@ -170,6 +170,7 @@ plugins.index=\
org.apache.nutch.indexer.basic*:\
org.apache.nutch.indexer.feed*:\
org.apache.nutch.indexer.geoip*:\
+ org.apache.nutch.indexer.jexl*:\
org.apache.nutch.indexer.filter*:\
org.apache.nutch.indexer.links*:\
org.apache.nutch.indexer.metadata*:\
diff --git a/src/plugin/index-jexl-filter/plugin.xml b/src/plugin/index-jexl-filter/plugin.xml
index 9bb543820..a24a0c95f 100644
--- a/src/plugin/index-jexl-filter/plugin.xml
+++ b/src/plugin/index-jexl-filter/plugin.xml
@@ -27,11 +27,11 @@
</library>
</runtime>
- <extension id="org.apache.nutch.indexer.filter"
+ <extension id="org.apache.nutch.indexer.jexl"
name="Nutch JEXL filter"
point="org.apache.nutch.indexer.IndexingFilter">
<implementation id="JexlIndexingFilter"
- class="org.apache.nutch.indexer.filter.JexlIndexingFilter"/>
+ class="org.apache.nutch.indexer.jexl.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.java
deleted file mode 100644
index 233ead3b3..000000000
--- a/src/plugin/index-jexl-filter/src/java/org/apache/nutch/indexer/filter/JexlIndexingFilter.java
+++ /dev/null
@@ -1,126 +0,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.
- */
-
-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 {
- // 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 evaluating JEXL {}", expr.getExpression(), e);
- }
- return null;
- }
-
- @Override
- public void setConf(Configuration conf) {
- this.conf = conf;
- 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
- 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;
- }
-}
diff --git a/src/plugin/index-jexl-filter/src/java/org/apache/nutch/indexer/jexl/JexlIndexingFilter.java b/src/plugin/index-jexl-filter/src/java/org/apache/nutch/indexer/jexl/JexlIndexingFilter.java
new file mode 100644
index 000000000..24284a67b
--- /dev/null
+++ b/src/plugin/index-jexl-filter/src/java/org/apache/nutch/indexer/jexl/JexlIndexingFilter.java
@@ -0,0 +1,131 @@
+/*
+ * 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.jexl;
+
+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 {
+ // 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 evaluating JEXL {}", expr.getExpression(), e);
+ }
+ return null;
+ }
+
+ @Override
+ public void setConf(Configuration conf) {
+ this.conf = conf;
+ 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
+ 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;
+ }
+}
diff --git a/src/plugin/index-jexl-filter/src/java/org/apache/nutch/indexer/jexl/package-info.java b/src/plugin/index-jexl-filter/src/java/org/apache/nutch/indexer/jexl/package-info.java
new file mode 100644
index 000000000..fc406ea9d
--- /dev/null
+++ b/src/plugin/index-jexl-filter/src/java/org/apache/nutch/indexer/jexl/package-info.java
@@ -0,0 +1,30 @@
+/**
+ * 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.
+ */
+/**
+ * <p>This plugin implements a dynamic indexing filter which uses JEXL
+ * expressions to allow filtering based on the page's metadata
+ * <p>Available primitives in the JEXL context:<ul>
+ * <li>status, fetchTime, modifiedTime, retries, interval, score, signature, url, text, title</li></ul>
+ * <p>Available objects in the JEXL context:<ul>
+ * <li>httpStatus - contains majorCode, minorCode, message</li>
+ * <li>documentMeta, contentMeta, parseMeta - contain all the Metadata properties.<br/>
+ * Each property value is always an array of Strings (so if you expect one value, use [0])</li>
+ * <li>doc - contains all the NutchFields from the NutchDocument.<br/>
+ * Each property value is always an array of Objects.</li></ul>
+ *
+ */
+package org.apache.nutch.indexer.jexl;
\ No newline at end of file
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
deleted file mode 100644
index 286d29651..000000000
--- a/src/plugin/index-jexl-filter/src/test/org/apache/nutch/indexer/filter/TestJexlIndexingFilter.java
+++ /dev/null
@@ -1,116 +0,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.
- */
-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.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-
-public class TestJexlIndexingFilter {
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
- @Test
- public void testAllowMatchingDocument() 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");
-
- NutchDocument result = filter.filter(doc, parse, new Text("http://nutch.apache.org/index.html"), crawlDatum, inlinks);
- Assert.assertNotNull(result);
- Assert.assertEquals(doc, result);
- }
-
- @Test
- public void testBlockNotMatchingDocuments() 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", "ru");
-
- NutchDocument result = filter.filter(doc, parse, new Text("http://nutch.apache.org/index.html"), crawlDatum, inlinks);
- Assert.assertNull(result);
- }
-
- @Test
- public void testMissingConfiguration() throws Exception {
- Configuration conf = NutchConfiguration.create();
-
- JexlIndexingFilter filter = new JexlIndexingFilter();
- thrown.expect(RuntimeException.class);
- filter.setConf(conf);
- }
-
- @Test
- public void testInvalidExpression() throws Exception {
- Configuration conf = NutchConfiguration.create();
- conf.set("index.jexl.filter", "doc.lang[0]=<>:='en'");
-
- JexlIndexingFilter filter = new JexlIndexingFilter();
- thrown.expect(RuntimeException.class);
- filter.setConf(conf);
- }
-}
diff --git a/src/plugin/index-jexl-filter/src/test/org/apache/nutch/indexer/jexl/TestJexlIndexingFilter.java b/src/plugin/index-jexl-filter/src/test/org/apache/nutch/indexer/jexl/TestJexlIndexingFilter.java
new file mode 100644
index 000000000..0427ad495
--- /dev/null
+++ b/src/plugin/index-jexl-filter/src/test/org/apache/nutch/indexer/jexl/TestJexlIndexingFilter.java
@@ -0,0 +1,124 @@
+/*
+ * 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.jexl;
+
+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.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+public class TestJexlIndexingFilter {
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ @Test
+ public void testAllowMatchingDocument() 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");
+
+ NutchDocument result = filter.filter(doc, parse,
+ new Text("http://nutch.apache.org/index.html"), crawlDatum, inlinks);
+ Assert.assertNotNull(result);
+ Assert.assertEquals(doc, result);
+ }
+
+ @Test
+ public void testBlockNotMatchingDocuments() 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", "ru");
+
+ NutchDocument result = filter.filter(doc, parse,
+ new Text("http://nutch.apache.org/index.html"), crawlDatum, inlinks);
+ Assert.assertNull(result);
+ }
+
+ @Test
+ public void testMissingConfiguration() throws Exception {
+ Configuration conf = NutchConfiguration.create();
+
+ JexlIndexingFilter filter = new JexlIndexingFilter();
+ thrown.expect(RuntimeException.class);
+ filter.setConf(conf);
+ }
+
+ @Test
+ public void testInvalidExpression() throws Exception {
+ Configuration conf = NutchConfiguration.create();
+ conf.set("index.jexl.filter", "doc.lang[0]=<>:='en'");
+
+ JexlIndexingFilter filter = new JexlIndexingFilter();
+ thrown.expect(RuntimeException.class);
+ filter.setConf(conf);
+ }
+}