MarcA
MarcA

Reputation: 227

Solr TextField to Java @Field Mapping

I am using Solr 5.2.1 (Simple stand alone instance on dev PC) and Running Solrj in a Spring Application.

I have everything working fine (Solrj Client is able to commit and retrieve the domain Model which has been annotated with @Field annotations) however I wanted to use free text search on one of the domain model's fields so I changed its type from String to a Text Field, my understanding is this will allow a more free text approach to the search instead of key:value pair type searches where solr give all or nothing back.

schema.xml snippet

<field name="description" type="text_general" indexed="true" stored="true"/>

Java bean snippet

public String getDescription() {
   return description;
}

@Field
public void setDescription(String description) {
    this.description = description;
}

The Save side worked fine, I am able to update solr vai the java bean ans solrj and using the http client I can see the data goes in, the problem I have is I can't seem to get the java bean back out when querying. Solr does not like the String based setter when it tries to populate the bean. I get the following error:

Caused by: org.apache.solr.client.solrj.beans.BindingException: Exception while setting value : [PetShop] on public void search.CompanySummarySolr.setDescription(java.lang.String)

INFO  [stdout] (http-localhost-127.0.0.1-8080-6)    at org.apache.solr.client.solrj.beans.DocumentObjectBinder$DocField.set(DocumentObjectBinder.java:447)

INFO  [stdout] (http-localhost-127.0.0.1-8080-6)    at org.apache.solr.client.solrj.beans.DocumentObjectBinder$DocField.inject(DocumentObjectBinder.java:430)

INFO  [stdout] (http-localhost-127.0.0.1-8080-6)    at org.apache.solr.client.solrj.beans.DocumentObjectBinder.getBean(DocumentObjectBinder.java:64)

INFO  [stdout] (http-localhost-127.0.0.1-8080-6)    ... 64 more

INFO  [stdout] (http-localhost-127.0.0.1-8080-6) Caused by: java.lang.IllegalArgumentException: argument type mismatch

I understand the error, I just can't find out how one would map the Text Field. In the http client the Json output it looks like an Array, but in the Solr API http://lucene.apache.org/solr/5_2_0/solr-core/org/apache/solr/schema/TextField.html it extends Object not an Array.

All I can see is I need do ether have the field as

@Field
public void setDescription(Object description) {
    this.description = (???) description;
}

In which case how do I get it back to a String?

Or I can use the org.apache.solr.schema.TextField

@Field
public void setDescription(TextField description) {
    this.description =  description.getValueSource(???, ???);
}

But I also don't really understand how to get the String back out for starters, and secondly it looks like I have to pull in the whole solr library into the project, as the TextField object is not in the Solrj library.

I am sure I have missed something, all the other fields map so simply, but I cant find an example of how to get description back to the client as a String and also allow a free text search on this field, as Strings don't allow that type of search as far as I understand.

Any help gladly appreciated

Upvotes: 1

Views: 1603

Answers (1)

MarcA
MarcA

Reputation: 227

So it would appear Solr defaults TextField to a multi valued field. This means that Solr wraps the String field I passed in with an Array, which causes the Bean Setter to break as Solr wants to return an Array with 1 String instead of a String.

To fix this simply edit your Schema.xml file and explicitly tell Solr text_general is a Single Value, so in my case the description field went

From this:

<field name="description" type="text_general" indexed="true" stored="true"/> 

To This:

<field name="description" type="text_general" indexed="true" stored="true" multiValued="false"/>

Now Solr happily converts my response into its component java bean.

Upvotes: 1

Related Questions