How do Solrj pojo Java types map to Solr types?

2019-02-27 12:45发布

问题:

I'm new to Solr, and I'm trying to use Solrj pojo's to create my documents.

Is there a mapping of Solr types to Java I should adhere to in my pojo's? is everything in the pojo a String? is there a magical mapping from Java basic types to Solr and it's transparent?

This seems like a basic question but I can't find an answer.

TIA.

回答1:

You can map pojo with solr types using @Field annotation of solrj

below is the example code

import org.apache.solr.client.solrj.beans.Field;

public class Person {

    @Field
    private int id;

    @Field("first_name")
    private String firstName;

    @Field("last_name")
    private String lastName;

    @Field
    private String age;

    // Getters & setters


}

your schema.xml should contain the fields id, first_name, last_name, age.



回答2:

The answer to this question appears to be that for Solrj beans (using @Field) the Java type of the field is a Java OBJECT that corresponds to the solr.*Field type.

For instance solr.BoolField maps to java.lang.Boolean.

Same follows for Long, Integer, Float, ...

I hope this helps the next poor soul :-)



回答3:

Solrj can stream the document as XML or Binary using the RequestWriter. The document.addField method does take an object, so it is ok to pass Java datatypes, but I have not seen a mapping. If there is one it probably assumes the basic data types. If you look at the example code it is so.

If I remember correctly (this was from a while back), we just had an issue with dates, and wrote our own formatter for it. This was from the Solr 1.2 days and that code worked fine and is still in production

http://lucene.apache.org/solr/api-4_0_0-BETA/org/apache/solr/common/SolrInputDocument.html

Here is the JavaDoc for the InputType. http://lucene.apache.org/solr/api-4_0_0-BETA/org/apache/solr/common/SolrInputField.html



标签: solr solrj