搜索引擎solr(六)--与spring data整合

一、简介
这一节,将介绍基于spring data,对solr进行数据操作。
二、步骤
1、添加maven依赖(略写spring依赖,版本为5.0.4.RELEASE)


    org.apache.solr
    solr-solrj
    6.6.1


    org.springframework.data
    spring-data-solr
    3.0.7.RELEASE

2、定义bean类 Stu.java

package com.dragon.study.solr.bean;

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

import java.io.Serializable;
import java.util.Date;

@SolrDocument(solrCoreName = "stu")
public class Stu implements Serializable {
    private static final long serialVersionUID = 566392553987979222L;

    @Id
    @Indexed(name = "stu_id")
    private String stuId;

    @Indexed(name = "stu_name")
    private String stuName;

    @Indexed(name = "create_time")
    private Date createTime;

    public Stu() {
    }

    public Stu(String stuId, String stuName) {
        this.stuId = stuId;
        this.stuName = stuName;
    }

    public Stu(String stuId, String stuName, Date createTime) {
        this.stuId = stuId;
        this.stuName = stuName;
        this.createTime = createTime;
    }

    public String getStuId() {
        return stuId;
    }

    public void setStuId(String stuId) {
        this.stuId = stuId;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public Date getCreateTime() {
        return createTime;
    }

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

    @Override
    public String toString() {
        return "Stu{" +
                "stuId='" + stuId + '\'' +
                ", stuName='" + stuName + '\'' +
                ", createTime=" + createTime +
                '}';
    }
}

3、定义repository类 StuSolrRepository.java

package com.dragon.study.solr.repository;

import com.dragon.study.solr.bean.Stu;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.solr.core.query.result.HighlightPage;
import org.springframework.data.solr.repository.Highlight;
import org.springframework.data.solr.repository.SolrCrudRepository;

public interface StuSolrRepository extends SolrCrudRepository {

    /**
     * 根据指定属性查询,高亮显示
     * @param stuName
     * @param pageable
     * @return
     */
    @Highlight(prefix = "", postfix = "")
    HighlightPage findByStuName(String stuName, Pageable pageable);

    /**
     * 分页模糊查询
     * @param stuName
     * @param pageable
     * @return
     */
    Page findByStuNameLike(String stuName, Pageable pageable);


    /**
     * 删除数据(模糊匹配)
     * @param stuName
     */
    void deleteByStuNameLike(String stuName);
}

4、定义spring文件spring-solr.xml




    
    
    
        
    

5、数据操作

package com.dragon.study.solr;

import com.dragon.study.solr.bean.Stu;
import com.dragon.study.solr.repository.StuSolrRepository;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.solr.core.query.result.HighlightPage;

import java.util.Date;
import java.util.function.Consumer;

public class SolrSpringMain {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring-solr.xml");
        StuSolrRepository stuSolrRepository = ac.getBean(StuSolrRepository.class);

        //查询单个
        Stu stu = stuSolrRepository.findById("002").get();
        System.out.println(stu);

        //工具,输出结果
        Consumer consumer = s -> System.out.println(s);

        //查询所有
        Iterable it = stuSolrRepository.findAll();
        it.forEach(consumer);


        //分页查询所有数据
        Pageable pageable = PageRequest.of(0, 3);
        Page page = stuSolrRepository.findAll(pageable);
        System.out.println(page.getContent());

        //高亮查询
        HighlightPage highlightPage = stuSolrRepository.findByStuName("刘邦", pageable);
        System.out.println(highlightPage);

        //模糊查询
        Page likPage = stuSolrRepository.findByStuNameLike("刘", pageable);
        System.out.println(likPage);


        //保存或更新(当唯一id,即stu_id一样时)数据
//        Stu stu1 = new Stu("008", "小方", new Date());
        Stu stu1 = new Stu("009", "小明", new Date());
        stuSolrRepository.save(stu1);


        //根据唯一id删除数据
        stuSolrRepository.deleteById("008");

        //模糊匹配删除数据
        stuSolrRepository.deleteByStuNameLike("小");

        ac.close();
    }
}


你可能感兴趣的:(spring,搜索)