You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by dw...@apache.org on 2015/06/30 16:26:31 UTC

svn commit: r1688455 - in /lucene/dev/trunk/solr: ./ solrj/src/java/org/apache/solr/client/solrj/response/ solrj/src/test-files/solrj/ solrj/src/test/org/apache/solr/client/solrj/response/

Author: dweiss
Date: Tue Jun 30 14:26:30 2015
New Revision: 1688455

URL: http://svn.apache.org/r1688455
Log:
SOLR-7724: SolrJ now supports parsing the output of the clustering component. (Alessandro Benedetti via Dawid Weiss)

Added:
    lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/response/Cluster.java   (with props)
    lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/response/ClusteringResponse.java   (with props)
    lucene/dev/trunk/solr/solrj/src/test-files/solrj/sampleClusteringResponse.xml   (with props)
    lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/response/TestClusteringResponse.java   (with props)
Modified:
    lucene/dev/trunk/solr/CHANGES.txt
    lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/response/QueryResponse.java

Modified: lucene/dev/trunk/solr/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/CHANGES.txt?rev=1688455&r1=1688454&r2=1688455&view=diff
==============================================================================
--- lucene/dev/trunk/solr/CHANGES.txt (original)
+++ lucene/dev/trunk/solr/CHANGES.txt Tue Jun 30 14:26:30 2015
@@ -109,6 +109,9 @@ Detailed Change List
 New Features
 ----------------------
 
+* SOLR-7724: SolrJ now supports parsing the output of the clustering component. 
+  (Alessandro Benedetti via Dawid Weiss)
+
 * SOLR-6273: Cross Data Center Replication. Active/passive replication for separate
   SolrClouds hosted on separate data centers. (Renaud Delbru, Yonik Seeley via Erick Erickson)
 

Added: lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/response/Cluster.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/response/Cluster.java?rev=1688455&view=auto
==============================================================================
--- lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/response/Cluster.java (added)
+++ lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/response/Cluster.java Tue Jun 30 14:26:30 2015
@@ -0,0 +1,94 @@
+package org.apache.solr.client.solrj.response;
+
+/*
+ * 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.
+ */
+
+import java.util.List;
+
+/**
+ * This class represents a cluster of Solr Docs .
+ * The cluster is produced from a set of Solr documents from the results.
+ * It is a direct mapping for the Json object Solr is returning.
+ */
+public class Cluster {
+
+  private List<String> labels;
+  private double score;
+  private List<String> docIds;
+
+  /**
+   * @param labels the list of human readable labels associated to the cluster
+   * @param score  the score produced by the clustering algorithm for the current cluster
+   * @param docIds   the list of document Ids belonging to the cluster
+   */
+  public Cluster(List<String> labels, double score, List<String> docIds) {
+    this.labels = labels;
+    this.score = score;
+    this.docIds = docIds;
+  }
+
+  @Override
+  public boolean equals(Object o) {
+    if (this == o) return true;
+    if (!(o instanceof Cluster)) return false;
+
+    Cluster cluster = (Cluster) o;
+
+    if (Double.compare(cluster.score, score) != 0) return false;
+    if (!docIds.equals(cluster.docIds)) return false;
+    if (!labels.equals(cluster.labels)) return false;
+
+    return true;
+  }
+
+  @Override
+  public int hashCode() {
+    int result;
+    long temp;
+    result = labels.hashCode();
+    temp = Double.doubleToLongBits(score);
+    result = 31 * result + (int) (temp ^ (temp >>> 32));
+    result = 31 * result + docIds.hashCode();
+    return result;
+  }
+
+  public List<String> getLabels() {
+    return labels;
+  }
+
+  public void setLabels(List<String> labels) {
+    this.labels = labels;
+  }
+
+  public double getScore() {
+    return score;
+  }
+
+  public void setScore(double score) {
+    this.score = score;
+  }
+
+  public List<String> getDocs() {
+    return docIds;
+  }
+
+  public void setDocs(List<String> docIds) {
+    this.docIds = docIds;
+  }
+
+
+}
\ No newline at end of file

Added: lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/response/ClusteringResponse.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/response/ClusteringResponse.java?rev=1688455&view=auto
==============================================================================
--- lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/response/ClusteringResponse.java (added)
+++ lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/response/ClusteringResponse.java Tue Jun 30 14:26:30 2015
@@ -0,0 +1,50 @@
+package org.apache.solr.client.solrj.response;
+/*
+ * 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.
+ */
+
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.solr.common.util.NamedList;
+
+/**
+ * Encapsulates responses from ClusteringComponent
+ */
+public class ClusteringResponse {
+
+  private static final String LABELS_NODE = "labels";
+  private static final String DOCS_NODE = "docs";
+  private static final String SCORE_NODE = "score";
+  private List<Cluster> clusters = new LinkedList<Cluster>();
+
+  public ClusteringResponse(List<NamedList<Object>> clusterInfo) {
+    for (NamedList<Object> clusterNode : clusterInfo) {
+      List<String> labelList;
+      List<String> docIdList;
+      labelList = (List<String>) clusterNode.get(LABELS_NODE);
+      double score = (double) clusterNode.get(SCORE_NODE);
+      docIdList = (List<String>) clusterNode.get(DOCS_NODE);
+      Cluster currentCluster = new Cluster(labelList, score, docIdList);
+      clusters.add(currentCluster);
+    }
+  }
+
+  public List<Cluster> getClusters() {
+    return clusters;
+  }
+
+}
\ No newline at end of file

Modified: lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/response/QueryResponse.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/response/QueryResponse.java?rev=1688455&r1=1688454&r2=1688455&view=diff
==============================================================================
--- lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/response/QueryResponse.java (original)
+++ lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/response/QueryResponse.java Tue Jun 30 14:26:30 2015
@@ -48,6 +48,7 @@ public class QueryResponse extends SolrR
   private NamedList<Object> _debugInfo = null;
   private NamedList<Object> _highlightingInfo = null;
   private NamedList<Object> _spellInfo = null;
+  private List<NamedList<Object>> _clusterInfo = null;
   private NamedList<Object> _statsInfo = null;
   private NamedList<NamedList<Number>> _termsInfo = null;
   private String _cursorMarkNext = null;
@@ -74,6 +75,9 @@ public class QueryResponse extends SolrR
   // SpellCheck Response
   private SpellCheckResponse _spellResponse = null;
 
+  // Clustering Response
+  private ClusteringResponse _clusterResponse = null;
+
   // Terms Response
   private TermsResponse _termsResponse = null;
   
@@ -87,7 +91,7 @@ public class QueryResponse extends SolrR
   // utility variable used for automatic binding -- it should not be serialized
   private transient final SolrClient solrClient;
 
-  public QueryResponse(){
+  public QueryResponse() {
     solrClient = null;
   }
   
@@ -145,6 +149,10 @@ public class QueryResponse extends SolrR
         _spellInfo = (NamedList<Object>) res.getVal( i );
         extractSpellCheckInfo( _spellInfo );
       }
+      else if ("clusters".equals(n)) {
+        _clusterInfo = (ArrayList<NamedList<Object>>) res.getVal(i);
+        extractClusteringInfo(_clusterInfo);
+      }
       else if ( "stats".equals( n ) )  {
         _statsInfo = (NamedList<Object>) res.getVal( i );
         extractStatsInfo( _statsInfo );
@@ -164,6 +172,10 @@ public class QueryResponse extends SolrR
     _spellResponse = new SpellCheckResponse(spellInfo);
   }
 
+  private void extractClusteringInfo(List<NamedList<Object>> clusterInfo) {
+    _clusterResponse = new ClusteringResponse(clusterInfo);
+  }
+
   private void extractTermsInfo(NamedList<NamedList<Number>> termsInfo) {
     _termsResponse = new TermsResponse(termsInfo);
   }
@@ -514,6 +526,10 @@ public class QueryResponse extends SolrR
     return _spellResponse;
   }
 
+  public ClusteringResponse getClusteringResponse() {
+    return _clusterResponse;
+  }
+
   public TermsResponse getTermsResponse() {
     return _termsResponse;
   }

Added: lucene/dev/trunk/solr/solrj/src/test-files/solrj/sampleClusteringResponse.xml
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/solrj/src/test-files/solrj/sampleClusteringResponse.xml?rev=1688455&view=auto
==============================================================================
--- lucene/dev/trunk/solr/solrj/src/test-files/solrj/sampleClusteringResponse.xml (added)
+++ lucene/dev/trunk/solr/solrj/src/test-files/solrj/sampleClusteringResponse.xml Tue Jun 30 14:26:30 2015
@@ -0,0 +1,93 @@
+<?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.
+  -->
+
+<response>
+  <lst name="responseHeader">
+    <int name="status">0</int>
+    <int name="QTime">10</int>
+    <lst name="params">
+      <str name="fl">id</str>
+      <str name="indent">true</str>
+      <str name="q">Test Query</str>
+      <str name="wt">xml</str>
+      <str name="hl">off</str>
+      <str name="clustering">true</str>
+      <str name="rows">5</str>
+    </lst>
+  </lst>
+  <result name="response" numFound="33" start="0">
+    <doc>
+      <str name="id">id1</str>
+    </doc>
+    <doc>
+      <str name="id">id2</str>
+    </doc>
+    <doc>
+      <str name="id">id3</str>
+    </doc>
+    <doc>
+      <str name="id">id4</str>
+    </doc>
+    <doc>
+      <str name="id">id5</str>
+    </doc>
+  </result>
+  <arr name="clusters">
+    <lst>
+      <arr name="labels">
+        <str>label1</str>
+      </arr>
+      <double name="score">0.6</double>
+      <arr name="docs">
+        <str>id1</str>
+        <str>id2</str>
+        <str>id3</str>
+      </arr>
+    </lst>
+    <lst>
+      <arr name="labels">
+        <str>label2</str>
+      </arr>
+      <double name="score">0.93</double>
+      <arr name="docs">
+        <str>id5</str>
+        <str>id6</str>
+      </arr>
+    </lst>
+    <lst>
+      <arr name="labels">
+        <str>label3</str>
+      </arr>
+      <double name="score">1.26</double>
+      <arr name="docs">
+        <str>id7</str>
+        <str>id8</str>
+      </arr>
+    </lst>
+    <lst>
+      <arr name="labels">
+        <str>label4</str>
+      </arr>
+      <double name="score">0.0</double>
+      <bool name="other-topics">true</bool>
+      <arr name="docs">
+        <str>id9</str>
+      </arr>
+    </lst>
+  </arr>
+</response>
\ No newline at end of file

