Apache Lucene open-source search software
SOLR-14550: fix duplicate issue in Atomic updates with add-distinct
Munendra S N001c4e2ddd51
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex cd808ad3106..ef87d32ce7a 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -231,6 +231,8 @@ Bug Fixes* SOLR-13203: Return 400 status code on invalid dynamic field for Edismax's user Fields(Johannes Kloos, mrsoong via Munendra S N)+* SOLR-14550: Fix duplicates issue in Atomic updates with add-distinct (Thomas Corthals, Munendra S N)+Other Changes---------------------* SOLR-14197: SolrResourceLoader: marked many methods as deprecated, and in some cases rerouted exiting logic to avoiddiff --git a/solr/core/src/java/org/apache/solr/update/processor/AtomicUpdateDocumentMerger.java b/solr/core/src/java/org/apache/solr/update/processor/AtomicUpdateDocumentMerger.javaindex 04f0ecd5262..673c4fae191 100644--- a/solr/core/src/java/org/apache/solr/update/processor/AtomicUpdateDocumentMerger.java+++ b/solr/core/src/java/org/apache/solr/update/processor/AtomicUpdateDocumentMerger.java@@ -302,7 +302,7 @@ public class AtomicUpdateDocumentMerger {/**** @param completeHierarchy SolrInputDocument that represents the nested document hierarchy from its root- * @param fieldPath the path to fetch, seperated by a '/' e.g. /children/grandChildren+ * @param fieldPath the path to fetch, separated by a '/' e.g. /children/grandChildren* @return the SolrInputField of fieldPath*/public static SolrInputField getFieldFromHierarchy(SolrInputDocument completeHierarchy, String fieldPath) {@@ -355,7 +355,7 @@ public class AtomicUpdateDocumentMerger {// This can happen, despite requesting for these fields in the call to RTGC.getInputDocument, if the document was// fetched from the tlog and had all these fields (possibly because it was a full document ADD operation).if (updatedFields != null) {- Collection<String> names = new HashSet<String>(oldDocument.getFieldNames());+ Collection<String> names = new HashSet<>(oldDocument.getFieldNames());for (String fieldName: names) {if (fieldName.equals(CommonParams.VERSION_FIELD)==false && fieldName.equals(ID)==false && updatedFields.contains(fieldName)==false) {oldDocument.remove(fieldName);@@ -450,33 +450,33 @@ public class AtomicUpdateDocumentMerger {final String name = sif.getName();SolrInputField existingField = toDoc.get(name);+ // throws exception if field doesn't existSchemaField sf = schema.getField(name);- if (sf != null) {- Collection<Object> original = existingField != null ?- existingField.getValues() :- new ArrayList<>();+ Collection<Object> original = existingField != null ?+ existingField.getValues() :+ new ArrayList<>();- int initialSize = original.size();- if (fieldVal instanceof Collection) {- for (Object object : (Collection) fieldVal) {- if (!original.contains(object)) {- original.add(object);- }- }- } else {- Object object = sf.getType().toNativeType(fieldVal);- if (!original.contains(object)) {- original.add(object);+ int initialSize = original.size();+ if (fieldVal instanceof Collection) {+ for (Object object : (Collection) fieldVal) {+ Object obj = sf.getType().toNativeType(object);+ if (!original.contains(obj)) {+ original.add(obj);}}+ } else {+ Object object = sf.getType().toNativeType(fieldVal);+ if (!original.contains(object)) {+ original.add(object);+ }+ }- if (original.size() > initialSize) { // update only if more are added- if (original.size() == 1) { // if single value, pass the value instead of List- doAdd(toDoc, sif, original.toArray()[0]);- } else {- toDoc.setField(name, original);- }+ if (original.size() > initialSize) { // update only if more are added+ if (original.size() == 1) { // if single value, pass the value instead of List+ doAdd(toDoc, sif, original.toArray()[0]);+ } else {+ toDoc.setField(name, original);}}}diff --git a/solr/core/src/test/org/apache/solr/update/processor/AtomicUpdatesTest.java b/solr/core/src/test/org/apache/solr/update/processor/AtomicUpdatesTest.javaindex 48c76b77812..5a15e10daec 100644--- a/solr/core/src/test/org/apache/solr/update/processor/AtomicUpdatesTest.java+++ b/solr/core/src/test/org/apache/solr/update/processor/AtomicUpdatesTest.java@@ -989,6 +989,7 @@ public class AtomicUpdatesTest extends SolrTestCaseJ4 {SolrInputDocument doc = new SolrInputDocument();doc.setField("id", "3");doc.setField("cat", new String[]{"aaa", "ccc"});+ doc.setField("atomic_is", 10);assertU(adoc(doc));doc = new SolrInputDocument();@@ -1005,22 +1006,30 @@ public class AtomicUpdatesTest extends SolrTestCaseJ4 {doc = new SolrInputDocument();doc.setField("id", "3");doc.setField("cat", ImmutableMap.of("add-distinct", "bbb"));+ doc.setField("atomic_is", ImmutableMap.of("add-distinct", 10));assertU(adoc(doc));assertU(commit());assertQ(req("q", "cat:*", "indent", "true"), "//result[@numFound = '2']");assertQ(req("q", "cat:bbb", "indent", "true"), "//result[@numFound = '1']");- assertQ(req("q", "cat:bbb", "indent", "true"), "//doc/arr[@name='cat'][count(str)=3]");+ assertQ(req("q", "cat:bbb", "indent", "true"),+ "//doc/arr[@name='cat'][count(str)=3]",+ "//doc/arr[@name='atomic_is'][count(int)=1]"+ );doc = new SolrInputDocument();doc.setField("id", "3");- doc.setField("cat", ImmutableMap.of("add-distinct", Arrays.asList(new String[]{"bbb", "bbb"})));+ doc.setField("cat", ImmutableMap.of("add-distinct", Arrays.asList("bbb", "bbb")));+ doc.setField("atomic_is", ImmutableMap.of("add-distinct", Arrays.asList(10, 34)));assertU(adoc(doc));assertU(commit());assertQ(req("q", "cat:*", "indent", "true"), "//result[@numFound = '2']");assertQ(req("q", "cat:bbb", "indent", "true"), "//result[@numFound = '1']");- assertQ(req("q", "cat:bbb", "indent", "true"), "//doc/arr[@name='cat'][count(str)=3]"); //'bbb' already present will not be added again+ assertQ(req("q", "cat:bbb", "indent", "true"),+ "//doc/arr[@name='cat'][count(str)=3]", //'bbb' already present will not be added again+ "//doc/arr[@name='atomic_is'][count(int)=2]"+ );doc = new SolrInputDocument();doc.setField("id", "5");