Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add multiValued field to a SolrInputDocument

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.

like image 901
Sal81 Avatar asked Jan 29 '14 17:01

Sal81


1 Answers

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);
like image 105
benjammin Avatar answered Oct 05 '22 07:10

benjammin