Uploaded image for project: 'Solr'
  1. Solr
  2. SOLR-4880

ClientUtils#toSolrInputDocument(SolrDocument d) creates shallow copy for multivalued fields

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: 3.6
    • Component/s: clients - java
    • Labels:
      None

      Description

      Multivalued fields are represented in SolrDocument as java.util.Collection.
      ClientUtils#toSolrInputDocument(SolrDocument d) creates shallow copy of the collections in resulted SolrInputDocument.
      That means that changes to resulted instance (i.e. adding/removing records) affect original instance as well, which is bad.

      Expected Behaviour: Deep copy of collections should be created. Changes to resulted instance shouldn't affect original instance

      Possible Implementation:

      public static SolrInputDocument toSolrInputDocument(final SolrDocument solrDocument) {
          final Map<String,SolrInputField> fields = new LinkedHashMap<String,SolrInputField>();
          return toSolrInputDocument(solrDocument, fields);
      }
      
      public static SolrInputDocument toSolrInputDocument(final SolrDocument solrDocument, final Map<String,SolrInputField> fields) {
          final SolrInputDocument result = new SolrInputDocument(fields);
          for(final Map.Entry<String, Object> entry : solrDocument.entrySet()) {
              if(entry.getValue() instanceof Collection) {
                  result.setField(entry.getKey(), new ArrayList<Object>((Collection<Object>) entry.getValue()));
              } else {
                  result.setField(entry.getKey(), entry.getValue());
              }
          }
          return result;
      }
      

      Note: Believe the same issue is true for ClientUtils#toSolrDocument(SolrInputDocument d)

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              ihrytsyuk Ivan Hrytsyuk
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: