lucene-solr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From yo...@apache.org
Subject svn commit: r790594 - in /lucene/solr/trunk/contrib/clustering/src: main/java/org/apache/solr/handler/clustering/ main/java/org/apache/solr/handler/clustering/carrot2/ test/java/org/apache/solr/handler/clustering/ test/java/org/apache/solr/handler/clus...
Date Thu, 02 Jul 2009 13:51:19 GMT
Author: yonik
Date: Thu Jul  2 13:51:19 2009
New Revision: 790594

URL: http://svn.apache.org/viewvc?rev=790594&view=rev
Log:
SOLR-769: make response JSON friendly, use SolrQueryRequest instead of core

Modified:
    lucene/solr/trunk/contrib/clustering/src/main/java/org/apache/solr/handler/clustering/ClusteringComponent.java
    lucene/solr/trunk/contrib/clustering/src/main/java/org/apache/solr/handler/clustering/SearchClusteringEngine.java
    lucene/solr/trunk/contrib/clustering/src/main/java/org/apache/solr/handler/clustering/carrot2/CarrotClusteringEngine.java
    lucene/solr/trunk/contrib/clustering/src/test/java/org/apache/solr/handler/clustering/ClusteringComponentTest.java
    lucene/solr/trunk/contrib/clustering/src/test/java/org/apache/solr/handler/clustering/carrot2/CarrotClusteringEngineTest.java

Modified: lucene/solr/trunk/contrib/clustering/src/main/java/org/apache/solr/handler/clustering/ClusteringComponent.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/contrib/clustering/src/main/java/org/apache/solr/handler/clustering/ClusteringComponent.java?rev=790594&r1=790593&r2=790594&view=diff
==============================================================================
--- lucene/solr/trunk/contrib/clustering/src/main/java/org/apache/solr/handler/clustering/ClusteringComponent.java
(original)
+++ lucene/solr/trunk/contrib/clustering/src/main/java/org/apache/solr/handler/clustering/ClusteringComponent.java
Thu Jul  2 13:51:19 2009
@@ -72,8 +72,8 @@
       SearchClusteringEngine engine = searchClusteringEngines.get(name);
       if (engine != null) {
         DocListAndSet results = rb.getResults();
-        NamedList nl = engine.cluster(rb.getQuery(), results.docList, params);
-        rb.rsp.add("clusters", nl);
+        Object clusters = engine.cluster(rb.getQuery(), results.docList, rb.req);
+        rb.rsp.add("clusters", clusters);
       } else {
         log.warn("No engine for: " + name);
       }

Modified: lucene/solr/trunk/contrib/clustering/src/main/java/org/apache/solr/handler/clustering/SearchClusteringEngine.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/contrib/clustering/src/main/java/org/apache/solr/handler/clustering/SearchClusteringEngine.java?rev=790594&r1=790593&r2=790594&view=diff
==============================================================================
--- lucene/solr/trunk/contrib/clustering/src/main/java/org/apache/solr/handler/clustering/SearchClusteringEngine.java
(original)
+++ lucene/solr/trunk/contrib/clustering/src/main/java/org/apache/solr/handler/clustering/SearchClusteringEngine.java
Thu Jul  2 13:51:19 2009
@@ -20,6 +20,7 @@
 import org.apache.solr.common.params.SolrParams;
 import org.apache.solr.core.SolrCore;
 import org.apache.solr.search.DocList;
+import org.apache.solr.request.SolrQueryRequest;
 import org.apache.lucene.search.Query;
 
 
@@ -30,7 +31,7 @@
 public abstract class SearchClusteringEngine extends ClusteringEngine {
 
 
-  public abstract NamedList cluster(Query query, DocList docList, SolrParams solrParams);
+  public abstract Object cluster(Query query, DocList docList, SolrQueryRequest sreq);
 
 
 }

Modified: lucene/solr/trunk/contrib/clustering/src/main/java/org/apache/solr/handler/clustering/carrot2/CarrotClusteringEngine.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/contrib/clustering/src/main/java/org/apache/solr/handler/clustering/carrot2/CarrotClusteringEngine.java?rev=790594&r1=790593&r2=790594&view=diff
==============================================================================
--- lucene/solr/trunk/contrib/clustering/src/main/java/org/apache/solr/handler/clustering/carrot2/CarrotClusteringEngine.java
(original)
+++ lucene/solr/trunk/contrib/clustering/src/main/java/org/apache/solr/handler/clustering/carrot2/CarrotClusteringEngine.java
Thu Jul  2 13:51:19 2009
@@ -27,6 +27,7 @@
 import org.apache.solr.common.params.HighlightParams;
 import org.apache.solr.common.params.SolrParams;
 import org.apache.solr.common.util.NamedList;
