一、Windows下安装solr-4.4.0
1、 下载solr.4.4
2、 下载绿色版tomcat6.0.18
3、 解压下载的solr到d:studysolr,将dist目录下的solr4.4.0.war包复制到tomcat的webapps下
4、 在web.xml中配置solr.home为解压的 solr中的d:studysolrexamplesolr
<env-entry>
<env-entry-name>solr/home</env-entry-name>
<env-entry-value>D:studysolrsolr-4.4.0examplesolr</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>
如果不做此配置,在启动的时候将会报异常 :
solr - org.apache.solr.common.SolrException: Could not load config for solrconfig.xml
5、 启动tomcat,访问http://localhost:8080/solr-4.4.0即可访问
6、 在左侧树中,选择collection1,会弹出节点菜单,其中documents功能表示往索引目录中添加记录,而Query可以查询指定目录。
7、 往服务中添加索引
String url = "http://localhost:8080/solr-4.4.0";
SolrServer server = new HttpSolrServer(url);
SolrInputDocument doc1 = new SolrInputDocument();
doc1.addField("id", "1");
doc1.addField("title", "信息科技");
doc1.addField("content", "企业信息门户,元数据,数字沙盘,知识管理");
server.add(docs);
server.commit();
8、 查询
public static void main(String[] args) {
String url = "http://localhost:8080/solr-4.4.0";
SolrServer server = new HttpSolrServer(url);
SolrQuery query = new SolrQuery("云南");
query.setFacetLimit(1);
//以下两个参数常用作分页时使用
query.setRows(1);// 设置每次取多少条
query.setStart(0);//设置从第几条开始查询
//以下几行设置查询结果关键字高亮显示
query.setHighlight(true);
// hl.fl参数表示哪个几Field关键字段高亮
query.setParam("hl.fl", "title, content");
query.setHighlightSimplePre("<font color=red>"); query.setHighlightSimplePost("</font"); query.setSort("name",ORDER.desc);
try {
QueryResponse response = server.query(query);
SolrDocumentList docs = response.getResults();
System.out.println("文档个数:" + docs.getNumFound());
System.out.println("查询时间:" + response.getQTime());
for (SolrDocument doc : docs) {
System.out.println("title: " + doc.getFieldValue("title"));
System.out.println("content : " + doc.getFieldValue("content "));
}
} catch (Exception e) {
e.printStackTrace();
}
}
二、在solr中配置IK中文分词器
在目录solr/example/solr/collection1/conf中的 secham.xml中types标签最后添加定义字段类型。
secham.xml文件是定义的类型以及存储的域。
<fieldType name="text_ik" class="solr.TextField">
<analyzer type="index" isMaxWordLength="false" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
<analyzer type="query" isMaxWordLength="true" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
在field中指定字段类型为上面定义的
<field name="name" type="text_ik" indexed="true" stored="true"/>
设置完比后重启服务
在admin管理台中,由于name是IK分词器,所以分词后的效果如下:

而title域是用的默认的分词器,即text_general,分词效果如下:
