springboot+solr基本使用

Posted 谁为含愁独不见,更教明月照流黄

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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了,然后就是大佬勿喷、欢迎吐槽

以上是关于springboot+solr基本使用的主要内容,如果未能解决你的问题,请参考以下文章

solr分布式索引实战分片配置读取:工具类configUtil.java,读取配置代码片段,配置实例

Solr 高亮是不是还可以指示返回的片段在原始字段中的位置或偏移量?

在 solr 3.4 中放置突出显示片段配置的位置

Solr Highlighting:如何为同一字段请求多个片段长度?

Springboot集成solr

springboot整合solr