##安装环境及版本:
- 系统:ubuntu 18.04 LTS
- jdk: 1.8.0
- Tomcat: 9.0.22
- Solr: 8.2.0
- 集群版:https://www.jianshu.com/p/91e6e4cc111d
一. 官方方式(推荐)
- 下载压缩包,上传并解压
Solr: https://lucene.apache.org/solr/downloads.html
官方安装教程:https://lucene.apache.org/solr/guide/8_1/installing-solr.html - 启动bin/solr create -c
1
2
3
4
5// -force 显示进度
bin/solr start -force
```
![image.png](https://upload-images.jianshu.io/upload_images/2803682-b99f655da01ee7ee.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
3. 创建核心tar zxvf apache-tomcat-9.0.22.tar.gz1
2
3
4
5
6
7
8![image.png](https://upload-images.jianshu.io/upload_images/2803682-aba83c383a8f5e30.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image.png](https://upload-images.jianshu.io/upload_images/2803682-1ba0564ae9ca7025.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
## 一. Solr+Tomcat
1 下载压缩包
tomcat :https://tomcat.apache.org/download-90.cgi
Solr: https://lucene.apache.org/solr/downloads.html
2. 上传至服务器并解压
tar zxvf solr-8.2.0.tgzcp apache-tomcat-9.0.22 /usr/local/tomcat -r1
2![image.png](https://upload-images.jianshu.io/upload_images/2803682-199c556af37e6c3e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
3. tomcat复制到/usr/local目录下//进入到solr的web项目下1
4. 把solr的web项目复制到tomcat的webapps下
cd solr-8.2.0/server/solr-webapp
//复制并重命名
cp webapp/ /usr/local/tomcat/webapps/solr -r// * 代表所有1
2
35. 复制对应jar包到tomcat/webapps/solr/WEB-INF/lib/
- solr-8.2.0/server/lib/ext 下所有jar包
- solr-8.2.0/server/lib 下metrics开头的jar包
cp .jar /usr/local/tomcat/webapps/solr/WEB-INF/lib/
cp metrics-.jar /usr/local/tomcat/webapps/solr/WEB-INF/lib/cp solr /usr/local/tomcat/solrhome -r1
6. 复制solr-8.2.0/server下的solr文件夹,到tomcat目录下solrhome
1
27. 编辑tomcat/webapps/solr/WEB-INF/web.xml
这段手动加上solr/home /usr/local/tomcat/solrhome java.lang.String 1
这段手动注释掉
Disable TRACE / TRACE Enable everything but TRACE / TRACE ./tomcat/bin/startup.sh1
2
3![image.png](https://upload-images.jianshu.io/upload_images/2803682-d915e35e69944b33.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image.png](https://upload-images.jianshu.io/upload_images/2803682-11ca1d1575bacb9a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
8. 启动1
2
3
4
5
6
7浏览器访问:http://ip:8080/solr/index.html
浏览器界面没相应的,换个浏览器试试
![image.png](https://upload-images.jianshu.io/upload_images/2803682-47e08b9978e2ff65.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
##二. 中文分词
1. 下载jar和文件:https://github.com/magese/ik-analyzer-solr
2. 将jar包放入Solr服务的Jetty或Tomcat的webapp/WEB-INF/lib/目录下;
3. 将resources目录下的5个配置文件放入solr服务的Jetty或Tomcat的webapp/WEB-INF/classes/目录下;
IKAnalyzer.cfg.xml
ext.dic
stopword.dic
ik.conf
dynamicdic.txt
1
4. 配置solrhome/new_core/conf的managed-schema,添加ik分词器,示例如下;
cp solr-8.2.0/server/solr/configsets/_default/conf /usr/local/tomcat/solrhome/new_core/ -r1
2
3
4
5
6
7
8官方安装方式的,修改路径为:solr/server/solr/configsets/_default/conf,然后还需要重新建立collection才行。
5. 重启服务器
![image.png](https://upload-images.jianshu.io/upload_images/2803682-a29a55417c714e29.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
##三 问题解决:
1 Can't find resource 'schema.xml' in classpath or '/usr/local/tomcat/solrhome/new_core'
![image.png](https://upload-images.jianshu.io/upload_images/2803682-b5b6edc488bc6af1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
解决方法:先复制,然后重启tomcat// solr的core名称(索引库) final String collection = "gettingstarted";1
2
## Java代码// 添加数据
@Test
public void add() throws Exception {// 创建客户端 HttpSolrClient client = new HttpSolrClient.Builder("http://192.168.234.128:8983/solr/").build(); // 添加数据 final SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", UUID.randomUUID().toString()); doc.addField("name", "我是测试数据"); // 保存并提交 client.add(collection, doc); client.commit(collection);
}
// 查询数据
@Test
public void query() throws Exception {// 创建客户端 HttpSolrClient client = new HttpSolrClient.Builder("http://192.168.234.128:8983/solr/").build(); // 查询条件 final Map<String, String> queryParamMap = new HashMap<String, String>(); queryParamMap.put("q", "*:*"); queryParamMap.put("fl", "id, name"); queryParamMap.put("sort", "id asc"); MapSolrParams queryParams = new MapSolrParams(queryParamMap); // 获取结果 final QueryResponse response = client.query(collection, queryParams); final SolrDocumentList documents = response.getResults(); for (SolrDocument document : documents) { System.out.println(document.getFirstValue("id")); System.out.println(document.getFirstValue("name")); }
}