solr环境建好后,可以通过数据库直接将相关内容索引,然后通过solrj进行调用,其步骤如下.
前提条件:已经建好了环境以及配置了相关中文分词。(-,二中有涉及).
1) 添加dataimport
编辑${catalina_home}\solr_config\solr\collection1\conf\solrconfig.xml,在最后添加以下内容.
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"> <lst name="defaults"> <str name="config">data-config.xml</str> </lst> </requestHandler>
2)在同一目录新建 data-config.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?> <dataConfig> <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/solr" user="root" password="123"/> <document name="messages"> <!--文件表 --> <entity name="blog" pk="id" transformer="ClobTransformer" query="select id,title,content from tb_blogs" deltaQuery="select id,title,content from tb_blogs where appllytime > '${dataimporter.last_index_time}'" > <field column="id" name="id" /> <field column="title" name="title"/> <field column="content" name="content"/> </entity> </document> </dataConfig>
3)将相关jar包copy到${catalina_home}\webapps\solr\WEB-INF\lib中,主要有三个包。mysql, solr-dataimporthandler-4.2.1.jar和solr-dataimporthandler-extras-4.2.1.jar。
4)编辑${catalina_home}\solr_config\solr\collection1\conf\schema.xml,将几个相关字段的属性添加上。因为原始文档中已经存在这几个字段名,所以此处做部分修改,此处如有不解可以参看下solr的配置文档。
<!-- <field name="title" type="text_general" indexed="true" stored="true" multiValued="true"/> --> <field name="title" type="text_ik" indexed="true" stored="true" multiValued="true"/> <!-- <field name="content" type="text_general" indexed="false" stored="true" multiValued="true"/>--> <field name="content" type="text_ik" indexed="false" stored="true" multiValued="true"/> <!--新建一个字段--> <field name="blog" type="text_ik" indexed="true" stored="true" multiValued="true"/> <!--统一搜索--> <!-- Text fields from SolrCell to search by default in our catch-all field --> <copyField source="title" dest="blog"/> <copyField source="content" dest="blog"/>
5)启动tomcat,直接访问 http://localhost:8983/solr/import?command=full-import,如果成功,会有相关消息显示有多少条数据index, 同时在${catalina_home}\solr_config\solr\collection1\conf\dataimport.properties中会产生一个dataimport.properties文件,其中记录最后index时间,这个是增量index的时间标致,标识下次index从哪里开始,原则上数据库中应该有个时间字段与其比较。手动插入几条数据,将时间设为标识之后,然后访问 /dataimport?command=delta-import,可见增量的index数据。
6)client端测试
相关jar包 solr-4.2.1\dist\solrj-lib中的所有包及solr-4.2.1\dist\solr-solrj-4.2.1.jar,执行以下程序,可看到结果。
package com.solr.client;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
//solr客户端调用
public class SolrJSearch {
String url = "http://localhost:8080/solr";
HttpSolrServer solrServer =null;
/**
* 初始化CommonsHttpSolrServer
*/
public SolrJSearch(){
solrServer = new HttpSolrServer(url);
solrServer.setMaxTotalConnections(100);
solrServer.setSoTimeout(10000);
solrServer.setConnectionTimeout(5000);
}
/**
* 全文检索方法
*/
public void search(String keywords){
SolrQuery query = new SolrQuery();
query.setQuery("中国");
/**
* solrconfig.xml
*<formatter name="html"
*default="true"
*class="solr.highlight.HtmlFormatter">
*<lst name="defaults">
*<str name="hl.simple.pre"><![CDATA[<font class='highlight'>]]></str>
*<str name="hl.simple.post"><![CDATA[</font>]]></str>
*</lst>
*</formatter>
*/
//设置合并
query.setFacet(true);
query.setHighlight(true)
.setHighlightSnippets(3)
.setHighlightSimplePre("<span class='highlight'>")//设置开头
.setHighlightSimplePost("</span>") //设置结尾
// .setParam("hl.mergeContiguous", "true")
.setParam("hl.fl", "content,title");
//.setStart(0)
//.setRows(10);//设置行数
//新加条件(and的作用对TERM_VAL的6到8折的限制)
//query.addFilterQuery("content:EditText");
//排序用的
//query.addSortField( "price", SolrQuery.ORDER.asc );
try {
QueryResponse rsp = solrServer.query( query );
SolrDocumentList docs = rsp.getResults();
System.out.println("数据匹配个数:" + docs.getNumFound());
System.out.println("查询时间:" + rsp.getQTime());
for (SolrDocument doc : docs) {
String id = (String) doc.getFieldValue("id");
if(rsp.getHighlighting().get(id)!=null){
System.out.println("id is "+id);
System.out.println(rsp.getHighlighting().get(id).get("title"));
System.out.println(rsp.getHighlighting().get(id).get("content"));
}
// String content = (String) doc.getFieldValue("content");
// String title = (String) doc.getFieldValue("title");
// System.out.println("id:"+id);
// System.out.println("content:"+content);
// System.out.println("title:"+title);
System.out.println("-------------------");
}
} catch (SolrServerException e) {
e.printStackTrace();
}
}
/**
* 测试方法
*/
public static void main(String[] args) {
SolrJSearch sj = new SolrJSearch();
sj.search("虑感");
// sj.search("比亚迪管理文件");
}
}