一基本配置和安装:
windows7 ,64位
JDK1.8.0_31
solr-6.1.0
apache-tomcat-8.0.33-windows-x64
1、在D盘建立D:\solr文件夹
2、在刚创建的文件夹solr中创建两个文件夹起名home和server
3、copyE:\it\api\solr\solr-6.1.0\solr-6.1.0\server\solr下的文件到home文件夹中,这个及solr的相关服务
4、copyE:\it\api\solr\solr-6.1.0\solr-6.1.0\server\solr-webapp\下的文件到server文件夹中,并重命名为solr,这个及solr的应用
5、部署solr的应用到tomcat下:这里采用jndi的方式部署,其他发布方式请自行配置。
在D:\meTools\java\apache-tomcat-8.0.36\conf\Catalina\localhost文件夹下建立solr.xml
内容为
<Context path="/solr" docBase="D:\solr\server\solr" reloadable="true" >
<Environment name="solr/home" type="java.lang.String" value="D:\solr\home" override="true" />
</Context>
6、修改tomcat的conf/server.xml 内容:
<Context path="/solr" docBase="D:\solr\server\solr" reloadable="true" >
<Environment name="solr/home" type="java.lang.String" value="D:\solr\home" override="true" />
</Context>
7、把solr-6.0.0\server\lib\ext包里的jar包拷贝到tomcat8的lib文件夹中
8、启动服务
9、访问路径为 http://localhost:8080/solr/index.html,不带index.html访问不到。
【注意】请使用jdk8和tomcat8,如果使用tomcat7会报错,之前在这栽过跟头。
中途遇到的问题:1、如果启动报错查看tomcat下的logs文件,出现SFL4类似的错误,是因为第7步没有导入jar包。
2、出现Version52 类似的错误是因为你的JDK版本不对,我用的是JDK1.8,solr6.0,用JDK1.7会报版本不对这个错。
=================以下基本的配置就已经配置完了===============================
二、core配置
1、copyD:\solr\home\configsets下的basic_configs文件夹到home下并修改文件夹名字为core1
2、 在浏览器中,选择core admin,点击add core,输入一下下内容,instanceDir一定要写为上面一步中修改后的文件夹名。
solrconfig.xml 和 managed-schema 两个文件在 D:\solr\home\core1\conf 中
======权限管理这块暂时还未做测试==================
solr管理界面登录权限配置(
注:
权限配置 最好先别配置 等 测试代码连接到 solr 并成功返回后 再尝试配置
)
1、修改tomcat的tomcat-user.xml ,</tomcat-user>上面添加:
<role rolename="solr"/>
<user username="admin" password="admin" roles="solr"/>
2、 修改solr项目中的D:\projects\solr\server\webapps\solr\WEB-INF \web.xml, 在</web-app>上面添加:
<security-constraint>
<web-resource-collection>
<web-resource-name>Solr Lockdown</web-resource-name>
<url-pattern>/</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>solr</role-name>
<role-name>admin</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>Solr</realm-name>
</login-config>
3、重新启动tomcat,再使用管理界面就必须要输入用户名/密码(admin/admin)了。
客户端访问权限控制
在D:\projects\solr\server\webapps下创建:communityserver_override.config,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<Overrides>
<Override xpath="/CommunityServer/Search/Solr" mode="change" name="host" value="http://localhost:8080" />
</Overrides>
value的值为浏览器中访问地址?待确认.
三、中文分词器
mmseg4j中文分词器
1、 从网上maven库下载最新版本: mmseg4j-core-1.10.0.jar; mmseg4j-solr-2.3.0.jar(中有这二个jar, mmseg4j-analysis-1.9.1.jar不能放,否则会报java.lang.NoSuchMethodError: com.chenlb.mmseg4j.analysis.MMSegTokenizer.<init>(Lcom/chenlb/mmseg4j/Seg;)V错误,因为mmseg4j-solr-2.3.0已经包含了mmseg4j-analysis)。放在D:\projects\solr\server\webapps\solr\WEB-INF\lib\下。
2、 配置schema.xml。
schema.xml的配置也挺简单,首先需要在schema.xml文件中配置一个fieldType节点,如下:
<!– mmseg4j –>
<fieldType name="text_zh" class="solr.TextField" positionIncrementGap="100">
<span style="white-space:pre"> </span><analyzer>
<span style="white-space:pre"> </span><tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="complex" />
<span style="white-space:pre"> </span></analyzer>
</fieldType>
然后就可以在field节点中引用该filedType了,假设你有个字段叫keyWord需要支持中文分词,只需要定义示例filed节点如下:
<field name="keyWord" type="text_zh" indexed="true" stored="false" multiValued="true"/>
四、使用solrj操作solr
相关参考地址
以下是个人的测试代码:
package com.noteshare.common.solr;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.SpellCheckResponse;
import org.apache.solr.client.solrj.response.SpellCheckResponse.Collation;
import org.apache.solr.client.solrj.response.SpellCheckResponse.Correction;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TestSolr {
// solr路径
String baseUrl = null;
HttpSolrClient.Builder solrBuilder = null;
HttpSolrClient solrClient = null;
@Before
public void start() {
baseUrl = "http://localhost:8080/solr/core1";
solrBuilder = new HttpSolrClient.Builder(baseUrl);
solrClient = solrBuilder.build();
}
/**
* @throws IOException
* @throws SolrServerException
* @Description: 查询总数
* @ @author :
* Create Date : 2016年7月6日 下午3:29:22
* @throws
*/
@Test
public void search() throws SolrServerException, IOException {
SolrQuery params = new SolrQuery();
params.add("q", "*:*");
QueryResponse query = solrClient.query(params);
SolrDocumentList results = query.getResults();
long numFound = results.getNumFound();
System.out.println("总数:" + numFound);
for (SolrDocument solrDocument : results) {
// 打印
Collection<String> solrDocuments = solrDocument.getFieldNames();
for (String field : solrDocuments) {
System.out.println(field + ":" + solrDocument.get(field));
}
}
}
/**
* @throws IOException
* @throws SolrServerException
* @Description: 查询并高亮显示
* @ @author :
* Create Date : 2016年7月6日 下午4:49:17 @throws
*/
@Test
public void search01() throws SolrServerException, IOException {
SolrQuery params = new SolrQuery();
params.setQuery("测试");
params.setHighlight(true).setHighlightSnippets(1); // set other params
params.setParam("hl.fl", "title");
QueryResponse query = solrClient.query(params);
SolrDocumentList results = query.getResults();
long numFound = results.getNumFound();
System.out.println("总数:" + numFound);
//显示高亮部分
for (SolrDocument solrDocument : results) {
// id is the
// uniqueKey
// field
String id = (String) solrDocument.getFieldValue("id");
if (query.getHighlighting().get(id) != null) {
List<String> highlightSnippets = query.getHighlighting().get(id).get("title");
for (String string : highlightSnippets) {
System.out.println("高亮:--" + string);
}
}
}
}
/**
* @Title : searchByHighlight
* @Description : 高亮查询结果中的查询字段---这种高亮方式有个问题,那就是必须存储,这个还需要测试。
* @throws SolrServerException
* @throws IOException : void
* @author : xingchen
* @date : 2016年7月13日 下午10:37:01
* @throws
*/
@Test
public void searchByHighlight() throws SolrServerException, IOException {
SolrQuery params = new SolrQuery();
params.setQuery("测试");
//开启高亮
// params.setHighlight(true).setHighlightSnippets(1); // set other params
params.setHighlight(true); // set other params
//设置高亮前缀和后缀
params.setHighlightSimplePre("<span class=’highligter’>").setHighlightSimplePost("</span>").setStart(0).setRows(5);
//设置高亮区域及过滤区域
params.setParam("hl.fl", "title,summary");
QueryResponse query = solrClient.query(params);
SolrDocumentList results = query.getResults();
long numFound = results.getNumFound();
System.out.println("总数:" + numFound);
//显示高亮部分
for (SolrDocument solrDocument : results) {
System.out.println("=============================打印高亮字段格式===========================");
// id is the
// uniqueKey
// field
String id = (String) solrDocument.getFieldValue("id");
if (query.getHighlighting().get(id) != null) {
System.out.println(query.getHighlighting().get(id));
System.out.println(query.getHighlighting().get(id).get("title"));
System.out.println(query.getHighlighting().get(id).get("summary"));
System.out.println("===");
List<String> highlightSnippets = query.getHighlighting().get(id).get("title");
for (String string : highlightSnippets) {
System.out.println("高亮:–" + string);
}
}
}
}
/**
* @throws IOException
* @throws SolrServerException
* @Description: 拼写检查spellcheck
* 设置查询的handler,修改为/spell,获取查询结果,如果查询结果为0,则说明没有查询到结果,
* 可能就是输入的查询关键字有错误,
* 这样就可以根据getspellcheckresponse方法获取拼写检查的response,
* 根据这个response的一些方法就可以获取到建议的结果。
* 可以根据下面的方式获取建议的结果。在这是从collation中获取结果。
* 或者根据下面的方法获取建议结果。在这是从suggestion中获取结果。 [此方法测试不通过] @
* @author : Create Date : 2016年7月6日 下午4:21:25
* @throws
*/
@Test
public void search02() throws SolrServerException, IOException {
SolrQuery params = new SolrQuery();
params.set("qt", "/spell");
params.set("q", "text:sumsung");
QueryResponse queryResponse = solrClient.query(params);
SolrDocumentList results = queryResponse.getResults();
long numFound = results.getNumFound();
if (0 == numFound) {
System.out.println("拼写错误!");
SpellCheckResponse spellCheckResponse = queryResponse.getSpellCheckResponse();
// 方式01
List<Collation> collatedResults = spellCheckResponse.getCollatedResults();
for (Collation collation : collatedResults) {
long numberOfHits = collation.getNumberOfHits();
System.out.println("推荐的词语个数:" + numberOfHits);
List<Correction> misspellingsAndCorrections = collation.getMisspellingsAndCorrections();
for (Correction correction : misspellingsAndCorrections) {
System.out.println("原始:" + correction.getOriginal() + "推荐:" + correction.getCorrection());
}
}
// 方式02--从suggestion中获取结果。
// List<Suggestion> suggestions =
// spellCheckResponse.getSuggestions();
// for (Suggestion suggestion : suggestions) {
// int numFound2 = suggestion.getNumFound();
// System.out.println("推荐的词语个数:" + numFound2);
// List<String> suggestions2 = suggestion.getAlternatives();
// for (String string : suggestions2) {
// System.out.println(string);
// }
// }
} else {
System.out.println("一共:" + numFound);
for (SolrDocument solrDocument : results) {
Collection<String> fieldNames = solrDocument.getFieldNames();
for (String field : fieldNames) {
System.out.println(field + ":" + solrDocument.get(field));
}
}
}
}
/**
* @Title : searchByProperties
* @Description : 指定属性进行搜索
* @throws SolrServerException
* @throws IOException
* @author : xingchen
* @date : 2016年7月10日 下午2:06:40
* @throws
*/
@Test
public void searchByProperties() throws SolrServerException, IOException{
SolrQuery query = new SolrQuery();
query.setQuery("title:测试");
QueryResponse queryResponse = solrClient.query(query);
SolrDocumentList results = queryResponse.getResults();
long numFound = results.getNumFound();
System.out.println("总数:" + numFound);
for (SolrDocument solrDocument : results) {
// 打印
Collection<String> solrDocuments = solrDocument.getFieldNames();
for (String field : solrDocuments) {
System.out.println(field + ":" + solrDocument.get(field));
}
}
}
/**
* @Title : searchByPage
* @Description : 分页查询
* 基于SolrDocumentList的查询和基于bean的查询两种方式
* @throws SolrServerException
* @throws IOException : void
* @author : xingchen
* @date : 2016年7月13日 下午10:13:55
* @throws
*/
@Test
public void searchByPage() throws SolrServerException, IOException{
SolrQuery query = new SolrQuery();
query.setQuery("title:测试");
query.setStart(2);
query.setRows(5);
QueryResponse queryResponse = solrClient.query(query);
//分页结果集1---此种方法遍历拿不到总数。
List<Entity> entitys = queryResponse.getBeans(Entity.class);
//这个不能获取到总数
System.out.println(entitys.size());
System.out.println("=======================================");
//分页结果集2
SolrDocumentList results = queryResponse.getResults();
long numFound = results.getNumFound();
System.out.println("总数:" + numFound);
for (SolrDocument solrDocument : results) {
// 打印
Collection<String> solrDocuments = solrDocument.getFieldNames();
for (String field : solrDocuments) {
System.out.println(field + ":" + solrDocument.get(field));
}
}
}
/**
* @throws IOException
* @throws SolrServerException
* @Description: 普通方式添加索引
* @throws IOException
* @author :
* Create Date :2016年7月6日 下午3:48:18
* @throws
*/
@Test
public void add1() throws SolrServerException, IOException {
SolrInputDocument doc = new SolrInputDocument();
doc.setField("id", "1");
doc.setField("title", "测试01");
doc.setField("summary", "地址概要:深圳市南山区比克大厦14楼");
doc.setField("text", "此人可爱活泼,有大智慧。");
solrClient.add(doc);
solrClient.commit();
}
/**
* @throws SolrServerException
* @throws IOException
* @Description: 通过注解的方式来添加索引
* @throws IOException
* @author :
* Create Date : 2016年7月6日 下午3:57:17
* @throws
*/
@Test
public void add2() throws IOException, SolrServerException {
Entity entity = new Entity();
entity.setId("2");
entity.setSummary("测试00002-summary");
entity.setText("测试000002-text");
entity.setTitle("测试00002-title");
solrClient.addBean(entity);
solrClient.commit();
}
/**
* @Title : addByBeanList
* @Description : 基于bean的list进行添加
* @throws SolrServerException
* @throws IOException : void
* @author : xingchen
* @date : 2016年7月13日 下午10:00:10
* @throws
*/
@Test
public void addByBeanList() throws SolrServerException, IOException{
List<Entity> entityList = new ArrayList<Entity>();
for(int i = 10;i < 20;i++){
Entity entity = new Entity();
entity.setId("" + i);
entity.setSummary("测试00002-summary");
entity.setText("测试000002-text");
entity.setTitle("测试00002-title");
entityList.add(entity);
}
solrClient.addBeans(entityList);
solrClient.commit();
}
/**
* @Title : addByList
* @Description : 批量添加索引
* @throws SolrServerException
* @throws IOException
* @author : xingchen
* @date : 2016年7月10日 下午2:01:11
* @throws
*/
@Test
public void addByList() throws SolrServerException, IOException {
List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
// 待索引、查询字段 http://localhost:8080/solr53/#/connection1
String[] contents = { "Solr是一个独立的企业级搜索应用服务器",
"它对外提供类似于Web-service的API接口",
"用户可以通过http请求",
"向搜索引擎服务器提交一定格式的XML文件生成索引",
"也可以通过Http Get操作提出查找请求",
"并得到XML格式的返回结果" };
int i = 0;
for (String title : contents) {
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "id" + i);
i++;
doc.addField("title", title);
docs.add(doc);
}
solrClient.add(docs);
solrClient.commit();
}
/**
* @throws IOException
* @throws SolrServerException
* @Description: 根据id删除索引
* @throws IOException
* @author :
* Create Date : 2016年7月6日 下午4:04:17
* @throws
*/
@Test
public void delete() throws SolrServerException, IOException {
solrClient.deleteById("1");
solrClient.commit();
}
@After
public void end() throws SolrServerException, IOException {
System.out.println("-----------执行search方法------------");
search();
solrClient = null;
// 强制调用已经失去引用的对象的finalize方法
System.runFinalization();
// 告诉垃圾收集器打算进行垃圾收集,而垃圾收集器进不进行收集是不确定的
System.gc();
}
}
package com.noteshare.common.solr;
import org.apache.solr.client.solrj.beans.Field;
public class Entity {
@Field
private String id;
@Field
private String title;
@Field
private String summary;
@Field
private String text;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
注: 如果配置了 访问权限 下面在连接的时候 会报错
如果想要在有用户名密码的环境下连接使用 则以下代码在 连接的时候 需要添加 http的用户名密码 验证(具体添加方法 自行网络搜索 )
另:安全配置除了 添加用户名密码 外 还需要 绑定 允许访问的ip地址 为localhost 127.0.0.1 或者服务器 真实Ip地址 防止 被恶意攻击
绑定方法 自行 网络搜索
问题解决记录
如果solr启动过程中包少了类,请查看tomcat下是否有以下几个包,如果没有的话下载这几个包放到solr的WEB-INF的lib下,
之所以放在此处而不是放在tomcat的lib下是因为怕其他系统依赖的并不是这个版本的jar,以免引起冲突。
![](/uploadFiles/notePics\2016-08-06_2_65_f7be4090-c879-46fe-b4cc-4c58c71acc8b.png)