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 快速入门指南_第1张图片

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

Springboot+ solr 快速入门指南_第2张图片

 

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

        Springboot+ solr 快速入门指南_第3张图片

springboot+solr入门代码实现:

 maven: pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.0.RELEASE
         
    
    com.mscloudmesh.solr
    springboot-solr
    0.0.1-SNAPSHOT
    springboot-solr
    springboot-solr

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

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

        
            org.projectlombok
            lombok
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

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

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);
    }

}

#实现类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入口类

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);
    }

}

#测试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 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系列)