fox111 发表于 2016-12-16 08:18:46

使用solr为数据库构建全文检索引擎

安装solr
 
1.选择solr(http://apache.etoak.com//lucene/solr/ )合适的版本 
   并解压缩到相应目录(例如D:\java\solr)。
2.进入solr/example目录,这个目录下自带有jetty, 所以可以直接控制台启动。
cd  D:\java\solr
java -jar startup.jar
3.访问http://localhost:8983/solr/admin/,如果网页正常显示,说明solr可以正常运行。
 
配置solrconfig.xml(D:\java\solr\example\solr\conf\目录下)
 
<requestHandler name="/dataimport"
class="org.apache.solr.handler.dataimport.DataImportHandler">
      <lst name="defaults">
           <str name="config">data-config.xml</str>
      </lst>
</requestHandler>

 
配置data-config.xml
 
1. D:\java\solr\example\solr\conf\目录下创建一个xml文件,并命名为data-config.xml。
2. 在该文件中,进行数据的相关配置
例如:
<dataConfig>


<dataSource type=”JdbcDataSource”
driver="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:microsoft:sqlserver://localhost:1433;databaseName=ExtDB"
user="sa"
password=”” />


      <document name="doc">         
 <entity name="book" query="select * from t_book">
                   <field column="bookId" name="id" />
                   <field column="bookName" name="name" />
                   <field column="bookAuthor" name="author" />
                   <field column="bookAmount" name="amount" />
                   <field column="bookPrice" name="price" />
                   <field column="bookMemo" name="memo" />
             </entity>
</document>


</dataConfig>

 
配置schema.xml(D:\java\solr\example\solr\conf\目录下)


<fields>
    <field name="id" type="string" indexed="true" stored="true" required="true" />
    <field name="name" type="text" indexed="true" stored="true" />
    <field name="author" type="textgen" indexed="true" stored="true" />
    <field name="amount" type="int" indexed="false" stored="true" />
    <field name="price" type="float" indexed="false" stored="true" />
    <field name="memo" type="text" indexed="false" stored="true" />
    <field name="text" type="text" indexed="true" stored="false" multiValued="true"/>
    <dynamicField name="*" type="ignored" multiValued="true" />  
 </fields>
 
<uniqueKey>id</uniqueKey>
 
<defaultSearchField>text</defaultSearchField>
 
<solrQueryParser defaultOperator="OR"/>
 
<copyField source="name" dest="text"/>
<copyField source="author" dest="text"/>
 

 
执行全量或增量索引
 
如果都按照上述步骤配置完毕,并能正确启动,你可以通过浏览器执行如下命令来开始全量索引 http://localhost:8983/solr/dataimport?command=full-import
你可以通过这个地址 http:// localhost:8983/solr/dataimport 检查执行的状态
 
如果一切正常,你就可以通过 http:// localhost:8983/solr/admin/ 进行查询,你会得到一个xml格式的返回。
 
如果要使用增量索引,使用这个命令
http:// localhost:8983/solr/dataimport?command=delta-import


本文参考自:http://blog.csdn.net/love_tu/archive/2010/09/03/5861942.aspx
官方详细说明:http://wiki.apache.org/solr/DataImportHandler
 
对原文作者表所感谢!

页: [1]
查看完整版本: 使用solr为数据库构建全文检索引擎