2
votes

I am trying to index and search documents from a Solr core using SolrJ. Solr core is running on schemaless mode.

I have the following bean to index:

public class Product {


    @Field("id")
    private String id;


    @Field(value="name")
    private String name;


    @Field(value="category")
    private String category;


    @Field(value="description")
    private String description;
    ...
}

When indexing the document it creates multiValued field strings in managed schema:

Product p = new Product();
p.setId("0001");
p.setName("Cat 1");
p.setDescription("Description");

SolrClient client = new  
 HttpSolrClient("http://localhost:8983/solr/product");
client.addBean(p);
client.commit();

Which dynamically creates mult-valued set of fields in managed-schema:

 <field name="category" type="strings"/>
 <field name="description" type="strings"/>
 <field name="name" type="strings"/>

And when trying to get the beans after searching it throws exception:

 SolrQuery query = new SolrQuery();
 query.set("q", "*:*");
 QueryResponse response = client.query(query);
 List<Product> products = response.getBeans(Product.class);

Exception:

java.lang.IllegalArgumentException: 
 Can not set java.lang.String field Product.name to java.util.ArrayList

How can I resolve this and make Solr to create single valued fields?

I am using solr-6.2.1 and solr-solrj-5.5.0.jar.

1

1 Answers

1
votes

As far as I know you'll have to actually define a schema to do that, since all fields in the schemaless mode (which guesses and creates a schema for you) will be multivalued by default.

You can do this by using the Add a new field functionality in the Solr Schema API.

curl -X POST -H 'Content-type:application/json' --data-binary '{
  "add-field":{
     "name":"category",
     "type":"strings",
     "stored":true }
}' http://localhost:8983/solr/collection/schema

.. multivalued is false by default when adding fields, so you shouldn't need to add that, but if you do, append "multiValued":false to the inner JSON structure.