Add multiValued field to a SolrInputDocument

Sal81 picture Sal81 · Jan 29, 2014 · Viewed 14.1k times · Source

We are using a solr embeded instance for Java SolrJ.

I want to add a multivalued field to a document. The multivalued field is a coma separated String.

In Java I want to do:

solrInputDocument.addField(Field1, "value1,value2,value3");

The definition for Field1 in the schema is as follow

<field name="Field1" type="multiValuedField"   indexed="true"  stored="true"  multiValued="true" required="false"/>

<fieldType name="multiValuedField" class="solr.TextField" positionIncrementGap="100">
     <analyzer type="index">
         <tokenizer class="solr.ClassicTokenizerFactory"/>
     </analyzer>
</fieldType> 

With this configuration we were expecting that when we invoke the addField method Solr was able to check that it is a multiValuedField and so it converts the String into an arrayList with the different values.

Instead we are getting an arraylist with just one value that is in fact the original string added to the document.

Question: should be the tokenizer taking care of this, or should we do it ourselves when we are adding multivalued fields to the document?

Thanks.

Answer

benjammin picture benjammin · Feb 3, 2014

The addField method of SolrInputDocument accepts a string and an object. So to handle multivalued fields, you can pass in an ArrayList with your desired values for the second parameter, and SolrJ will update the multivalued field accordingly:

String[] valuesArray = {"value1", "value2", "value3"};
ArrayList<String> values = new ArrayList<String>(Arrays.asList(valuesArray));
solrInputDocument.addField("Field1", values);