Added: lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/response/TestClusteringResponse.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/response/TestClusteringResponse.java?rev=1688455&view=auto
==============================================================================
--- lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/response/TestClusteringResponse.java (added)
+++ lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/response/TestClusteringResponse.java Tue Jun 30 14:26:30 2015
@@ -0,0 +1,100 @@
+package org.apache.solr.client.solrj.response;
+/*
+ * 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.
+ */
+
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.nio.charset.StandardCharsets;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.solr.SolrJettyTestBase;
+import org.apache.solr.client.solrj.impl.XMLResponseParser;
+import org.apache.solr.common.util.NamedList;
+import org.apache.solr.core.SolrResourceLoader;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Test for ClusteringComponent's response in Solrj
+ */
+public class TestClusteringResponse extends SolrJettyTestBase {
+
+  @Test
+  public void testClusteringResponse() throws Exception {
+    XMLResponseParser parser = new XMLResponseParser();
+    /*Load a simple XML with the clustering response encoded in an XML format*/
+    InputStream is = new SolrResourceLoader(null, null).openResource("solrj/sampleClusteringResponse.xml");
+    assertNotNull(is);
+    Reader in = new InputStreamReader(is, StandardCharsets.UTF_8);
+    NamedList<Object> response = parser.processResponse(in);
+    in.close();
+
+    QueryResponse qr = new QueryResponse(response, null);
+    ClusteringResponse clusteringResponse = qr.getClusteringResponse();
+    List<Cluster> clusters = clusteringResponse.getClusters();
+    Assert.assertEquals(4, clusters.size());
+
+    //First Cluster
+    Cluster cluster1 = clusters.get(0);
+    List<String> expectedLabel1 = new LinkedList<String>();
+    expectedLabel1.add("label1");
+    List<String> expectedDocs1 = new LinkedList<String>();
+    expectedDocs1.add("id1");
+    expectedDocs1.add("id2");
+    expectedDocs1.add("id3");
+    Assert.assertEquals(expectedLabel1, cluster1.getLabels());
+    Assert.assertEquals(expectedDocs1, cluster1.getDocs());
+    Assert.assertEquals(expectedLabel1, cluster1.getLabels());
+    Assert.assertEquals(0.6, cluster1.getScore(), 0);
+    //Second Cluster
+    Cluster cluster2 = clusters.get(1);
+    List<String> expectedLabel2 = new LinkedList<String>();
+    expectedLabel2.add("label2");
+    List<String> expectedDocs2 = new LinkedList<String>();
+    expectedDocs2.add("id5");
+    expectedDocs2.add("id6");
+    Assert.assertEquals(expectedLabel2, cluster2.getLabels());
+    Assert.assertEquals(expectedDocs2, cluster2.getDocs());
+    Assert.assertEquals(expectedLabel2, cluster2.getLabels());
+    Assert.assertEquals(0.93, cluster2.getScore(), 0);
+    //Third Cluster
+    Cluster cluster3 = clusters.get(2);
+    List<String> expectedLabel3 = new LinkedList<String>();
+    expectedLabel3.add("label3");
+    List<String> expectedDocs3 = new LinkedList<String>();
+    expectedDocs3.add("id7");
+    expectedDocs3.add("id8");
+    Assert.assertEquals(expectedLabel3, cluster3.getLabels());
+    Assert.assertEquals(expectedDocs3, cluster3.getDocs());
+    Assert.assertEquals(expectedLabel3, cluster3.getLabels());
+    Assert.assertEquals(1.26, cluster3.getScore(), 0);
+    //Fourth Cluster
+    Cluster cluster4 = clusters.get(3);
+    List<String> expectedLabel4 = new LinkedList<String>();
+    expectedLabel4.add("label4");
+    List<String> expectedDocs4 = new LinkedList<String>();
+    expectedDocs4.add("id9");
+    Assert.assertEquals(expectedLabel4, cluster4.getLabels());
+    Assert.assertEquals(expectedDocs4, cluster4.getDocs());
+    Assert.assertEquals(expectedLabel4, cluster4.getLabels());
+    Assert.assertEquals(0.0, cluster4.getScore(), 0);
+
+  }
+
+}