Apache Solr open-source search software
SOLR-7724: SolrJ now supports parsing the output of the clustering component. (Alessandro Benedetti via Dawid Weiss)
Dawid Weissb6e28103ec27
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 76822b9ceb4..8381069524e 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -109,6 +109,9 @@ Detailed Change ListNew 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 separateSolrClouds hosted on separate data centers. (Renaud Delbru, Yonik Seeley via Erick Erickson)diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/response/Cluster.java b/solr/solrj/src/java/org/apache/solr/client/solrj/response/Cluster.javanew file mode 100644index 00000000000..9f4e196d05f--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/response/Cluster.java@@ -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 filediff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/response/ClusteringResponse.java b/solr/solrj/src/java/org/apache/solr/client/solrj/response/ClusteringResponse.javanew file mode 100644index 00000000000..3f29ecd984a--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/response/ClusteringResponse.java@@ -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 filediff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/response/QueryResponse.java b/solr/solrj/src/java/org/apache/solr/client/solrj/response/QueryResponse.javaindex 59846ee80b1..fb7c7ee93e8 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/response/QueryResponse.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/response/QueryResponse.java@@ -48,6 +48,7 @@ public class QueryResponse extends SolrResponseBaseprivate 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 SolrResponseBase// SpellCheck Responseprivate SpellCheckResponse _spellResponse = null;+ // Clustering Response+ private ClusteringResponse _clusterResponse = null;+// Terms Responseprivate TermsResponse _termsResponse = null;@@ -87,7 +91,7 @@ public class QueryResponse extends SolrResponseBase// utility variable used for automatic binding -- it should not be serializedprivate transient final SolrClient solrClient;- public QueryResponse(){+ public QueryResponse() {solrClient = null;}@@ -145,6 +149,10 @@ public class QueryResponse extends SolrResponseBase_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 SolrResponseBase_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 SolrResponseBasereturn _spellResponse;}+ public ClusteringResponse getClusteringResponse() {+ return _clusterResponse;+ }+public TermsResponse getTermsResponse() {return _termsResponse;}diff --git a/solr/solrj/src/test-files/solrj/sampleClusteringResponse.xml b/solr/solrj/src/test-files/solrj/sampleClusteringResponse.xmlnew file mode 100644index 00000000000..16d6e4a9ed3--- /dev/null+++ b/solr/solrj/src/test-files/solrj/sampleClusteringResponse.xml@@ -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 filediff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/response/TestClusteringResponse.java b/solr/solrj/src/test/org/apache/solr/client/solrj/response/TestClusteringResponse.javanew file mode 100644index 00000000000..057e2c2243b--- /dev/null+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/response/TestClusteringResponse.java@@ -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);++ }++}