Skip to content

Apache Lucene open-source search software

SOLR-14387 add testcase for ids with separators to GetByIdTest and fix SolrClient to escape ids properly

Markus Schuch74ecc13816fb
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index dccca215632..8d0d4d6a8f8 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -21,6 +21,8 @@ Improvements
* SOLR-11725: Use corrected sample formula for computing stdDev and variance in JSON aggregations
(hossman, Munendra S N, yonik)
+* SOLR-14387: SolrClient.getById() will escape comma separater within ids (Markus Schuch via Mike Drob)
+
Other Changes
----------------------
* SOLR-10288: Remove non-minified JavaScript from the webapp. (Erik Hatcher, marcussorealheis)
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/SolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/SolrClient.java
index 0bbdc1a2ad3..11081bfd484 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/SolrClient.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/SolrClient.java
@@ -42,6 +42,7 @@ import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.NamedList;
+import org.apache.solr.common.util.StrUtils;
/**
* Abstraction through which all communication with a Solr server may be routed
@@ -1239,8 +1240,7 @@ public abstract class SolrClient implements Serializable, Closeable {
if (StringUtils.isEmpty(reqParams.get(CommonParams.QT))) {
reqParams.set(CommonParams.QT, "/get");
}
- reqParams.set("ids", ids.toArray(new String[ids.size()]));
-
+ reqParams.set("ids", ids.stream().map(id -> StrUtils.escapeTextWithSeparator(id, ',')).toArray(String[]::new));
return query(collection, reqParams).getResults();
}
diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/GetByIdTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/GetByIdTest.java
index 3078a0a0560..4ebac1efc70 100644
--- a/solr/solrj/src/test/org/apache/solr/client/solrj/GetByIdTest.java
+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/GetByIdTest.java
@@ -42,7 +42,9 @@ public class GetByIdTest extends EmbeddedSolrServerTestBase {
getSolrClient().add(Arrays.asList(
sdoc("id", "1", "term_s", "Microsoft", "term2_s", "MSFT"),
sdoc("id", "2", "term_s", "Apple", "term2_s", "AAPL"),
- sdoc("id", "3", "term_s", "Yahoo", "term2_s", "YHOO")));
+ sdoc("id", "3", "term_s", "Yahoo", "term2_s", "YHOO"),
+ sdoc("id", ",", "term_s", "b00m! 1", "term2_s", "id separator escape test document 1"),
+ sdoc("id", "1,2", "term_s", "b00m! 2", "term2_s", "id separator escape test document 2")));
getSolrClient().commit(true, true);
}
@@ -63,6 +65,21 @@ public class GetByIdTest extends EmbeddedSolrServerTestBase {
assertEquals("AAPL", rsp.get("term2_s"));
}
+ @Test
+ public void testGetIdWithSeparator() throws Exception {
+ SolrDocument rsp = getSolrClient().getById(",");
+ assertNotNull(rsp);
+ assertEquals(",", rsp.get("id"));
+ assertEquals("b00m! 1", rsp.get("term_s"));
+ assertEquals("id separator escape test document 1", rsp.get("term2_s"));
+
+ rsp = getSolrClient().getById("1,2");
+ assertNotNull(rsp);
+ assertEquals("1,2", rsp.get("id"));
+ assertEquals("b00m! 2", rsp.get("term_s"));
+ assertEquals("id separator escape test document 2", rsp.get("term2_s"));
+ }
+
@Test
public void testGetIdWithParams() throws Exception {
final SolrParams ID_FL_ONLY = params(CommonParams.FL, "id");
@@ -98,6 +115,14 @@ public class GetByIdTest extends EmbeddedSolrServerTestBase {
assertEquals("YHOO", rsp.get(2).get("term2_s"));
}
+ @Test
+ public void testGetIdsWithSeparator() throws Exception {
+ SolrDocumentList rsp = getSolrClient().getById(Arrays.asList(",", "1,2"));
+ assertEquals(2, rsp.getNumFound());
+ assertEquals(",", rsp.get(0).get("id"));
+ assertEquals("1,2", rsp.get(1).get("id"));
+ }
+
@Test
public void testGetIdsWithParams() throws Exception {
SolrDocumentList rsp = getSolrClient().getById(Arrays.asList("0", "1", "2"), params(CommonParams.FL, "id"));