简体   繁体   中英

How to define a custom java class as a field type in Solr

I am working on a web application using Spring + Jersey + Mongo + Solr.

I want a custom field type in solr.

For eg, below is the class that I want as a custom field type for solr.

public class Address {
    private String id;

    private String country;

    private String city;

    private String state;

    private String postalCode;

    private String roadNumber;

    private String roadName;

    private String houseNumber;

    private String locality;

    private String userId;

    private Double latitude;

    private Double longitude;

    public String getId() {
    return id;
    }

    public void setId(String id) {
    this.id = id;
    }

    public String getCountry() {
    return country;
    }

    public void setCountry(String country) {
    this.country = country;
    }

    public String getCity() {
    return city;
    }

    public void setCity(String city) {
    this.city = city;
    }

    public String getState() {
    return state;
    }

    public void setState(String state) {
    this.state = state;
    }

    public String getPostalCode() {
    return postalCode;
    }

    public void setPostalCode(String postalCode) {
    this.postalCode = postalCode;
    }

    public String getRoadNumber() {
    return roadNumber;
    }

    public void setRoadNumber(String roadNumber) {
    this.roadNumber = roadNumber;
    }

    public String getRoadName() {
    return roadName;
    }

    public void setRoadName(String roadName) {
    this.roadName = roadName;
    }

    public String getHouseNumber() {
    return houseNumber;
    }

    public void setHouseNumber(String houseNumber) {
    this.houseNumber = houseNumber;
    }

    public String getUserId() {
    return userId;
    }

    public void setUserId(String userId) {
    this.userId = userId;
    }

    public Double getLatitude() {
    return latitude;
    }

    public void setLatitude(Double latitude) {
    this.latitude = latitude;
    }

    public Double getLongitude() {
    return longitude;
    }

    public void setLongitude(Double longitude) {
    this.longitude = longitude;
    }

    public String getLocality() {
    return locality;
    }

    public void setLocality(String locality) {
    this.locality = locality;
    }

    @Override
    public String toString() {
    return "Address [id=" + id + ", country=" + country + ", city=" + city + ", state=" + state + ", postalCode=" + postalCode + ", roadNumber="
        + roadNumber + ", roadName=" + roadName + ", houseNumber=" + houseNumber + ", locality=" + locality + ", userId=" + userId + ", latitude="
        + latitude + ", longitude=" + longitude + "]";
    }

}

Say my schema will be something like (Just a dummy eg as I don't know what to do):

<field name="address" type="Address" indexed="true" stored="true" multiValued="true"/>

Is there's any way(s) to achieve this in Solr?

I don't think you want to define a custom FieldType. The schema as you have specified, I am guessing will be representing a record/document in you application. All the attributes of your POJO class will be defined as Field in schema.xml . The FieldType defines how you want to pre-process your attributes for indexing/querying. Here is sample of schema for your POJO (not complete).

<field name="id" type="keyword" indexed="true" stored="true" />
<field name="city" type="string" indexed="true" stored="true" />
<field name="country" type="string" indexed="true" stored="true" />
<field name="state" type="string" indexed="true" stored="true" />
<field name="postalCode" type="string" indexed="true" stored="true" />
<field name="roadNumber" type="test_general" indexed="true" stored="true" />

The type property in the Field tag defines the FieldType and processes text stream before indexing or querying. You can write custom Tokenizers and Filters for your specific requirement.

To add the nested documents in solr you can find more information here . Even though your schema in solr schema.xml will still be flat. You can add child documents under any document.

Hope this will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM