Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-2642 MoreIndexingFilter parses ISO 8601 UTC dates in local time zone - fix date pattern not to convert date in local time zone (as suggested by John Lacey) - do not add last-modified date if there is none - add unit test

Sebastian Nageld3864d66c0d8
diff --git a/src/plugin/index-more/src/java/org/apache/nutch/indexer/more/MoreIndexingFilter.java b/src/plugin/index-more/src/java/org/apache/nutch/indexer/more/MoreIndexingFilter.java
index 3de951ab7..c16d23361 100644
--- a/src/plugin/index-more/src/java/org/apache/nutch/indexer/more/MoreIndexingFilter.java
+++ b/src/plugin/index-more/src/java/org/apache/nutch/indexer/more/MoreIndexingFilter.java
@@ -104,8 +104,10 @@ public class MoreIndexingFilter implements IndexingFilter {
String lastModified = data.getMeta(Metadata.LAST_MODIFIED);
if (lastModified != null) { // try parse last-modified
time = getTime(lastModified, url); // use as time
- // store as string
- doc.add("lastModified", new Date(time));
+ // store as Date
+ if (time > -1) {
+ doc.add("lastModified", new Date(time));
+ }
}
if (time == -1) { // if no last-modified specified in HTTP header
@@ -139,7 +141,7 @@ public class MoreIndexingFilter implements IndexingFilter {
"MMM dd yyyy HH:mm:ss. zzz", "MMM dd yyyy HH:mm:ss zzz",
"dd.MM.yyyy HH:mm:ss zzz", "dd MM yyyy HH:mm:ss zzz",
"dd.MM.yyyy; HH:mm:ss", "dd.MM.yyyy HH:mm:ss", "dd.MM.yyyy zzz",
- "yyyy-MM-dd'T'HH:mm:ss'Z'" });
+ "yyyy-MM-dd'T'HH:mm:ssXXX" });
time = parsedDate.getTime();
// if (LOG.isWarnEnabled()) {
// LOG.warn(url + ": parsed date: " + date +" to:"+time);
diff --git a/src/plugin/index-more/src/test/org/apache/nutch/indexer/more/TestMoreIndexingFilter.java b/src/plugin/index-more/src/test/org/apache/nutch/indexer/more/TestMoreIndexingFilter.java
index f918dde97..5a22cf6de 100644
--- a/src/plugin/index-more/src/test/org/apache/nutch/indexer/more/TestMoreIndexingFilter.java
+++ b/src/plugin/index-more/src/test/org/apache/nutch/indexer/more/TestMoreIndexingFilter.java
@@ -16,6 +16,9 @@
*/
package org.apache.nutch.indexer.more;
+import java.time.format.DateTimeFormatter;
+import java.util.Date;
+
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.Text;
import org.apache.nutch.crawl.CrawlDatum;
@@ -120,4 +123,42 @@ public class TestMoreIndexingFilter {
Assert.assertEquals("mime type not detected", expected,
doc.getFieldValue("type"));
}
+
+ @Test
+ public void testDates() throws IndexingException {
+ Configuration conf = NutchConfiguration.create();
+
+ Metadata metadata = new Metadata();
+ MoreIndexingFilter filter = new MoreIndexingFilter();
+ filter.setConf(conf);
+
+ Text url = new Text("http://www.example.com/");
+ ParseImpl parseImpl = new ParseImpl("text",
+ new ParseData(new ParseStatus(), "title", new Outlink[0], metadata));
+ CrawlDatum fetchDatum = new CrawlDatum();
+ NutchDocument doc = new NutchDocument();
+
+ long dateEpocheSeconds = 1537898340; // 2018-09-25T17:59:00+0000
+ fetchDatum.setModifiedTime(dateEpocheSeconds * 1000);
+ // fetch time 30 days later
+ fetchDatum.setFetchTime((dateEpocheSeconds + 30 * 24 * 60 * 60) * 1000);
+ // NOTE: the datum.getLastModified() returns the fetch time
+ // of the latest fetch of changed content (i.e., not a "HTTP 304
+ // not-modified" or a re-fetch resulting in the same signature)
+
+ doc = filter.filter(doc, parseImpl, url, fetchDatum, new Inlinks());
+
+ Assert.assertEquals("last fetch date not extracted",
+ new Date(dateEpocheSeconds * 1000), doc.getFieldValue("date"));
+
+ // set last-modified time (7 days before fetch time)
+ Date lastModifiedDate = new Date(
+ (dateEpocheSeconds - 7 * 24 * 60 * 60) * 1000);
+ String lastModifiedDateStr = DateTimeFormatter.ISO_INSTANT.format(lastModifiedDate.toInstant());
+ parseImpl.getData().getParseMeta().set(Metadata.LAST_MODIFIED, lastModifiedDateStr);
+ doc = filter.filter(doc, parseImpl, url, fetchDatum, new Inlinks());
+
+ Assert.assertEquals("last-modified date not extracted", lastModifiedDate,
+ doc.getFieldValue("lastModified"));
+ }
}