Springboot使用solr

pom中引包

        
        
            org.springframework.boot
            spring-boot-starter-data-solr
            2.1.8.RELEASE
        

在config文件夹下建立配置



import org.apache.solr.client.solrj.SolrClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.solr.core.SolrTemplate;

@Configuration
public class MySolrConfig {
    @Autowired
    SolrClient solrClient;

    @Bean
    public SolrTemplate getSolrTemplate() {
        return new SolrTemplate(solrClient);
    }

}

在application.properties下配置

# solr配置
spring.data.solr.host=http://localhost:8983/solr

或者在application.yml下配置

Springboot使用solr_第1张图片

在entity文件夹下建立实体类(要有get&set方法)

这个实体类里,跟solr中搜索匹配的属性加@Indexed注解,id要另外加一个@Id注解

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 = "articles")
public class Articles implements Serializable {
    @Id
    @Indexed
    private int id;
    @Indexed
    private String title;
    private String content;
    @Indexed
    private Date createDate;
}

controller中使用

import org.apache.solr.client.solrj.response.UpdateResponse;
import org.lanqiao.entity.Articles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.Criteria;
import org.springframework.data.solr.core.query.Query;
import org.springframework.data.solr.core.query.SimpleQuery;
import org.springframework.data.solr.core.query.result.ScoredPage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Optional;


/**
 * Created by David on 2019/9/7.
 */
@Controller
public class ArticlesController {
    @Autowired
    private SolrTemplate solrTemplate;

    @RequestMapping("/save")
    @ResponseBody
    public String save() {
        Articles articles = new Articles();
        articles.setId(11);
        articles.setTitle("山东济南商城");
//        articles.setCreateDate(new Date());
        solrTemplate.saveBean("articles",articles);
        solrTemplate.commit("articles");
        return "success save !";
    }

    @RequestMapping("/get")
    @ResponseBody
    public Articles selectById() {
        Optional optional = solrTemplate.getById("articles", 11, Articles.class);
        return optional.get();//optional jdk1.8+ 解决空指针问题。
    }

    @RequestMapping("/del")
    @ResponseBody
    public int delById() {
        UpdateResponse updateResponse = solrTemplate.deleteByIds("articles","11");
        return updateResponse.getStatus();//0 表示成功
    }

    @RequestMapping("/select")
    @ResponseBody
    public List select() {
        // 查询所有
        Query query = new SimpleQuery();
        
        // 设置条件
        Criteria criteria = new Criteria("name").is("张三");
        query.addCriteria(criteria);
        
        //设置分页
        query.setOffset(0l); //开始索引(默认0)
        query.setRows(2);  //每页记录数(默认10)

        //设置排序规则
        Sort sort = new Sort(Sort.Direction.ASC, "id");
        query.addSort(sort);
        
        //查询
        ScoredPage pages = solrTemplate.queryForPage("articles", query, Articles.class);
        System.out.println("pages.getTotalElements() = " + pages.getTotalElements());
        List content = pages.getContent();
        return content;
    }
    
}

 

你可能感兴趣的:(Springboot使用solr)