Solr与项目集成

下载地址:http://www.apache.org/dyn/closer.cgi/lucene/solr/

安装路径:
apache-tomcat-6.0.41\bin\solr\collection1\conf



重点配置文件:
data-config.xml

<dataConfig> 
    <dataSource type="JdbcDataSource" 
   driver="com.mysql.jdbc.Driver" 
   url="jdbc:mysql://192.168.3.12/jqtest" 
   user="root" 
   password="123456"/> 
    <document name="content"> 
        <entity name="node" query="select newsId,title,content from News"> 
            <field column="newsId" name="newsId" />        
            <field column="title" name="title" /> 
            <field column="content" name="content" />
        </entity> 
    </document> 
</dataConfig>


schema.xml
<?xml version="1.0" encoding="UTF-8" ?> 
<schema name="example" version="1.5"> 
  <types>     

    <fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/>

 
      <fieldType name="string" class="solr.StrField" sortMissingLast="true" />
  <!-- IKAnalyzer 配置 -->
     <fieldType name="text" class="solr.TextField">
     <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
     </fieldType>
  
</types> 
 
 
<fields> 
   <field name="newsId" type="string" indexed="true" stored="true" required="true" />
   
   <field name="title" type="string" indexed="true" stored="true" multiValued="false"/> 
   <field name="content" type="string" indexed="true" stored="true" multiValued="true" /> 
   <field name="_version_" type="long" indexed="true" stored="true"/>
</fields> 
 
<uniqueKey>newsId</uniqueKey> 
<defaultSearchField>content</defaultSearchField> 
<solrQueryParser defaultOperator="OR"/> 
<copyField source="title" dest="content"/> 
<copyField source="content" dest="content"/> 

 
</schema> 


solrconfig.xml

<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">    
          <lst name="defaults">    
               <str name="config">data-config.xml</str>    
          </lst>    
  </requestHandler>


启动tomcat, 进入http://localhost:8080/solr

点击DataImport, 然后Qurey. 查询条件q: "*:*"  有结果显示.  Solr安装配置成功.

项目中导入如下包:
apache-solr-solrj-4.0.0.jar
httpclient-4.1.3.jar
httpcore-4.1.4.jar
httpmime-4.1.3.jar


测试代码:

public class SolrMain {

/**
* @param args
* @throws SolrServerException
*/
public static void main(String[] args) throws SolrServerException {
String url = "http://localhost:8080/solr";
SolrServer server = new HttpSolrServer(url);
SolrQuery query = new SolrQuery("*方案*");

    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("id: " + doc.getFieldValue("newsId"));
System.out.println("title: " + doc.getFieldValue("title"));
System.out.println();
}

}

}



运行结果:
文档个数:9
查询时间:6
id: 2
title: 关于进取

id: 11
title: 销售

id: 17
title: test-消息test

id: 18
title: test-消息01

id: 28
title: test-解决方案-政府行业

id: 29
title: test-解决方案-金融行业

id: 30
title: 市民卡解决方案

id: 31
title: 决策支持解决方案

id: 32
title: 银行卡运营外包解决方案

你可能感兴趣的:(Solr)