springboot+ solr 快速入门指南

Solr基于Lucene的Java搜索引擎服务器
Apache Lucene项目的开源企业搜索平台。其主要功能包括全文检索、命中标示、分面搜索、动态聚类、数据库集成,以及富文本(如Word、PDF)的处理。Solr是高度可扩展的,并提供了分布式搜索和索引复制。Solr是最流行的企业级搜索引擎,Solr4 还增加了NoSQL支持。

Solr安装配置
   1.下载Solr安装包
      wget https://downloads.apache.org/lucene/solr/8.5.2/solr-8.5.2.tgz

  2.创建常用软件目录及解压到指此目录
     mkdir -p /opt/software
      tar -zxvf solr-8.5.2.tgz

 3.创建自己的collection目录(例如collection1)
    mkdir -p /opt/software/solr-8.5.2/server/solr/collection1
    cp -r /opt/software/solr-8.5.2/server/solr/configsets/sample_techproducts_configs/conf /opt/software/solr-8.5.2/server/solr/collection1/

 4.运行solr
    cd /opt/software/solr-8.5.2/bin
    ./solr start -p 8983 -force

浏览器访问solr,地址为http://localhost:8983
springboot+ solr 快速入门指南springboot+ solr 快速入门指南

添加core(选core admin,点击add core ,name、instanceDir都填写book

springboot+ solr 快速入门指南springboot+ solr 快速入门指南

 

      下一步core selector下拉选择book添加自己需要的字段(例如:type 类型pint, create_time 类型string  publish_time string)

        springboot+ solr 快速入门指南springboot+ solr 快速入门指南

springboot+solr入门代码实现:

 maven: pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.mscloudmesh.solr</groupId>
    <artifactId>springboot-solr</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-solr</name>
    <description>springboot-solr</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-solr</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
springboot+ solr 快速入门指南

application.yml配置信息:

server:
  port: 8080
spring:
  application:
    name: springboot-solr
  data:
    solr:
      host: http://192.168.1.9:8983/solr/ #192.168.1.9 换成自己安装solr服务器的ip
springboot+ solr 快速入门指南

solr配置类信息:

package com.mscloudmesh.solr.springbootsolr.config;
import org.apache.solr.client.solrj.SolrClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.solr.core.SolrTemplate;
/**
 * @author kevin
 * @date 2020/6/12
 * @desc solr配置类
 */
@Configuration
public class ESConfig {

    @Bean
    @ConditionalOnMissingBean(SolrTemplate.class)
    public SolrTemplate solrTemplate(SolrClient solrClient) {
        return new SolrTemplate(solrClient);
    }

}
springboot+ solr 快速入门指南

#实现类bookinfo
  

package com.mscloudmesh.solr.springbootsolr.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.solr.client.solrj.beans.Field;
/**
 * @author kevin
 * @date 2020/6/12
 * @desc book实体类
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BookInfo {
    @Field("id")
    private Integer id;
    @Field("title")
    private String title;
    @Field("content")
    private String content;
    @Field("type")
    private Integer type;
    @Field("create_time")
    private Long createAt;
    @Field("publish_time")
    private Long publishAt;

}
springboot+ solr 快速入门指南

#springboot入口类

package com.mscloudmesh.solr.springbootsolr;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootSolrApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootSolrApplication.class, args);
    }

}
springboot+ solr 快速入门指南

#测试controller

package com.mscloudmesh.solr.springbootsolr.controller;
import com.mscloudmesh.solr.springbootsolr.model.BookInfo;
import org.apache.solr.client.solrj.SolrResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

 
import java.util.Optional;

/**
 * @author kevin
 * @date 2020/6/12
 * @desc
 */
@RestController
public class SolrController {

    @Autowired
    private SolrTemplate template;

    @GetMapping("/add")
    public BookInfo add() {
        BookInfo bookInfo = new BookInfo(1, "三国演义", "三国时期的故事", 1, System.currentTimeMillis(), System.currentTimeMillis());
        SolrResponse response =
                template.saveBean("book", bookInfo);
        template.commit("book");
        return bookInfo;
    }

    @GetMapping("/findById")
    public Optional<BookInfo> select(String id) {
        return template.getById("book", 1, BookInfo.class);
    }

    @GetMapping("/delById")
    public int delById(String id) {
        UpdateResponse response =
                template.deleteByIds("book", id);
        template.commit("book");
        return response.getStatus();
    }

    @GetMapping("/update")
    public BookInfo update() {
        BookInfo bookInfo = new BookInfo(1, "三国演义", "修改内容", 1, System.currentTimeMillis(), System.currentTimeMillis());
        SolrResponse response = template.saveBean("book", bookInfo);
        template.commit("book");
        return bookInfo;
    }

}
springboot+ solr 快速入门指南
上一篇:linux – 在shell脚本中减去浮点数


下一篇:Solr搜索引擎的搭建与应用