原创文章,转载请注明出处

最近有个项目想针对现有的Mysql数据库做全文检索.Mysql本身的全文检索相对较弱,所以想寻找个替代的方案。最先考虑的肯定是Lucene,先实现起来相对复杂,自然而然的找到了构建在Lucene之上的Solr和Elasticsearch。Elasticsearch无疑是目前最为流行的数据检索的框架,但我这个项目对实时性并没有要求,且Solr方案相对简单一些。所以最终采用了Solr作为检索服务器.项目后端采用的是Spring boot,所以最终就涉及了如何在Spring boot中集成Solr的问题。

在Spring Boot项目中引入Solr

如果你使用Spring Initializr创建Spring Boot项目,那么在过程中就可以引入Solr
引入Solr

在已有项目中引入Solr,则在pom文件中加入依赖

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-solr</artifactId>
</dependency>

配置Solr数据源

假定你已经有一个部署好的Solr服务器,在application.properies文件中配置

application.properies

1
2
spring.data.solr.host=http://127.0.0.1:8393/solr/
spring.data.solr.repositories.enabled=true

使用spring data jpa访问solr

假如你的solr服务器中已经配置了一个叫做student的core,那么我们首先需要编写一个与之对应的实体

创建Student实体

Student类

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
package com.felix.springbootpractice.domain.entity;

import org.apache.solr.client.solrj.beans.Field;
import org.springframework.data.solr.core.mapping.SolrDocument;
import java.lang.annotation.Documented;

@SolrDocument(solrCoreName="student")
public class Student {
@Field("id")
Long id;
@Field("name")
String name;
@Field("age")
Integer age;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}

  • @SolrDocument(solrCoreName=”student”),solrCoreName就是core的名称
  • @Field(“id”),用于指定对应core中的字段名称

创建Repository

StudentRepository类

1
2
3
4
5
6
7
8
9
10
11
12
13
@Repository
public interface StudentRepository extends SolrCrudRepository<Student,Long> {

List<Student> findByName(String name);

@Query("*:*")
Page<Student> findAllWithPageable(Pageable pageable);

@Highlight(prefix = "</highlight>",postfix = "</highlight>")
@Query("*:*")
HighlightPage<Student> findWithHighlight();

}

我们在StudentRepository创建了三个查询方法

  • findByName,根据方法命名实现默认的查询
  • findAllWithPageable,根据@Query中的Lucene查询语法进行查询,并进行了分页操作
  • findWithHighlight,根据@Query查询且返回高亮数据

创建Service方法

StudentService类

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
@Service
public class StudentService {
@Autowired
private StudentRepository studentRepository;

public List<Student> searchByName(String name){
return studentRepository.findByName(name);
}
public List<Student> searchWithPageable(Integer pageNum,Integer pageSize){
PageRequest pageRequest = PageRequest.of(pageNum,pageSize);
return studentRepository.findAllWithPageable(pageRequest).getContent();
}
public List<Student> searchWithHighlight(Integer pageNum,Integer pageSize){
List<Student> result = new ArrayList<>();
PageRequest pageRequest = PageRequest.of(pageNum,pageSize);
HighlightPage<Student> highlightPage = studentRepository.findWithHighlight(pageRequest);
// highlightPage.getContent(); 这个是返回不带高亮数据
/*
这里处理逻辑是遍历Highlighted数据,然后把指定的字段的高亮数据替换到Entry的数据中,然后再放到返回结果列表中
*/
for(HighlightEntry<Student> highlightEntry : highlightPage.getHighlighted()){
for(HighlightEntry.Highlight highlight : highlightEntry.getHighlights() ){
if(highlight.getField().equals("name")){
highlightEntry.getEntity().setName("");
String temp = "";
for (String sl : highlight.getSnipplets()){
temp+=sl;
}
highlightEntry.getEntity().setName(temp);
}
}
result.add(highlightEntry.getEntity());
}
return result;
}
}

项目目录结构
引入Solr

至此,Spring Boot集成Solr差不多就完成了,最后Contrller调用Service就可以了.

项目代码地址: https://github.com/felix1982/spring-boot-practice/tree/master/spring-boot-solr

在日常开发过程中,特别是敏捷开发过程以及测试过程中,我们有时候不想操纵真实的数据库.那么,H2数据库是个不错的选择。

H2是一个开源的、纯java实现的关系数据库。
h2数据库特点
(1)性能、小巧
(2)同时支持网络版和嵌入式版本,另外还提供了内存版
(3)有比较好的兼容性,支持相当标准的sql标准
(4)提供了非常友好的基于web的数据库管理界面

在spring boot项目中配置H2数据库

application.properies

1
2
3
4
5
6
7
spring.datasource.url=jdbc:h2:mem:test_db
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.schema=classpath:db/schema.sql
spring.datasource.data=classpath:db/data.sql
spring.h2.console.enabled=true

  • spring.datasource.url=jdbc:h2:mem:test,配置h2数据库的连接地址
  • spring.datasource.driver-class-name=org.h2.Driver,配置JDBC Driver
  • spring.datasource.username=root,配置数据库用户名
  • spring.datasource.password=,配置数据库密码
  • spring.datasource.schema=classpath:db/schema.sql,进行该配置后,每次启动程序,程序都会运行resources/db/schema.sql文件,对数据库的结构进行操作。
  • spring.datasource.data=classpath:db/data.sql,进行该配置后,每次启动程序,程序都会运行resources/db/data.sql文件,对数据库的数据操作
  • spring.h2.console.enabled=true,开启web console功能

schema.sql

1
2
3
4
5
CREATE TABLE student(
id int not null,
name varchar(20) null,
age int null
);

data.sql

1
2
INSERT  INTO  student VALUES (1,'张三',10);
INSERT INTO student VALUES (2,'李四',16);

项目目录结构变为如下:
项目目录结构

配置完成后,启动项目后,浏览器中输入http://localhost:8080/h2-console/

H2数据库登录界面

登录后页面为:
H2数据操作界面

项目代码地址: https://github.com/felix1982/spring-boot-practice/tree/master/spring-boot-h2

这是一个开发小技巧,可以省去每次启动Spring Boot后自己打开浏览器,输入URL的麻烦。

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
@SpringBootApplication
public class SpringBootDemoApplication {

public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
String url = "http://localhost:8063";
if(Desktop.isDesktopSupported()){
Desktop desktop = Desktop.getDesktop();
try {
desktop.browse(new URI(url));
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}else{
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("rundll32 url.dll,FileProtocolHandler " + url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(url);
}

一、在windows环境下修改pip镜像源的方法(以python3.5为例)

(1):在windows文件管理器中,输入 %APPDATA%

(2):会定位到一个新的目录下,在该目录下新建pip文件夹,然后到pip文件夹里面去新建个pip.ini文件

(3):在新建的pip.ini文件中输入以下内容,搞定

1
2
3
4
[global]
timeout = 6000
index-url = http://pypi.douban.com/simple
trusted-host = pypi.douban.com

二、在linux系统中更新pip源的方式(以centos,python2.7为例)

在linux环境下的修改方式和在windows环境下修改方式基本相同,这里简单总结一下:

(1):在用户的家目录下面创建名为.pip文件夹

(2):在创建好的.pip文件夹中创建名为pip.conf的文件

(3):在pip.conf文件中输入以下内容,ok!!!

1
2
3
4
[global]
timeout = 6000
index-url = http://pypi.douban.com/simple
trusted-host = pypi.douban.com