Skip to content

Commit

Permalink
SOLR-16811: fix echo and mapUniqueKeyOnly params at /update/json/docs. (
Browse files Browse the repository at this point in the history
apache#1656)

* SOLR-16811: fix echo and mapUniqueKeyOnly params at /update/json/docs.
  • Loading branch information
mkhludnev authored May 23, 2023
1 parent 5b9cfe1 commit 8c83faa
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 22 deletions.
2 changes: 2 additions & 0 deletions solr/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ Bug Fixes

* SOLR-16808: Stop publishing environment variables via the Metrics API (Houston Putman)

* SOLR-16811: /update/json/docs?echo and mapUniqueKeyOnly params doesn't work (Mikhail Khludnev)

Dependency Upgrades
---------------------
* PR#1494: Upgrade forbiddenapis to 3.5 (Uwe Schindler)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,9 @@ public void handle(Map<String, Object> record, String path) {

private Map<String, Object> getDocMap(
Map<String, Object> record, JSONParser parser, String srcField, boolean mapUniqueKeyOnly) {
Map<String, Object> result = record;
Map<String, Object> result = mapUniqueKeyOnly ? record : new LinkedHashMap<>(record);
if (srcField != null && parser instanceof RecordingJSONParser) {
// if srcFIeld specified extract it out first
result = new LinkedHashMap<>(record);
RecordingJSONParser rjp = (RecordingJSONParser) parser;
result.put(srcField, rjp.getBuf());
rjp.resetBuf();
Expand All @@ -320,7 +319,9 @@ private Map<String, Object> getDocMap(
if (srcField != null && result.containsKey(srcField)) {
copy.put(srcField, result.remove(srcField));
}
copy.put(df, result.values());
final List<Object> deepValues = new ArrayList<>();
deepValues.addAll(result.values());
copy.put(df, deepValues);
result = copy;
}

Expand Down
131 changes: 112 additions & 19 deletions solr/core/src/test/org/apache/solr/handler/JsonLoaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
*/
package org.apache.solr.handler;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.UnaryOperator;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.SolrInputField;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.util.ContentStreamBase;
import org.apache.solr.common.util.Utils;
import org.apache.solr.handler.loader.JsonLoader;
Expand Down Expand Up @@ -300,25 +303,6 @@ public void testJsonDocFormat() throws Exception {
assertEquals("v2", obj.get("f2"));
assertTrue(obj.containsKey("f3"));

// TODO new test method
doc = "[{'id':'1'},{'id':'2'}]".replace('\'', '"');
req = req("srcField", "_src_");
req.getContext().put("path", "/update/json/docs");
rsp = new SolrQueryResponse();
p = new BufferingRequestProcessor(null);
loader = new JsonLoader();
loader.load(req, rsp, new ContentStreamBase.StringStream(doc), p);
assertEquals(2, p.addCommands.size());

content = (String) p.addCommands.get(0).solrDoc.getFieldValue("_src_");
assertNotNull(content);
obj = (Map) Utils.fromJSONString(content);
assertEquals("1", obj.get("id"));
content = (String) p.addCommands.get(1).solrDoc.getFieldValue("_src_");
assertNotNull(content);
obj = (Map) Utils.fromJSONString(content);
assertEquals("2", obj.get("id"));

// TODO new test method
String json = "{a:{" + "b:[{c:c1, e:e1},{c:c2, e :e2, d:{p:q}}]," + "x:y" + "}}";
req = req("split", "/|/a/b");
Expand All @@ -338,6 +322,115 @@ public void testJsonDocFormat() throws Exception {
p.addCommands.get(0).solrDoc.toString());
}

public void testSrcAndUniqueDocs() throws Exception {
BufferingRequestProcessor p;
JsonLoader loader;
SolrQueryRequest req;
SolrQueryResponse rsp;
String doc;
String content;
Map<?, ?> src;
doc = "[{'id':'1','a':'b'},{'id':'2','c':'d'}]".replace('\'', '"');
boolean idOnly = random().nextBoolean();
boolean srcField = random().nextBoolean();
ModifiableSolrParams params = new ModifiableSolrParams();
if (idOnly) {
params.set("mapUniqueKeyOnly", "true");
params.set("df", "_catch_all");
}
if (srcField) {
params.set("srcField", "_src_");
}
req = req(params);
req.getContext().put("path", "/update/json/docs");
rsp = new SolrQueryResponse();
p = new BufferingRequestProcessor(null);
loader = new JsonLoader();
loader.load(req, rsp, new ContentStreamBase.StringStream(doc), p);
assertEquals(2, p.addCommands.size());

{
final SolrInputDocument doc1 = p.addCommands.get(0).solrDoc;
if (srcField) {
content = (String) doc1.getFieldValue("_src_");
assertNotNull(content);
src = (Map) Utils.fromJSONString(content);
assertEquals("1", src.get("id"));
assertEquals("b", src.get("a"));
} else {
assertFalse(doc1.containsKey("_src_"));
}
assertEquals("1", doc1.getFieldValue("id"));
if (idOnly) {
assertEquals(Arrays.asList("1", "b"), new ArrayList<>(doc1.getFieldValues("_catch_all")));
assertFalse(doc1.containsKey("a"));
} else {
assertEquals("b", doc1.getFieldValue("a"));
assertFalse(doc1.containsKey("_catch_all"));
}
}
{
final SolrInputDocument doc2 = p.addCommands.get(1).solrDoc;
if (srcField) {
content = (String) doc2.getFieldValue("_src_");
assertNotNull(content);
src = (Map) Utils.fromJSONString(content);
assertEquals("2", src.get("id"));
assertEquals("d", src.get("c"));
} else {
assertFalse(doc2.containsKey("_src_"));
}
assertEquals("2", doc2.getFieldValue("id"));
if (idOnly) {
assertEquals(Arrays.asList("2", "d"), new ArrayList<>(doc2.getFieldValues("_catch_all")));
assertFalse(doc2.containsKey("c"));
} else {
assertEquals("d", doc2.getFieldValue("c"));
assertFalse(doc2.containsKey("_catch_all"));
}
}
}

public void testEchoDocs() throws Exception {
BufferingRequestProcessor p;
JsonLoader loader;
SolrQueryRequest req;
SolrQueryResponse rsp;
String doc;

doc = "[{'id':'1'},{'id':'2'}]".replace('\'', '"');
boolean src = random().nextBoolean();
req = src ? req("srcField", "_src_", "echo", "true") : req("echo", "true");
req.getContext().put("path", "/update/json/docs");
rsp = new SolrQueryResponse();
p = new BufferingRequestProcessor(null);
loader = new JsonLoader();
loader.load(req, rsp, new ContentStreamBase.StringStream(doc), p);
assertEquals(0, p.addCommands.size());
@SuppressWarnings("unchecked")
final List<Map<String, String>> docs = (List<Map<String, String>>) rsp.getValues().get("docs");
assertNotNull(docs);
assertEquals(2, docs.size());
{
Map<String, String> doc1 = docs.get(0);
assertEquals("1", doc1.get("id"));
if (src) {
assertEquals("{\"id\":\"1\"}", doc1.get("_src_"));
} else {
assertFalse(doc1.containsKey("_src_"));
}
}
{
Map<String, String> doc2 = docs.get(1);
assertEquals("2", doc2.get("id"));
if (src) {
assertEquals("{\"id\":\"2\"}", doc2.get("_src_"));
} else {
assertFalse(doc2.containsKey("_src_"));
}
}
}

private static final String PARENT_TWO_CHILDREN_JSON =
"{\n"
+ " \"id\": \"1\",\n"
Expand Down

0 comments on commit 8c83faa

Please sign in to comment.