(一)、solr实例安装
1、安装jdk、安装tomcat
2、解压solr压缩包
3、把目录下的/solr.war部署到tomcat下,即复制到webapps下。
4、启动tomcat,会自动解压war包。
5、需要把目录下example/lib/ext的所有的jar包添加到solr工程中。
cp * /usr/local/solr/tomcat/webapps/solr/WEB-INF/lib/
6、创建solrhome。把/example/solr文件夹复制一份作为solrhome。
cp -r solr /usr/local/solr/solrhome
7.告诉solr服务solrhome的位置。需要修改web.xml

8、启动Tomcat,访问界面如下:

(二)、配置中文分析器、自定义业务域
分析器使用IKAnalyzer。使用方法如下:
1、把IKAnalyzer依赖的jar包添加到solr工程中。把分析器使用的扩展词典添加到classpath中。
2、需要自定义一个FieldType。Schema.xml中定义。可以在FieldType中指定中文分析器。命令如下
cp IKAnalyzer2012FF_u1.jar /usr/local/solr/tomcat/webapps/solr/WEB-INF/lib/
cp IKAnalyzer.cfg.xml mydict.dic /usr/local/solr/tomcat/webapps/solr/WEB-INF/classes
cd /usr/local/solr/solrhome/collection1/conf/
vi schema.xml
- 1
- 2
- 3
- 4
配置schema.xml ,添加如下:指定中文分析器
<fieldType name="text_ik" class="solr.TextField">
<analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
- 1
- 2
- 3
3、接下来自定义域,根据搜索的字段配置:
<field name="title" type="text_ik" indexed="true" stored="true"/>
<field name="sell_point" type="text_ik" indexed="true" stored="true"/>
<field name="price" type="long" indexed="true" stored="true"/>
<field name="image" type="string" indexed="false" stored="true" />
<field name="category_name" type="string" indexed="true" stored="true" />
<field name="desc" type="text_ik" indexed="true" stored="false" />
<field name="item_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>
<copyField source="title" dest="item_keywords"/>
<copyField source="sell_point" dest="item_keywords"/>
<copyField source="category_name" dest="item_keywords"/>
<copyField source="desc" dest="item_keywords"/>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
4、重启tomcat
(三)java代码中使用搜索
使用solrj实现
public class SolrJTest {
@Test
public void testSolrJ() throws Exception {
//创建连接
SolrServer solrServer = new HttpSolrServer("http://192.168.147.129:8080/solr");
//创建一个文档对象
SolrInputDocument document = new SolrInputDocument();
//添加域
document.addField("id", "solrtest01");
document.addField("title", "测试");
document.addField("sell_point", "卖点");
//添加到索引库
solrServer.add(document);
//提交
solrServer.commit();
}
@Test
public void testQuery() throws Exception {
//创建连接
SolrServer solrServer = new HttpSolrServer("http://192.168.147.129:8080/solr");
//创建一个查询对象
SolrQuery query = new SolrQuery();
query.setQuery("*:*");
//执行查询
QueryResponse response = solrServer.query(query);
//取查询结果
SolrDocumentList solrDocumentList = response.getResults();
for (SolrDocument solrDocument : solrDocumentList) {
System.out.println(solrDocument.get("id"));
System.out.println(solrDocument.get("title"));
System.out.println(solrDocument.get("sell_point"));
}
}
}
- 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
页面操作删除所有索引Document中 type选择XML
<delete>
<query>*:*</query>
</delete>
<commit/>
- 1
- 2
- 3
- 4
二、集群版
(一)、相关知识简介
搭建SolrCloud需要用到solr+zookeeper
Zookeeper用途
1、集群管理。主从的管理、负载均衡、高可用的管理。集群的入口。Zookeeper必须是集群才能保证高可用。Zookeeper有选举和投票的机制。集群中至少应该有三个节点。
2、配置文件的集中管理搭建solr集群时,需要把Solr的配置文件上传zookeeper,让zookeeper统一管理。每个节点都到zookeeper上取配置文件。
3、分布式锁
(二)搭建步骤
此次搭建一个伪分布式集群其中zookeeper三个实例、tomcat(solr)需要四个实例。
1、 搭建Zookeeper集群
1.1、需要下载zookeeper的安装包
1.2、在/usr/local/下新建solr-cloud目录,并复制三分zookeep文件
1.3、配置zookeeper
1、在zookeeper01目录下创建一个data文件夹。
2、在data目录下创建一个myid的文件
3、Myid的内容为1(02对应“2”,03对应“3”)
4、Zookeeper02、03以此类推。
dataDir=/usr/local/solr-cloud/zookeeper01/data
5、进入conf文件,把zoo_sample.cfg文件改名为zoo.cfg
6、修改zoo.cfg,把dataDir=属性指定为刚创建的data文件夹。
7、修改zoo.cfg,把clientPort指定为不冲突的端口号(01:2181、02:2182、03:2183)
8、在zoo.cfg中添加如下内容://一个是投票端口,一个是选举端口
server.1=192.168.119.129:2881:3881
server.2=192.168.119.129:2882:3882
server.3=192.168.119.129:2883:3883
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12