+import org.apache.solr.common.util.SimpleOrderedMap;
 import org.apache.solr.common.SolrException;
 import org.apache.solr.core.SolrCore;
 import org.apache.solr.handler.clustering.SearchClusteringEngine;
@@ -58,23 +59,22 @@
 	private CachingController controller = new CachingController();
 	private Class<? extends IClusteringAlgorithm> clusteringAlgorithmClass;
 	
-	private SolrCore core;
 	private String idFieldName;
 
-	public NamedList cluster(Query query, DocList docList, SolrParams solrParams) {
+	public Object cluster(Query query, DocList docList, SolrQueryRequest sreq) {
 		try {
 			// Prepare attributes for Carrot2 clustering call
 			Map<String, Object> attributes = new HashMap<String, Object>();
-			List<Document> documents = getDocuments(docList, core, query, solrParams);
+			List<Document> documents = getDocuments(docList, query, sreq);
 			attributes.put(AttributeNames.DOCUMENTS, documents);
 			attributes.put(AttributeNames.QUERY, query.toString());
 			
 			// Pass extra overriding attributes from the request, if any
-			extractCarrotAttributes(solrParams, attributes);
+			extractCarrotAttributes(sreq.getParams(), attributes);
 
 			// Perform clustering and convert to named list
 			return clustersToNamedList(controller.process(attributes,
-					clusteringAlgorithmClass).getClusters(), solrParams);
+					clusteringAlgorithmClass).getClusters(), sreq.getParams());
 		} catch (Exception e) {
 			log.error("Carrot2 clustering failed", e);
 			throw new RuntimeException(e);
@@ -91,7 +91,6 @@
 		extractCarrotAttributes(initParams, initAttributes);
 		this.controller.init(initAttributes);
 		
-		this.core = core;
 		this.idFieldName = core.getSchema().getUniqueKeyField().getName();
 
 		// Make sure the requested Carrot2 clustering algorithm class is available 
@@ -116,9 +115,11 @@
 	/**
 	 * Prepares Carrot2 documents for clustering.
 	 */
-	private List<Document> getDocuments(DocList docList, SolrCore core,
-			Query query, SolrParams solrParams) {
+	private List<Document> getDocuments(DocList docList,
+			Query query, final SolrQueryRequest sreq) throws IOException {
 		SolrHighlighter highligher = null;
+                SolrParams solrParams = sreq.getParams();
+                SolrCore core = sreq.getCore();
 
 		// Names of fields to deliver content for clustering
 		String urlField = solrParams.get(CarrotParams.URL_FIELD_NAME, "url");
@@ -145,13 +146,16 @@
 			snippetFieldAry = new String[] { snippetField };
 			args.put(HighlightParams.FIELDS, snippetFieldAry);
 			args.put(HighlightParams.HIGHLIGHT, "true");
-			req = new LocalSolrQueryRequest(core, query.toString(), "", 0, 1, args);
+			req = new LocalSolrQueryRequest(core, query.toString(), "", 0, 1, args) {
+                          @Override
+                          public SolrIndexSearcher getSearcher() {
+                            return sreq.getSearcher();
+                          }
+                        };
 		}
 
-		RefCounted<SolrIndexSearcher> refCounter = core.getSearcher();
-		SolrIndexSearcher searcher = refCounter.get();
+		SolrIndexSearcher searcher = sreq.getSearcher();
 		List<Document> result = new ArrayList<Document>(docList.size());
-		try {
 			FieldSelector fieldSelector = new SetBasedFieldSelector(fieldsToLoad,
 					Collections.emptySet());
 			float[] scores = { 1.0f };
@@ -173,11 +177,7 @@
 				carrotDocument.addField("solrId", doc.get(idFieldName));
 				result.add(carrotDocument);
 			}
-		} catch (IOException e) {
-			log.error("IOException", e);
-		} finally {
-			refCounter.decref();
-		}
+
 		return result;
 	}
 
@@ -194,9 +194,9 @@
 		return result.toString().trim();
 	}
 
