Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

added checkbox tile to control activity of clustering

Sami Sirenf90d6c2041bd
diff --git a/contrib/web2/plugins/web-clustering/src/conf/tiles-defs.xml b/contrib/web2/plugins/web-clustering/src/conf/tiles-defs.xml
index e8e88f5fc..705be883a 100644
--- a/contrib/web2/plugins/web-clustering/src/conf/tiles-defs.xml
+++ b/contrib/web2/plugins/web-clustering/src/conf/tiles-defs.xml
@@ -5,4 +5,7 @@
<definition name="cluster"
path="/plugin/web-clustering/cluster.jsp"
controllerClass="org.apache.nutch.webapp.controller.ClusteringController" />
+ <definition name="cluster-checkbox"
+ path="/plugin/web-clustering/cluster-checkbox.jsp"
+ controllerClass="org.apache.nutch.webapp.controller.ClusteringCheckboxController" />
</tiles-definitions>
\ No newline at end of file
diff --git a/contrib/web2/plugins/web-clustering/src/java/org/apache/nutch/clustering/ClusteringPresearchExtension.java b/contrib/web2/plugins/web-clustering/src/java/org/apache/nutch/clustering/ClusteringPresearchExtension.java
index 57c2cfd9c..62c23dc99 100644
--- a/contrib/web2/plugins/web-clustering/src/java/org/apache/nutch/clustering/ClusteringPresearchExtension.java
+++ b/contrib/web2/plugins/web-clustering/src/java/org/apache/nutch/clustering/ClusteringPresearchExtension.java
@@ -15,24 +15,50 @@
*/
package org.apache.nutch.clustering;
-import org.apache.nutch.webapp.common.SearchContext;
+import org.apache.nutch.webapp.common.ServiceLocator;
import org.apache.nutch.webapp.extension.PreSearchExtensionPoint;
+/**
+ * This class is responsible for interpreting request parameters
+ * and reacting if named parameter is defined.
+ *
+ * If clustering is available the result window is checked
+ * and expanded to 100 hits if required.
+ */
public class ClusteringPresearchExtension implements PreSearchExtensionPoint {
- /**
+
+ /**
+ * Check wether clustering is active or not
+ * @param locator
+ * @return true if clustering is active
+ */
+ public static boolean isClusteringActive(ServiceLocator locator){
+ return locator.getSearchForm().getValueString(REQ_PARAM_CLUSTERING_ENABLED)!=null;
+ }
+
+ /**
+ * The parameter name to be searched from request
+ */
+ public static final String REQ_PARAM_CLUSTERING_ENABLED="clustering";
+
+ /*
* This hook is executed before actual search
* so we have a change to expand the result window
- * for clusterer
+ * for clusterer if clustering is active for the request
*/
- public void doPreSearch(SearchContext context) {
- int orig=context.getSearch().getHitsRequired();
+ public void doPreSearch(ServiceLocator locator) {
+
+ if(isClusteringActive(locator)) {
+
+ int orig=locator.getSearch().getHitsRequired();
- int hitsToCluster = context.getConfigiration().getInt(
+ int hitsToCluster = locator.getConfiguration().getInt(
"extension.clustering.hits-to-cluster", 100);
- if(orig < hitsToCluster){
- context.getSearch().setHitsRequired(hitsToCluster);
+ if(orig < hitsToCluster){
+ locator.getSearch().setHitsRequired(hitsToCluster);
+ }
}
}
}
diff --git a/contrib/web2/plugins/web-clustering/src/java/org/apache/nutch/webapp/controller/ClusteringCheckboxController.java b/contrib/web2/plugins/web-clustering/src/java/org/apache/nutch/webapp/controller/ClusteringCheckboxController.java
new file mode 100644
index 000000000..ac235f621
--- /dev/null
+++ b/contrib/web2/plugins/web-clustering/src/java/org/apache/nutch/webapp/controller/ClusteringCheckboxController.java
@@ -0,0 +1,32 @@
+package org.apache.nutch.webapp.controller;
+import java.io.IOException;
+
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.nutch.clustering.ClusteringPresearchExtension;
+import org.apache.nutch.webapp.common.ServiceLocator;
+import org.apache.nutch.webapp.controller.NutchController;
+import org.apache.struts.tiles.ComponentContext;
+
+/**
+ * Controller logic to decide wether clustering checkbox
+ * is checked or not.
+ */
+public class ClusteringCheckboxController extends NutchController {
+
+ public static final String REQ_ATTR_CLUSTERING_ENABLED="clusteringEnabled";
+
+ public void nutchPerform(ComponentContext tileContext,
+ HttpServletRequest request, HttpServletResponse response,
+ ServletContext servletContext) throws ServletException, IOException {
+
+ ServiceLocator locator=getServiceLocator(request);
+
+ if(ClusteringPresearchExtension.isClusteringActive(locator)) {
+ request.setAttribute(REQ_ATTR_CLUSTERING_ENABLED, "1");
+ }
+ }
+}
diff --git a/contrib/web2/plugins/web-clustering/src/java/org/apache/nutch/webapp/controller/ClusteringController.java b/contrib/web2/plugins/web-clustering/src/java/org/apache/nutch/webapp/controller/ClusteringController.java
index a89ca9762..e7a0da817 100644
--- a/contrib/web2/plugins/web-clustering/src/java/org/apache/nutch/webapp/controller/ClusteringController.java
+++ b/contrib/web2/plugins/web-clustering/src/java/org/apache/nutch/webapp/controller/ClusteringController.java
@@ -22,6 +22,7 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import org.apache.nutch.clustering.ClusteringPresearchExtension;
import org.apache.nutch.clustering.Clusters;
import org.apache.nutch.clustering.HitsCluster;
import org.apache.nutch.clustering.OnlineClusterer;
@@ -33,49 +34,53 @@ import org.apache.nutch.webapp.common.ServiceLocator;
import org.apache.nutch.webapp.common.Startable;
import org.apache.struts.tiles.ComponentContext;
-public class ClusteringController extends NutchController implements Startable {
+public class ClusteringController extends NutchController implements Startable {
public static final String REQ_ATTR_CLUSTERS = "clusters";
- static OnlineClusterer clusterer=null;
-
+ static OnlineClusterer clusterer = null;
+
public void nutchPerform(ComponentContext tileContext,
HttpServletRequest request, HttpServletResponse response,
ServletContext servletContext) throws ServletException, IOException {
ServiceLocator locator = getServiceLocator(request);
- // display top N clusters and top Q documents inside them.
- int N = locator.getConfiguration().getInt(
- "extension.clustering.cluster-count", 10);
- int Q = locator.getConfiguration().getInt(
- "extension.clustering.cluster-top-documents-count", 3);
- int maxLabels = 2;
-
- HitDetails[] details = locator.getSearch().getDetails();
- Summary[] summaries = locator.getSearch().getSummaries();
-
- HitsCluster[] clusters = null;
- if (clusterer != null) {
- final long clusteringStart = System.currentTimeMillis();
- try {
- clusters = clusterer.clusterHits(details, Summary.toStrings(summaries));
- final long clusteringDuration = System.currentTimeMillis()
- - clusteringStart;
- LOG.info("Clustering took: " + clusteringDuration + " milliseconds.");
-
- } catch (Exception e) {
- LOG.info("Could not do clustering???" + e);
- return;
+ if (ClusteringPresearchExtension.isClusteringActive(locator)) {
+
+ // display top N clusters and top Q documents inside them.
+ int N = locator.getConfiguration().getInt(
+ "extension.clustering.cluster-count", 10);
+ int Q = locator.getConfiguration().getInt(
+ "extension.clustering.cluster-top-documents-count", 3);
+ int maxLabels = 2;
+
+ HitDetails[] details = locator.getSearch().getDetails();
+ Summary[] summaries = locator.getSearch().getSummaries();
+
+ HitsCluster[] clusters = null;
+ if (clusterer != null) {
+ final long clusteringStart = System.currentTimeMillis();
+ try {
+ clusters = clusterer.clusterHits(details, Summary
+ .toStrings(summaries));
+ final long clusteringDuration = System.currentTimeMillis()
+ - clusteringStart;
+ LOG.info("Clustering took: " + clusteringDuration + " milliseconds.");
+
+ } catch (Exception e) {
+ LOG.info("Could not do clustering???" + e);
+ return;
+ }
}
- }
- // set new limit if fever than N results
- N = Math.min(N, clusters.length);
+ // set new limit if fever than N results
+ N = Math.min(N, clusters.length);
- //set to request
- Clusters clusterResult = new Clusters(clusters, N, Q, maxLabels);
- request.setAttribute(REQ_ATTR_CLUSTERS, clusterResult);
+ // set to request
+ Clusters clusterResult = new Clusters(clusters, N, Q, maxLabels);
+ request.setAttribute(REQ_ATTR_CLUSTERS, clusterResult);
+ }
}
public void start(ServletContext servletContext) {
diff --git a/contrib/web2/plugins/web-clustering/src/web/web-clustering/cluster-checkbox.jsp b/contrib/web2/plugins/web-clustering/src/web/web-clustering/cluster-checkbox.jsp
new file mode 100644
index 000000000..0acd7b51a
--- /dev/null
+++ b/contrib/web2/plugins/web-clustering/src/web/web-clustering/cluster-checkbox.jsp
@@ -0,0 +1,11 @@
+<%@ page session="false"%>
+<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
+<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
+<c:choose>
+ <c:when test="${clusteringEnabled != null}">
+ <input type="checkbox" name="clustering" value="on" checked="checked"/>
+ </c:when>
+ <c:otherwise>
+ <input id="clustbox" type="checkbox" name="clustering" /><label for="clustbox"><fmt:message key="search.clustering"/></label>
+ </c:otherwise>
+</c:choose>
diff --git a/contrib/web2/plugins/web-clustering/src/web/web-clustering/cluster.jsp b/contrib/web2/plugins/web-clustering/src/web/web-clustering/cluster.jsp
index e5a279387..a9cdc72fd 100644
--- a/contrib/web2/plugins/web-clustering/src/web/web-clustering/cluster.jsp
+++ b/contrib/web2/plugins/web-clustering/src/web/web-clustering/cluster.jsp
@@ -43,9 +43,5 @@ No clusters available
</c:choose>
</c:when>
- <c:otherwise>
- <!-- todo: i18n -->
-Unable to do clustering.
-</c:otherwise>
</c:choose>
</div>