You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by mu...@apache.org on 2020/06/10 13:57:22 UTC

[lucene-solr] 01/02: SOLR-14550: fix duplicate issue in Atomic updates with add-distinct

This is an automated email from the ASF dual-hosted git repository.

munendrasn pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucene-solr.git

commit 001c4e2ddd515c47ec842efadad3e56c4e648c61
Author: Munendra S N <mu...@apache.org>
AuthorDate: Wed Jun 10 18:59:44 2020 +0530

    SOLR-14550: fix duplicate issue in Atomic updates with add-distinct
    
    * When add-distinct value is list, it can end up adding duplicate
      entries
---
 solr/CHANGES.txt                                   |  2 +
 .../processor/AtomicUpdateDocumentMerger.java      | 46 +++++++++++-----------
 .../solr/update/processor/AtomicUpdatesTest.java   | 15 +++++--
 3 files changed, 37 insertions(+), 26 deletions(-)

diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index cd808ad..ef87d32 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 avoid
diff --git a/solr/core/src/java/org/apache/solr/update/processor/AtomicUpdateDocumentMerger.java b/solr/core/src/java/org/apache/solr/update/processor/AtomicUpdateDocumentMerger.java
index 04f0ecd..673c4fa 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 exist
     SchemaField 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.java
index 48c76b7..5a15e10 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");