三个目录中都需要配置
1.4、启动zookeeper。Zookeeper的目录下有一个bin目录。使用zkServer.sh启动zookeeper服务。
启动:./zkServer.sh start
关闭:./zkServer.sh stop
查看服务状态:./zkServer.sh status
- 1
- 2
- 3
可以新建一个shell文件内容如下,一键启动三个zookeeper
./zookeeper01/bin/zkServer.sh start
./zookeeper02/bin/zkServer.sh start
./zookeeper03/bin/zkServer.sh start
- 1
- 2
- 3
2、搭建solr集群
2.1、安装四个tomcat,修改其端口号不能冲突。8080~8083
2.2、向tomcat下部署solr。把单例版的solr工程复制到tomcat下即可。
2.3、为每个solr实例创建一solrhome。
2.4、为每个solr实例关联对应的solrhome。修改web.xml

2.6、把配置文件上传到zookeeper。需要使用solr包中example/scripts/cloud-scripts/zkcli.sh命令上传配置文件。把
/usr/local/solr-cloud/solrhome01/collection1/conf目录上传到zookeeper。需要zookeeper集群已经启动。
./zkcli.sh -zkhost 192.168.119.129:2181,192.168.119.129:2182,192.168.119.129:2183 -cmd upconfig -confdir /usr/local/solr-cloud/solrhome01/collection1/conf -confname myconf
- 1
2.7、查看是否上传成功。使用zookeeper的zkCli.sh命令。在zookeeper目录bin下面,执行会默认连接
2.8、告诉solr实例zookeeper的位置。需要修改tomcat的catalina.sh,每个节点都需要添加。
JAVA_OPTS="-DzkHost=192.168.119.129:2181,192.168.119.129:2182,192.168.119.129:2183"
- 1
2.9、启动每个solr实例。
2.10集群分片。将集群分为两片,每片两个副本。
http://192.168.119.129:8080/solr/admin/collections?action=CREATE&name=collection2&numShards=2&replicationFactor=2
- 1
2.11 删除不用collection1
http://192.168.119.129:8080/solr/admin/collections?action=DELETE&name=collection1
- 1
3 、使用solrJ连接集群
@Test
public void testSolrClout() throws Exception {
//创建一个SolrServer对象
CloudSolrServer solrServer = new CloudSolrServer("192.168.119.129:2181,192.168.119.129:2182,192.168.119.129:2183");
//设置默认的collection
solrServer.setDefaultCollection("collection2");
//创建一个文档对象
SolrInputDocument document = new SolrInputDocument();
document.addField("id", "test01");
document.addField("item_title", "title1");
//添加文档
solrServer.add(document);
//提交
solrServer.commit();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
在springboot项目中使用时需要一如以下包
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>5.3.1</version>
</dependency>
<!--一般分词器,适用于英文分词-->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
<version>5.3.1</version>
</dependency>
<!--中文分词器-->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-smartcn</artifactId>
<version>5.3.1</version>
</dependency>
<!--对分词索引查询解析-->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>5.3.1</version>
</dependency>
<!--检索关键字高亮显示-->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-highlighter</artifactId>
<version>5.3.1</version>
</dependency>
<!--<dependency>-->
<!--<groupId>org.apache.solr</groupId>-->
<!--<artifactId>solr-solrj</artifactId>-->
<!--<version>6.3.0</version>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-solr</artifactId>
</dependency>
- 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
配置需要在application.properties
中配置spring.data.solr.host=http://192.168.119.129:8080/solr
以下是一个使用实例dao层:
@Autowired
private SolrClient solrClient;
@Override
public PageData search(SolrQuery query) throws Exception{
//执行查询
QueryResponse response = solrClient.query(query);
//取查询结果列表
SolrDocumentList solrDocumentList = response.getResults();
List<PageData> itemList = new ArrayList<>();
for (SolrDocument solrDocument : solrDocumentList) {
//创建一个SearchItem对象
PageData item = new PageData();
item.put("id",solrDocument.get("id"));
item.put("type",solrDocument.get("TYPE"));
item.put("title",solrDocument.get("TITLE"));
item.put("descr",solrDocument.get("DESCR"));
item.put("image",solrDocument.get("IMAGE"));
item.put("num",solrDocument.get("NUM"));
item.put("createtime",solrDocument.get("CREATETIME"));
//取高亮显示
Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
List<String> list = highlighting.get(solrDocument.get("id")).get("TITLE");
String itemTitle = "";
if (list != null && list.size() > 0) {
//取高亮后的结果
itemTitle = list.get(0);
} else {
itemTitle = (String) solrDocument.get("TITLE");
}
item.put("title",itemTitle);
//添加到列表
itemList.add(item);
}
PageData result = new PageData();
result.put("blogs",itemList);
//查询结果总数量
result.put("count",solrDocumentList.getNumFound());
return result;
}
- 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
service层如下:
@Autowired
private SearchDao searchDao;
@Override
public PageData search(String queryString, int page, int rows) throws Exception {
//创建查询条件
SolrQuery query = new SolrQuery();
//设置查询条件
query.setQuery(queryString);
//设置分页条件
// query.setStart((page-1)*rows);
// query.setRows(rows);
//设置默认搜索域
query.set("df", "TITLE");
//设置高亮
query.setHighlight(true);
query.addHighlightField("TITLE");
query.setHighlightSimplePre("<span style=\"color:#cc0000;\">");
query.setHighlightSimplePost("</span>");
//执行查询
PageData searchResult = searchDao.search(query);
//计算总页数
Long recordCount = Long.valueOf(searchResult.get("count").toString());
int pageCount = (int) (recordCount / rows);
if (recordCount % rows > 0) {
pageCount++;
}
searchResult.put("PageCount",pageCount);
searchResult.put("curPage",page);
return searchResult;
}
- 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
对于添加搜索缓存时使用如下
@Autowired
private SolrServer solrServer;
@RequestMapping("/save")
public void save(){
PageData pd = super.getPageData();
try {
List<PageData> lists = blogService.listAll(pd);
for(PageData pageData:lists){
SolrInputDocument document = new SolrInputDocument();
document.addField("id", pageData.getString("id"));
document.addField("type", pageData.getString("TYPE"));
document.addField("title", pageData.getString("TITLE"));
document.addField("desc", pageData.getString("DESCR"));
document.addField("detail", pageData.getString("DETAIL"));
document.addField("image", pageData.getString("IMAGE"));
document.addField("num", pageData.getString("NUM"));
document.addField("createtime", pageData.getString("CREATETIME"));
solrServer.add(document);
}
solrServer.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
- 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