1、maven配置

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-solr</artifactId>
    <version>4.0.10.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.apache.solr</groupId>
    <artifactId>solr-solrj</artifactId>
    <version>7.7.2</version>
</dependency>

2、application-solr.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:solr="http://www.springframework.org/schema/data/solr"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                https://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/data/solr
                https://www.springframework.org/schema/data/solr/spring-solr.xsd">

    <solr:solr-client id="solrClient" url="${solr.url}" />
    <solr:repositories base-package="com.zhi.demo.**.repository" />

    <bean name="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
        <constructor-arg name="solrClient" ref="solrClient" />
        <!-- 根据Indexed注解创建不存在的字段 -->
        <property name="schemaCreationFeatures">
            <list>
                <value>CREATE_MISSING_FIELDS</value>
            </list>
        </property>
    </bean>
</beans>

注意schemaCreationFeatures选择,如果有CREATE_MISSING_FIELDS选择,Spring会在启动时根据Indexed注解在solr中创建没定义的field。关键代码:

Solr7.x学习(8)-使用spring-data-solr

 

 

 3、创建Bean

    Dept.java

package com.zhi.demo.dept.model;

import java.util.Date;

import org.springframework.data.annotation.Id;
import org.springframework.data.solr.core.mapping.Indexed;
import org.springframework.data.solr.core.mapping.SolrDocument;

/**
 * 部门信息,collection为对应的core名称,Indexed注解会自动在solr添加对应的field
 * 
 * @author zhi
 * @time 2016年12月22日09:55:08
 *
 */
@SolrDocument(collection = "dept")
public class Dept {
    /**
     * 部门ID
     */
    @Id
    @Indexed
    private String id;
    /**
     * 部门名称
     */
    @Indexed(type = "text_ik")
    private String name;
    /**
     * 创建时间
     */
    @Indexed(name = "createTime", type = "pdate")
    private Date createTime;
    /**
     * 备注
     */
    @Indexed(type = "text_ik")
    private String remark;

    public Dept() {
        super();
    }

    public Dept(String id, String name, Date createTime, String remark) {
        super();
        this.id = id;
        this.name = name;
        this.createTime = createTime;
        this.remark = remark;
    }

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

}
View Code

相关文章: