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

SolrInputField.setValue method should not use supplied collection as backing value.

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Patch Available
    • Minor
    • Resolution: Unresolved
    • None
    • None
    • SolrJ
    • None

    Description

      The setValue method in SolrInputField takes an argument of Object. If the supplied object is a collection, then the collection is used as the backing value for the field. This can cause unexpected results when the collection is used to initialize two or more different fields.

      Consider the example where a list of values 'a', 'b', 'c' is used to initialize two fields in a SolrInputDocument.

      List<String> lst = new ArrayList<>();
      lst.add("a");
      lst.add("b");
      lst.add("c");
              
      SolrInputDocument sid = new SolrInputDocument();
      sid.addField("alpha", lst);
      sid.addField("beta", lst);
      .
      .  {add more fields to doc}
      .
      sid.addField("beta", "blah");  // add another value to field 'beta'
      

      Because the same list is used to initialize both fields 'alpha' and 'beta', they not only contain the same values, but point to the same instance of the list. Therefore, if an additional value is added to one of the fields, both will contain the value.

      In the example provided, the user would expect field 'alpha' to contain values 'a', 'b', 'c'. While field 'beta' should contain fields 'a', 'b', 'c' and 'blah'. But that is not the case. Both fields point to the same instance of the list, so if a new value is added to either field, the list is updated and both fields will contain the same values ('a', 'b', 'c', 'blah').

      This is not a bug, but the intended logic of the method based on the method comment.

      /**
         * Set the value for a field.  Arrays will be converted to a collection. If
         * a collection is given, then that collection will be used as the backing
         * collection for the values.
         */
      

      This jira is a request to improve the logic of the method by not using the supplied collection as the backing value for the field. But rather, create a new collection and add the values from the supplied collection to the new collection. This way the field is not backed by the actual instance of the supplied collection, but its values only.

      Attachments

        1. SOLR-13017.patch
          7 kB
          Charles Sanders

        Activity

          People

            Unassigned Unassigned
            Charles Sanders Charles Sanders
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: