一灰灰blog 一灰灰blog
首页
  • InfluxDB
  • MongoDB
  • MySql
  • 基础系列
  • DB系列
  • 搜索系列
  • MQ系列
  • WEB系列
  • 中间件
  • 运维
  • SpringSecurity
  • SpringCloud
  • QuickAlarm
  • QuickCrawer
  • QuickFix
  • QuickMedia
  • QuickSpi
  • QuickTask
  • 高可用
  • 分类
  • 标签
  • 归档
  • 收藏
  • 关于
GitHub (opens new window)

一灰灰blog

资深搬运工
首页
  • InfluxDB
  • MongoDB
  • MySql
  • 基础系列
  • DB系列
  • 搜索系列
  • MQ系列
  • WEB系列
  • 中间件
  • 运维
  • SpringSecurity
  • SpringCloud
  • QuickAlarm
  • QuickCrawer
  • QuickFix
  • QuickMedia
  • QuickSpi
  • QuickTask
  • 高可用
  • 分类
  • 标签
  • 归档
  • 收藏
  • 关于
GitHub (opens new window)
  • 基础系列

  • DB系列

  • 搜索系列

    • Solr

      • 【搜索系列】Solr环境搭建与简单测试
        • I. Solr环境搭建
          • 1. docker方式安装solr
          • 2. schema
        • II. SpringBoot搭建solr环境
          • 1. 配置
          • 2. 简单测试
          • 3. 测试
        • III. 小结
          • 0. 项目
          • 1. 一灰灰Blog
          • 2. 声明
          • 3. 扫描关注
      • 【搜索系列】Solr之文档新增与修改使用姿势
      • 【搜索系列】Solr文档删除
      • 【搜索系列】Solr查询使用姿势小结
      • 【搜索系列】Solr身份认证与授权更新异常解决方案
    • ElasticSearch

  • MQ系列

  • WEB系列

  • 中间件

  • 运维

  • SpringSecurity

  • SpringCloud

  • Spring系列
  • 搜索系列
  • Solr
一灰灰
2019-05-10

【搜索系列】Solr环境搭建与简单测试

搜索可以说是非常常见的场景了,一般选择比较多的有solr和es,底层都是基于Lucene搜索引擎实现。之前简单的使用过solr,一直没有成体系的学习过,正好需要给一个内部项目封装统一的查询组件,借这个机会好好的撸一把solr的知识要点

# I. Solr环境搭建

# 1. docker方式安装solr

使用docker实现solr环境的搭建,快速简洁

docker pull solr
1

启动solr容器

docker run --name my-solr -d -p 8983:8983 -t solr
1

浏览器打开: http://localhost:8983/solr/#/ (opens new window)

新建core

docker exec -it --user=solr my-solr bin/solr create_core -c yhh
1

建立成功之后,终端会有相应的提示,然后刷新浏览器,可以看到新的yhh

控制台

# 2. schema

通过docker安装的最新solr版本为8.0,可以直接在http界面通过控制台来创建schema,而不需要像以前那样,进入配置文件进行添加处理;当然也可以通过修改对应的配置

假定我们现在需要在yhh这个core中存文章,结构为

id: string # 默认的全局唯一字段
title: string # 文章标题
content: string # 文章内容
type: int # 文章类型
1
2
3
4

# a. 控制台添加方式

首先进入schema的页面,可以如下操作,也可以点击连接: http://localhost:8983/solr/#/yhh/schema (opens new window)

1

然后通过点击Add Field按钮添加字段,确认按钮之后完成添加

2

添加完成之后点击please select...,弹出下拉框,看到刚才添加的东西

3

# b. 编辑xml文件方式

通过控制台的overiew可以定位到core存储路径,然后我们找到对应的定义文件,添加两个字段

create_at: long # 文章创建时间
publish_at: long # 文章发布时间
1
2

修改配置文件

# 首先进入docker内
docker exec -it --user=root my-solr /bin/bash

# 定位配置文件
vim /var/solr/data/yhh/conf/managed-schema

# 新增字段
<field name="create_at" type="pint" uninvertible="true" default="0" indexed="true" stored="true" />
<field name="publish_at" type="pint" uninvertible="true" default="0" indexed="true" stored="true" />
1
2
3
4
5
6
7
8
9

修改完成之后如下图

xml

配置文件修改之后,再去刷控制台,发现并没有显示出来,通过重启solr之后,新的才显示出来

show

# c. 功能测试

schema定义完毕之后,就可以进行简单的测试了,先加几个文档;然后再进行查询

添加文档

直接在控制台进行添加: http://localhost:8983/solr/#/yhh/documents (opens new window)

{
id: 2,
title: "一灰灰",
content: "欢迎来到一灰灰的博客",
type: 1,
create_at: 1557488164,
publish_at: 1557488164
}
1
2
3
4
5
6
7
8

添加文档

文档查询

直接在控制台进行操作:http://localhost:8983/solr/#/yhh/query (opens new window)

文档查询

然后来个高级一点的查询,我希望查询所有内容包含一灰灰的数据,可以如下查询

文档查询

# II. SpringBoot搭建solr环境

# 1. 配置

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from update -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    <java.version>1.8</java.version>
</properties>


<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>


<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-solr</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
</dependencies>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

上面的配置中,需要注意是引入包 spring-boot-starter-data-solr

引入这个包之后,我们就可以愉快的使用SolrTemplate来完成solr的各种骚操作了

package com.git.hui.boot.solr.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;

/**
 * Created by @author yihui in 19:49 19/5/10.
 */
@Configuration
public class SearchAutoConfig {

    @Bean
    @ConditionalOnMissingBean(SolrTemplate.class)
    public SolrTemplate solrTemplate(SolrClient solrClient) {
        return new SolrTemplate(solrClient);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# 2. 简单测试

下面搞一个简单的查询,看下能不能获取到solr文档

package com.git.hui.boot.solr.solr;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.stereotype.Component;


/**
 * Created by @author yihui in 19:45 19/5/10.
 */
@Component
public class SolrSearchService {
    @Autowired
    private SolrTemplate solrTemplate;

    @Data
    public static class DocDO {
        private Integer id;
        private String title;
        private String content;
        private Integer type;
        private Long create_at;
        private Long publish_at;
    }

    public void query() {
        DocDO ans = solrTemplate.getById("yhh", 2, DocDO.class).get();
        System.out.println(ans);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

# 3. 测试

启动下任务开始测试

package com.git.hui.boot.solr;

import com.git.hui.boot.solr.solr.SolrSearchService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Created by @author yihui in 19:44 19/5/10.
 */
@SpringBootApplication
public class Application {
    private SolrSearchService solrSearchService;

    public Application(SolrSearchService solrSearchService) {
        this.solrSearchService = solrSearchService;
        query();
    }

    private void query() {
        this.solrSearchService.query();
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

执行截图如下

测试输出

# III. 小结

上面介绍了最基础的solr环境搭建,springboot的solr测试环境准备,并实现了一个简单的查询实例,但距离真正上手撸solr还缺不少东西

  • solr的基础知识,前面的字段定义是否合法,索引什么的改怎么考虑
  • 配置修改,安全保证
  • 中文分词如何设置,如何使用在solr中进行使用
  • solr的增删改查的基本操作姿势
  • solr的全文搜索优势如何体现
  • SpringBoot中进行solr操作
  • ...

# 0. 项目

  • 工程:https://github.com/liuyueyi/spring-boot-demo (opens new window)
  • 项目: https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-boot/140-search-solr (opens new window)

# 1. 一灰灰Blog

  • 一灰灰Blog个人博客 https://blog.hhui.top (opens new window)
  • 一灰灰Blog-Spring专题博客 http://spring.hhui.top (opens new window)

一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛

# 2. 声明

尽信书则不如,以上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现bug或者有更好的建议,欢迎批评指正,不吝感激

  • 微博地址: 小灰灰Blog (opens new window)
  • QQ: 一灰灰/3302797840

# 3. 扫描关注

一灰灰blog

QrCode

知识星球

goals

编辑 (opens new window)
#Solr
上次更新: 2021/10/15, 19:56:22
【DB系列】SpringBoot+Mysql 无法保存emoj表情
【搜索系列】Solr之文档新增与修改使用姿势

← 【DB系列】SpringBoot+Mysql 无法保存emoj表情 【搜索系列】Solr之文档新增与修改使用姿势→

最近更新
01
【基础系列】基于maven多环境配置
04-25
02
【WEB系列】内嵌Tomcat配置Accesslog日志文件生成位置源码探索
04-24
03
【搜索系列】ES查询常用实例演示
04-18
更多文章>
Theme by Vdoing | Copyright © 2017-2022 一灰灰Blog
MIT License | 鄂ICP备18017282号 |
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×