- JDK 1.8.0.181
- solr 7.6.0
solr包含一个服务安装脚本(bin/install_solr_service.sh
),可以快速在Linux安装solr服务
# 将install_solr_service.sh脚本从压缩文档中提取
tar xzf solr-7.6.0.tgz solr-7.6.0/bin/install_solr_service.sh --strip-components=2
lsof
sudo yum install lsof
# 默认安装:运行程序安装至/opt 配置文件安装至/var/solr
sudo bash ./install_solr_service.sh solr-7.6.0.tgz -n
# 自定义安装路径
# -i 运行程序安装目录
# -d 配置文件安装目录
# -u 运行solr用户
# -s solr服务名称
# -p solr服务运行端口
# -n 安装完成不启动服务
sudo bash ./install_solr_service.sh solr-7.6.0.tgz -i /opt -d /var/solr -u solr -s solr -p 8983
# 查看自定义选项说明
sudo bash ./install_solr_service.sh -help
# centos7 启动命令
systemctl start solr
# centos6 启动命令
service solr start
# 命令行查看服务状态
systemctl status solr
# 管理界面查看
http://xxxx:8983
# solr用户到solr安装目录执行
./solr create -c test
ik-analyzer-x.x.x.jar
上传至/opt/solr-7.6.0/server/solr-webapp/webapp/WEB-INF/lib/
目录下# 将resources目录下的5个配置文件放入solr服务的jetty或tomcat的webapp/WEB-INF/classes/目录下(没有class目录可以新建)
---------------
①IKAnalyzer.cfg.xml
②ext.dic
③stopword.dic
④ik.conf
⑤dynamicdic.txt
---------------
# ikAnalyzer配置说明(ik.conf)
- files为动态字典列表,可以设置多个字典表,用逗号进行分隔,默认动态字典表为dynamicdic.txt;
- lastupdate默认值为0,每次对动态字典表修改后请+1,不然不会将字典表中新的词语添加到内存中,lastupdate采用的是int类型,不支持时间戳,如果使用时间戳的朋友可以把源码中的int改成long即可;
- dynamicdic.txt 为动态字典,在此文件配置的词语不需重启服务即可加载进内存中;
cd /var/solr/data/test/conf/
vi managed-schema
# 添加以下配置
# name 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>
<schema name="default-config" version="1.6">
<uniqueKey>iduniqueKey>
<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>
- springboot 2.1.1
- solr 7.5
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-solrartifactId>
dependency>
spring.data.solr.host=http://172.30.1.33:8983/solr/test
/*
* Copyright 2014-2018 nickboyer.cn.
* All rights reserved.
*/
package cn.nickboyer.demo.solr.controller;
import org.apache.solr.client.solrj.beans.Field;
import java.util.Date;
import java.util.List;
/**
* @author Kang.Y
* @Date 15:09 2018/12/11
* @since 1.8
*/
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 略...
}
/*
* Copyright 2014-2018 nickboyer.cn.
* All rights reserved.
*/
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;
/**
* @author Kang.Y
* @Date 15:08 2018/12/11
* @since 1.8
*/
@RestController
public class DemoController {
@Autowired
private SolrClient solrClient;
/**
* 新增/修改
*
* @param person
* @return
* @throws Exception
*/
@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() + "}";
}
/**
* 删除
*
* @param person
* @return
* @throws Exception
*/
@RequestMapping("delete")
public Object delete(@RequestBody Person person) throws Exception {
UpdateResponse updateResponse = solrClient.deleteById(person.getId());
return "{\"QTime\":" + updateResponse.getQTime() + "}";
}
/**
* 查询
*
* @param person
* @return
* @throws Exception
*/
@RequestMapping("select")
public Object selectById(@RequestBody Person person) throws Exception {
SolrDocument document = solrClient.getById(person.getId());
return document;
}
/**
* 搜索
*
* @param person
* @return
* @throws Exception
*/
@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());
}
// 参数sort,设置返回结果的排序规则
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;
}
}