-	private NamedList clustersToNamedList(List<Cluster> carrotClusters,
+	private List clustersToNamedList(List<Cluster> carrotClusters,
 			SolrParams solrParams) {
-		NamedList result = new NamedList();
+          List result = new ArrayList();
 		clustersToNamedList(carrotClusters, result, solrParams.getBool(
 				CarrotParams.OUTPUT_SUB_CLUSTERS, false), solrParams.getInt(
 				CarrotParams.NUM_DESCRIPTIONS, Integer.MAX_VALUE));
@@ -204,33 +204,27 @@
 	}
 
 	private void clustersToNamedList(List<Cluster> outputClusters,
-			NamedList parent, boolean outputSubClusters, int maxLabels) {
+			List parent, boolean outputSubClusters, int maxLabels) {
 		for (Cluster outCluster : outputClusters) {
-			NamedList cluster = new NamedList();
-			parent.add("cluster", cluster);
+			NamedList cluster = new SimpleOrderedMap();
+			parent.add(cluster);
 
 			List<String> labels = outCluster.getPhrases();
-			NamedList labelsNL = new NamedList();
-			cluster.add("labels", labelsNL);
-			int labelsAdded = 0;
-			for (String label : labels) {
-				if (++labelsAdded > maxLabels) {
-					break;
-				}
-				labelsNL.add("label", label);
-			}
+                  if (labels.size() > maxLabels)
+                    labels = labels.subList(0,maxLabels);
+			cluster.add("labels", labels);
 
 			List<Document> docs = outCluster.getDocuments();
-			NamedList docsNL = new NamedList();
-			cluster.add("docs", docsNL);
+			List docList = new ArrayList();
+			cluster.add("docs", docList);
 			for (Document doc : docs) {
-				docsNL.add("doc", doc.getField("solrId"));
+				docList.add(doc.getField("solrId"));
 			}
 
 			if (outputSubClusters) {
-				NamedList subclustersNL = new NamedList();
-				cluster.add("clusters", subclustersNL);
-				clustersToNamedList(outCluster.getSubclusters(), subclustersNL,
+				List subclusters = new ArrayList();
+				cluster.add("clusters",subclusters);
+				clustersToNamedList(outCluster.getSubclusters(), subclusters,
 						outputSubClusters, maxLabels);
 			}
 		}

Modified: lucene/solr/trunk/contrib/clustering/src/test/java/org/apache/solr/handler/clustering/ClusteringComponentTest.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/contrib/clustering/src/test/java/org/apache/solr/handler/clustering/ClusteringComponentTest.java?rev=790594&r1=790593&r2=790594&view=diff
==============================================================================
--- lucene/solr/trunk/contrib/clustering/src/test/java/org/apache/solr/handler/clustering/ClusteringComponentTest.java
(original)
+++ lucene/solr/trunk/contrib/clustering/src/test/java/org/apache/solr/handler/clustering/ClusteringComponentTest.java
Thu Jul  2 13:51:19 2009
@@ -55,7 +55,7 @@
     rsp.add("responseHeader", new SimpleOrderedMap());
     handler.handleRequest(new LocalSolrQueryRequest(core, params), rsp);
     NamedList values = rsp.getValues();
-    NamedList clusters = (NamedList) values.get("clusters");
+    Object clusters = values.get("clusters");
     //System.out.println("Clusters: " + clusters);
     assertTrue("clusters is null and it shouldn't be", clusters != null);
 
@@ -72,7 +72,7 @@
     rsp.add("responseHeader", new SimpleOrderedMap());
     handler.handleRequest(new LocalSolrQueryRequest(core, params), rsp);
     values = rsp.getValues();
-    clusters = (NamedList) values.get("clusters");
+    clusters = values.get("clusters");
     //System.out.println("Clusters: " + clusters);
     assertTrue("clusters is null and it shouldn't be", clusters != null);
 

Modified: lucene/solr/trunk/contrib/clustering/src/test/java/org/apache/solr/handler/clustering/carrot2/CarrotClusteringEngineTest.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/contrib/clustering/src/test/java/org/apache/solr/handler/clustering/carrot2/CarrotClusteringEngineTest.java?rev=790594&r1=790593&r2=790594&view=diff
==============================================================================
--- lucene/solr/trunk/contrib/clustering/src/test/java/org/apache/solr/handler/clustering/carrot2/CarrotClusteringEngineTest.java
(original)
+++ lucene/solr/trunk/contrib/clustering/src/test/java/org/apache/solr/handler/clustering/carrot2/CarrotClusteringEngineTest.java
Thu Jul  2 13:51:19 2009
@@ -18,6 +18,7 @@
  */
 
 import java.io.IOException;
+import java.util.List;
 
 import org.apache.lucene.search.*;
 import org.apache.solr.common.params.ModifiableSolrParams;
@@ -28,6 +29,7 @@
 import org.apache.solr.search.DocList;
 import org.apache.solr.search.SolrIndexSearcher;
 import org.apache.solr.util.RefCounted;
+import org.apache.solr.request.LocalSolrQueryRequest;
 import org.carrot2.util.attribute.AttributeUtils;
 
 /**
@@ -82,12 +84,12 @@
 		return engine;
 	}
 
-	private NamedList checkEngine(CarrotClusteringEngine engine,
+	private List checkEngine(CarrotClusteringEngine engine,
 			int expectedNumClusters) throws IOException {
 		return checkEngine(engine, expectedNumClusters, new ModifiableSolrParams());
 	}
 
-	private NamedList checkEngine(CarrotClusteringEngine engine,
+	private List checkEngine(CarrotClusteringEngine engine,
 			int expectedNumClusters, SolrParams clusteringParams) throws IOException {
 		// Get all documents to cluster
 		RefCounted<SolrIndexSearcher> ref = h.getCore().getSearcher();
@@ -107,45 +109,42 @@
 		solrParams.add(clusteringParams);
 
 		// Perform clustering
-		NamedList results = engine.cluster(query, docList, solrParams);
+                LocalSolrQueryRequest req = new LocalSolrQueryRequest(h.getCore(), solrParams);
+		List results = (List)engine.cluster(query, docList, req);
+                req.close();
 		assertEquals("number of clusters", expectedNumClusters, results.size());
 		checkClusters(results, false);
 		return results;
 	}
 
-	private void checkClusters(NamedList results, int expectedDocCount,
+	private void checkClusters(List results, int expectedDocCount,
 			int expectedLabelCount, int expectedSubclusterCount) {
 		for (int i = 0; i < results.size(); i++) {
-			if (results.getName(i).equals("cluster")) {
-				NamedList cluster = (NamedList) results.getVal(i);
+				NamedList cluster = (NamedList) results.get(i);
 				checkCluster(cluster, expectedDocCount, expectedLabelCount,
 						expectedSubclusterCount);
-			}
 		}
 	}
 
-	private void checkClusters(NamedList results, boolean hasSubclusters) {
+	private void checkClusters(List results, boolean hasSubclusters) {
 		for (int i = 0; i < results.size(); i++) {
-			if (results.getName(i).equals("cluster")) {
-				NamedList cluster = (NamedList) results.getVal(i);
-				checkCluster(cluster, hasSubclusters);
-			}
+                  checkCluster((NamedList)results.get(i), hasSubclusters );
 		}
 	}
 
 	private void checkCluster(NamedList cluster, boolean hasSubclusters) {
-		NamedList docs = (NamedList) cluster.get("docs");
+		List docs = (List)cluster.get("docs");
 		assertNotNull("docs is null and it shouldn't be", docs);
 		for (int j = 0; j < docs.size(); j++) {
-			String id = (String) docs.getVal(j);
+			String id = (String) docs.get(j);
 			assertNotNull("id is null and it shouldn't be", id);
 		}
 
-		NamedList labels = (NamedList) cluster.get("labels");
+		List labels = (List) cluster.get("labels");
 		assertNotNull("labels is null but it shouldn't be", labels);
 
 		if (hasSubclusters) {
-			NamedList subclusters = (NamedList) cluster.get("clusters");
+			List subclusters = (List) cluster.get("clusters");
 			assertNotNull("subclusters is null but it shouldn't be", subclusters);
 		}
 	}
@@ -154,12 +153,12 @@
 			int expectedLabelCount, int expectedSubclusterCount) {
 		checkCluster(cluster, expectedSubclusterCount > 0);
 		assertEquals("number of docs in cluster", expectedDocCount,
-				((NamedList) cluster.get("docs")).size());
+				((List) cluster.get("docs")).size());
 		assertEquals("number of labels in cluster", expectedLabelCount,
-				((NamedList) cluster.get("labels")).size());
+				((List) cluster.get("labels")).size());
 
 		if (expectedSubclusterCount > 0) {
-			NamedList subclusters = (NamedList) cluster.get("clusters");
+			List subclusters = (List) cluster.get("clusters");
 			assertEquals("numClusters", expectedSubclusterCount, subclusters.size());
 			assertEquals("number of subclusters in cluster",
 					expectedSubclusterCount, subclusters.size());



Mime
View raw message