本文结合网络资源及自己的使用整理如下:
一、前期准备
1:需要solr-solrj-5.3.0.jar 包 本人使用的maven 配置如下:
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>5.3.0</version>
</dependency>
2:solr下载 各种版本下载地址 http://archive.apache.org/dist/lucene/solr/
二、Solr环境搭建
1.下载solr并解压
2.新建solr_home,将解压文件中的 server/solr 文件夹的复制到solr_home
三.配置solr_home。在solr_home/solr中新建应用 mysolr
4.将solr_home/solr/configsets/sample_techproducts_configs中的conf文件夹复制到mysolr中
5.在mysolr目录中新建core.properties内容为name=mysolr (solr中的mysolr应用)
6.以tomcat 7为solr容器,解压tomcat
7.将solr-5.3.0\server中的solr-webapp复制到tomcat的webapps目录,重命名为solr
8.复制文件
(1)将以下内容复制到tomcat/webapps/solr/WEB-INF/lib文件夹中
(2)将log4j.properties复制到tomcat/webapps/solr/WEB-INF/classes文件夹中(新建classes)
9.配置solr项目中的web.xml,打开 env-entry节点,配置solr_home
10.启动tomcat浏览器输入http://localhost:8080/solr
11.选择刚在solr_home中建的mysolr的应用、测试分词、默认分词器对中文支持不好
四、配置中文分词器(mmseg4j)
mmseg4j-solr-2.3.0支持solr5.3
1.将两个jar包考入tomcat中solr项目里的lib文件内
2.配置solr_home中mysolr域的schema.xml
新增:
<fieldtype name="textComplex" class="solr.TextField" positionIncrementGap="100">
<analyzer>
<tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="complex" dicPath="dic"/>
</analyzer>
</fieldtype>
<fieldtype name="textMaxWord" class="solr.TextField" positionIncrementGap="100">
<analyzer>
<tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="max-word" />
</analyzer>
</fieldtype>
<fieldtype name="textSimple" class="solr.TextField" positionIncrementGap="100">
<analyzer>
<tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="simple" dicPath="n:/custom/path/to/my_dic" />
</analyzer>
</fieldtype>
3.重启tomcat测试分词:(选择刚刚定义的textMaxWord)
修改solr_home中mysolr域的schema.xml,新增要用到mmseg4j分词索引的字段 content_test 分词器选择定义好的textMaxWord
<field name="content_test" type="textMaxWord" indexed="true" stored="true" multiValued="true"/>
五、Java调用Solr 5.3
public class Test {
//solr url
public static final String URL = "http://localhost:8080/solr";
//solr应用
public static final String SERVER = "mysolr";
//待索引、查询字段
public static String[] docs = { "Solr是一个独立的企业级搜索应用服务器", "它对外提供类似于Web-service的API接口",
"用户可以通过http请求", "向搜索引擎服务器提交一定格式的XML文件生成索引",
"也可以通过Http Get操作提出查找请求", "并得到XML格式的返回结果" };
public static SolrClient getSolrClient() {
return new HttpSolrClient(URL + "/" + SERVER);
}
/**
* 新建索引
*/
public static void createIndex() {
SolrClient client = getSolrClient();
int i = 0;
List<SolrInputDocument> docList = new ArrayList<SolrInputDocument>();
for (String str : docs) {
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", i++);
doc.addField("content", str);
docList.add(doc);
}
try {
client.add(docList);
client.commit();
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 搜索
*/
public static void search() {
String keyword = "(Home Furnishing)";
SolrClient client = getSolrClient();
SolrQuery query = new SolrQuery();
StringBuilder sb = new StringBuilder();
sb.append("name");
sb.append(":");
sb.append(keyword);
query.setQuery(sb.toString());
query.set("start", 0);
query.set("rows", 5);
query.setHighlight(true);
query.setHighlightSimplePre("<font color=\"red\">");
query.setHighlightSimplePost("</font>");
query.setParam("hl.fl", "name1");
QueryResponse response = null;
try {
response = client.query(query);
System.out.println(response.toString());
SolrDocumentList docs = response.getResults();
System.out.println("文档个数:" + docs.getNumFound());
System.out.println("查询时间:" + response.getQTime());
for (SolrDocument doc : docs) {
String id = (String) doc.getFieldValue("id");
if (response.getHighlighting().get(id) != null) {
System.out.println("----------------"+id);
}
}
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
// 删除索引
public static void deleteByQuery() {
SolrClient client = getSolrClient();
try {
for (int i = 9; i < 10; i++) {
// 删除所有的索引
client.deleteById(i + "");
client.commit();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// createIndex();
search();
// deleteByQuery();
}
}