准备
- JDK 1.8.0.181
- solr 7.5.0
- zookeeper 3.4.13
安装步骤
zookeeper集群安装
- 下载zookeeper http://mirror.bit.edu.cn/apache/zookeeper
- 解压缩
zookeeper-3.4.13.tar.gz
cd /usr/local/app
tar -xf zookeeper-3.4.13.tar.gz
mv zookeeper-3.4.13 zookeeper
- 修改zoo.cfg配置文件
cd /usr/local/app/zookeeper/conf
vi zoo.cfg
--------------------------------
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/local/app/zookeeper/data
clientPort=2181
# server.1中的1位器群机器zookeeper服务的myid
server.1=172.30.1.31:2888:3888
server.2=172.30.1.32:2888:3888
server.3=172.30.1.33:2888:3888
--------------------------------
- data目录创建myid文件
cd /usr/local/app/zookeeper/
mkdir data
vi myid
--------------------------------
# 第二台机器为2,依次类推
1
--------------------------------
- 启动服务
./bin/zkServer.sh start
- 集群状态检查
# 带所有服务启动命令之后执行
./bin/zkServer.sh start
solrcloud安装
- 单机安装所有节点服务,见
solr单机版安装
- 创建chroot
# 登录任意一台solr节点服务器
cd /opt/solr/bin/
./solr zk mkroot solr -z 172.30.1.31:2181,172.30.1.32:2181,172.30.1.33:2181
- solr集群配置
# 每个节点执行
vi /etc/default/solr.in.sh
--------------------------------------------
# 修改java运行环境
SOLR_JAVA_HOME="/usr/local/jdk1.8.0_31"
# 修改solrcloud配置
ZK_HOST="172.30.1.32:2181,172.30.1.33:2181/test"
# 修改内存
SOLR_JAVA_MEM="-Xms512m -Xmx512m"
--------------------------------------------
- 创建collection
# 登录任意节点执行
cd /opt/solr/bin
# -c collection名称
# -shards 分片数量
# -replicationFactor 副本数量
./solr create_collection -c second -d _default -shards 2 -replicationFactor 3
- 修改字段配置
- 下载zookeeper管理的配置文件
# -d 配置文件下载目标目录
# -n chroot名称
# -z zookeeper集群地址
/opt/solr/bin/solr zk downconfig -n second -d ./
- 修改下载的配置文件
vi managed-schema
-----------------------------------------------------------
<?xml version="1.0" encoding="UTF-8" ?>
<schema name="default-config" version="1.6">
<uniqueKey>id</uniqueKey>
<fieldType name="binary" class="solr.BinaryField"/>
<fieldType name="boolean" class="solr.BoolField" sortMissingLast="true"/>
<fieldType name="booleans" class="solr.BoolField" sortMissingLast="true" multiValued="true"/>
<fieldType name="pdate" class="solr.DatePointField" docValues="true"/>
<fieldType name="pdates" class="solr.DatePointField" docValues="true" multiValued="true"/>
<fieldType name="pdouble" class="solr.DoublePointField" docValues="true"/>
<fieldType name="pdoubles" class="solr.DoublePointField" docValues="true" multiValued="true"/>
<fieldType name="pfloat" class="solr.FloatPointField" docValues="true"/>
<fieldType name="pfloats" class="solr.FloatPointField" docValues="true" multiValued="true"/>
<fieldType name="pint" class="solr.IntPointField" docValues="true"/>
<fieldType name="pints" class="solr.IntPointField" docValues="true" multiValued="true"/>
<fieldType name="plong" class="solr.LongPointField" docValues="true"/>
<fieldType name="plongs" class="solr.LongPointField" docValues="true" multiValued="true"/>
<fieldType name="random" class="solr.RandomSortField" indexed="true"/>
<fieldType name="string" class="solr.StrField" sortMissingLast="true" docValues="true"/>
<fieldType name="strings" class="solr.StrField" sortMissingLast="true" docValues="true" multiValued="true"/>
<fieldType name="text_general" class="solr.TextField" positionIncrementGap="100" multiValued="true">
<analyzer type="index">
<tokenizer class="solr.StandardTokenizerFactory"/>
<filter class="solr.StopFilterFactory" words="stopwords.txt" ignoreCase="true"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
<analyzer type="query">
<tokenizer class="solr.StandardTokenizerFactory"/>
<filter class="solr.StopFilterFactory" words="stopwords.txt" ignoreCase="true"/>
<filter class="solr.SynonymGraphFilterFactory" expand="true" ignoreCase="true" synonyms="synonyms.txt"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
</fieldType>
<fieldType name="text_ik" class="solr.TextField">
<analyzer type="index">
<tokenizer class="org.wltea.analyzer.lucene.IKTokenizerFactory" conf="ik.conf" useSmart="false"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
<analyzer type="query">
<tokenizer class="org.wltea.analyzer.lucene.IKTokenizerFactory" conf="ik.conf" useSmart="true"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
</fieldType>
<field name="_version_" type="plong" indexed="false" stored="false"/>
<field name="id" type="string" multiValued="false" indexed="true" required="true" stored="true"/>
<field name="name" type="string" indexed="true" stored="true"/>
<field name="intro" type="text_ik" indexed="true" stored="true"/>
<field name="hobby" type="strings" multiValued="true" indexed="false" stored="true"/>
<field name="birthday" type="pdate" indexed="true" stored="true"/>
</schema>
- 上传配置文件到zookeeper
/opt/solr/bin/solr zk upconfig -n demo -d ./
- 重启集群
客户端开始使用
导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-solr</artifactId>
</dependency>
application.properties配置
spring.data.solr.zk-host=172.30.1.31:2181,172.30.1.32:2181,172.30.1.33:2181/solr
spring.data.solr.repositories.enabled=true
entity实体
package cn.nickboyer.demo.solr.controller;
import org.apache.solr.client.solrj.beans.Field;
import java.util.Date;
import java.util.List;
public class Person {
@Field
private String id;
@Field
private String name;
@Field
private String intro;
@Field
private List<String> hobby;
@Field
private Date birthday;
@Override
public String toString() {
return "Person{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", intro='" + intro + '\'' +
", hobby=" + hobby +
", birthday=" + birthday +
'}';
}
get/set 略...
}
service调用(增删改查、搜索)
package cn.nickboyer.demo.solr.controller;
import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.UUID;
@RestController
public class DemoController {
@Autowired
private SolrClient solrClient;
@PostConstruct
public void init() {
if (solr instanceof CloudSolrClient) {
((CloudSolrClient) solr).setDefaultCollection("second");
}
}
@RequestMapping("add")
public Object add(@RequestBody Person person) throws Exception {
String id = UUID.randomUUID().toString().replaceAll("-", "");
if (StringUtils.isEmpty(person.getId())) {
person.setId(id);
}
UpdateResponse updateResponse = solrClient.addBean(person);
solrClient.commit();
return "{\"QTime\":" + updateResponse.getQTime() + "}";
}
@RequestMapping("delete")
public Object delete(@RequestBody Person person) throws Exception {
UpdateResponse updateResponse = solrClient.deleteById(person.getId());
return "{\"QTime\":" + updateResponse.getQTime() + "}";
}
@RequestMapping("select")
public Object selectById(@RequestBody Person person) throws Exception {
SolrDocument document = solrClient.getById(person.getId());
return document;
}
@RequestMapping("search")
public Object selects(@RequestBody Person person) throws Exception {
SolrQuery query = new SolrQuery();
if (StringUtils.isNotEmpty(person.getIntro())) {
query.set("q", "intro:" + person.getIntro());
}
query.setSort("id", SolrQuery.ORDER.desc);
query.setStart(0);
query.setRows(10);
QueryResponse response = solrClient.query(query);
List<Person> tmpLists = response.getBeans(Person.class);
return tmpLists;
}
}