# 简介
数据库太慢
# 启动
bin目录下 solr start 后台url (opens new window)
# 创建core
solr create -c name 创建的core文件在: ~solr-8.11.0serversolr
# 配置schema
core目录下conf下managed-schema文件
uniqueKey不能改
java里面的int 对应solr的pint,注意id必须是tring
可以用postman添加字段,post方法 http://localhost:8983/solr/user_core/schema id本身就有,因此id不用再添加
代码语言:javascript复制{
"add-field":
{
"name":"name",
"type":"string",
"stored":"true"
},
"add-field":
{
"name":"email",
"type":"string",
"stored":"true"
},
"add-field":
{
"name":"phone",
"type":"string",
"stored":"true"
},
"add-field":
{
"name":"sex",
"type":"pint",
"stored":"true"
},
"add-field":
{
"name":"password",
"type":"string",
"stored":"true"
},
"add-field":
{
"name":"age",
"type":"pint",
"stored":"true"
},
"add-field":
{
"name":"create_time",
"type":"string",
"stored":"true"
}, "add-field":
{
"name":"update_time",
"type":"string",
"stored":"true"
},
}
成功返回:
代码语言:javascript复制{
"responseHeader": {
"status": 0,
"QTime": 537
}
}
core目录下conf下managed-schema文件 450行左右已经增加字段描述
代码语言:javascript复制 <field name="_nest_path_" type="_nest_path_"/>
<field name="_root_" type="string" docValues="false" indexed="true" stored="false"/>
<field name="_text_" type="text_general" multiValued="true" indexed="true" stored="false"/>
<field name="_version_" type="plong" indexed="false" stored="false"/>
<field name="age" type="pint" stored="true"/>
<field name="create_time" type="string" stored="true"/>
<field name="email" type="string" stored="true"/>
<field name="id" type="string" multiValued="false" indexed="true" required="true" stored="true"/>
<field name="name" type="string" stored="true"/>
<field name="password" type="string" stored="true"/>
<field name="phone" type="string" stored="true"/>
<field name="sex" type="pint" stored="true"/>
<field name="update_time" type="string" stored="true"/>
# 中文分词器
自带分词器比较弱 ~solr-8.11.0contribanalysis-extraslucene-libslucene-analyzers-smartcn-8.11.0.jar 复制jar包到: ~solr-8.11.0serversolr-webappwebappWEB-INFlib
代码语言:javascript复制<fieldType name="text_hmm_chinese" class="solr.TextField" positionIncrementGap="100">
<analyzer type="index">
<tokenizer class="org.apache.lucene.analysis.cn.smart.HMMChineseTokenizerFactory"/>
</analyzer>
<analyzer type="query">
<tokenizer class="org.apache.lucene.analysis.cn.smart.HMMChineseTokenizerFactory"/>
</analyzer>
</fieldType>
启动Solr服务测试分词; solr restart -p 8983
# 配置第三方分词器
- 下载jar包 (opens new window),GitHub地址:https://github.com/magese/ik-analyzer-solr
- 将ik-analyzer-8.5.0.jar放置在webapp/WEB-INF/lib/目录下
- 将resources目录下的5个配置文件放入solr服务的Jetty或Tomcat的webapp/WEB-INF/classes/目录下
- 配置Solr的managed-schema,添加ik分词器,示例如下;
<!-- ik分词器 -->
<fieldType name="text_ik" class="solr.TextField">
<analyzer type="index">
<tokenizer class="org.wltea.analyzer.lucene.IKTokenizerFactory" useSmart="false" conf="ik.conf"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
<analyzer type="query">
<tokenizer class="org.wltea.analyzer.lucene.IKTokenizerFactory" useSmart="true" conf="ik.conf"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
</fieldType>
结果比较
# 导入数据DIH
DIH全称是Data Import Handler 数据导入处理器,顾名思义这是向solr中导入数据的,我们的solr目的就是为了能让我们的应用程序更快的查询出用户想要的数据,而数据存储在应用中的各种地方入xml、pdf、关系数据库中,那么solr首先就要能够获取这些数据并在这些数据中建立索引来达成快速搜索的目的,这里就列举我们最常用的从关系型数据库中向solr导入索引数据。 ~solr-8.11.0serversolruser_coreconf solrconfig.xml添加配置
代码语言:javascript复制<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">db-data-config.xml</str>
</lst>
</requestHandler>
~solr-8.11.0serversolruser_coreconf db-data-config.xml配置数据源
代码语言:javascript复制<dataConfig>
<dataSource driver="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1:3306/solr" user="root" password="mysql" />
<document>
<entity name="user" query="select * from t_user" pk="id">
<field column="id" name="id" />
<field name="age" column="age" />
<field name="create_time" column="create_time" />
<field name="email" column="email" />
<field name="name" column="name" />
<field name="password" column="password" />
<field name="phone" column="phone" />
<field name="sex" column="sex" />
<field name="update_time" column="update_time" />
</entity>
</document>
</dataConfig>
# 添加驱动
- 复制Solr解压后dist目录中的:solr-dataimporthandler-8.11.0粘贴到contrib/dataimporthandler/lib目录下, lib目录需要手动创建。
- 复制所用数据库服务器的相应jar包粘贴到contrib/db/lib目录下, db/lib目录需要手动创建。
solrconfig.xml添加驱动
代码语言:javascript复制<!-- 配置加入数据导入、数据库驱动的jar包 -->
<lib dir="${solr.install.dir:../..}/contrib/dataimporthandler/lib" regex=".*.jar"/>
<lib dir="${solr.install.dir:../..}/contrib/db/lib" regex=".*.jar"/>
默认0.5g内存 启动时需要增加内存 solr restart -p 8983 -m 6g 否则导入数据时会报内存溢出错误
mysql需要7.4s
solr,瞬间就出来了
# spring Boot 继承solr
添加依赖
代码语言:javascript复制<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-solr</artifactId>
<version>2.4.13</version>
</dependency>
vo
代码语言:javascript复制package com.zr.vo;
import org.apache.solr.client.solrj.beans.Field;
import org.springframework.data.solr.core.mapping.SolrDocument;
import java.util.Date;
@SolrDocument(collection = "user")
public class User {
@Field("id")
private String id;
@Field("name")
private String name;
@Field("email")
private String email;
@Field("phone")
private String phone;
@Field("sex")
private Integer sex;
@Field("age")
private Integer age;
@Field("password")
private String password;
@Field("create_time")
private String createTime;
@Field("update_time")
private String updateTime;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public String getUpdateTime() {
return updateTime;
}
public void setUpdateTime(String updateTime) {
this.updateTime = updateTime;
}
@Override
public String toString() {
return "User{"
"id='" id '''
", name='" name '''
", email='" email '''
", phone='" phone '''
", sex=" sex
", age=" age
", password='" password '''
", createTime=" createTime
", updateTime=" updateTime
'}';
}
}
controller
代码语言:javascript复制package com.zr.controller;
import com.alibaba.fastjson.JSON;
import com.zr.vo.User;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
public class UserController {
@Autowired
private SolrClient solrClient;
@RequestMapping("/query")
public String query(String phone) {
List<User> msgList = new ArrayList<>();
SolrQuery solrQuery = new SolrQuery();
solrQuery.set("q", "phone:*" phone "*");
solrQuery.setRows(100000000);//分页
try {
Long begin = System.currentTimeMillis();
QueryResponse queryResponse = solrClient.query(solrQuery);
if (queryResponse != null) {
msgList = queryResponse.getBeans(User.class);
}
Long end = System.currentTimeMillis();
System.out.println(end - begin);
} catch (Exception e) {
e.printStackTrace();
}
return JSON.toJSONString(msgList);
}
}
其余
增删改的时候,先操作solr,再操作mysql 查询只查询solr就可以
#solr挂了
查询MySQL 修复后同步一下数据,可以修改导入sql添加筛选时间条件