springboot+solr基本使用

接着上一篇的搭建

 

首先需要的pom节点有

 <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-solr -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-solr</artifactId>
        </dependency>
    <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

配置实体类

package com.lhf.novel.solr;

import org.apache.solr.client.solrj.beans.Field;

import java.io.Serializable;

public class SysUser implements Serializable {

    private static final long serialVersionUID = -6874933410372889213L;

    private Long userid;

    private String username;

    private String realName;

    public Long getUserid() {
        return userid;
    }

    @Field("userid")
    public void setUserid(String userid) {
        this.userid = Long.parseLong(userid);
    }

    public String getUsername() {
        return username;
    }

    @Field("username")
    public void setUsername(String username) {
        this.username = username;
    }

    public String getRealName() {
        return realName;
    }

    @Field("realName")
    public void setRealName(String realName) {
        this.realName = realName;
    }

    @Override
    public String toString() {
        return "SysUser{" +
                "userid=" + userid +
                ", username='" + username + '\'' +
                ", realName='" + realName + '\'' +
                '}';
    }
}

再来看看controller

package com.lhf.novel.solr;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@RestController
public class Test {

    @Autowired
    private SolrClient client;

    @GetMapping("llll")
    public void test() throws IOException, SolrServerException {
        SolrQuery query = new SolrQuery("*:*");
        query.setHighlight(true);
        query.setHighlightSimplePre("<font style='red'>");
        query.setHighlightSimplePost("</font>");
        QueryResponse response = client.query("sysuser", query);
        List<SysUser> beans = response.getBeans(SysUser.class);
        beans.forEach(System.out::println);
    }
}

???对好像就是这么简单

至于配置文件那就更简单了

  data:
    solr:
      host: http://192.168.109.139:8983/solr
      repositories:
        enabled: false

这样就ojbk了,然后就是大佬勿喷、欢迎吐槽

posted @ 2020-01-15 17:44  葬月!  阅读(985)  评论(0编辑  收藏  举报