Springboot使用solr

pom中引包

        <!-- solr -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-solr</artifactId>
            <version>2.1.8.RELEASE</version>
        </dependency>

在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下配置

在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<Articles> 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<Articles> 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<Articles> pages = solrTemplate.queryForPage("articles", query, Articles.class);
        System.out.println("pages.getTotalElements() = " + pages.getTotalElements());
        List<Articles> content = pages.getContent();
        return content;
    }
    
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值