将数据库表导入到solr索引
將數據庫表導入到solr索引
編輯solrcofnig.xml添加處理器
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"><lst name="defaults"><str name="config">data-config.xml</str></lst></requestHandler>創(chuàng)建一個名為data-config.xml的文件并保存如下內容到conf目錄(也就是solrconfig.xml的目錄)
<dataConfig><dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver"url="jdbc:mysql://localhost/dbname" user="user-name" password="password"/><document><entity name="id" query="select id,name,desc from mytable"></entity></document></dataConfig>編輯schema.xml文件,保證文件中有'id','name','desc'等fields。并更改data-config.xml的詳細信息。
將JDBC的jar驅動文件放到/lib文件夾中(tomcat/webapps/solr/WEB-INF/lib)
運行命令 http://solr-host:port/solr/dataimport?command=full-import進行全量索引,每次進行全量索引時,會將數據清空,如果不想清空需要添加clean=false。例如http://solr-host:port/solr/dataimport?command=full-import&clean=false
在字段名和field明不同的時候添加索引
修改data-config.xml,如下所示
<dataConfig><dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver"url="jdbc:mysql://localhost/dbname" user="user-name" password="password"/><document><entity name="id" query="select id,name,desc from mytable"><field column="id" name="solr_id"/><field column="name" name="solr_name"/><field column="desc" name="solr_desc"/></entity></document></dataConfig>寫入solr的字段為'solr_id', 'solr_name', solr_desc'。所以schema.xml中必須要要這幾個field。
運行 http://solr-host:port/dataimpor?command=full-import 建立索引
配置多個表建立索引
修改data-config如下:
<dataConfig><dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver"url="jdbc:mysql://localhost/dbname" user="user-name" password="password"/><document><entity name="outer" query="select id,name,desc from mytable"><field column="id" name="solr_id"/><field column="name" name="solr_name"/><field column="desc" name="solr_desc"/><entity name="inner"query="select details from another_table where id ='${outer.id}'"><field column="details" name="solr_details"/> </entity></entity></document></dataConfig>schema.xml應該包含solr_details的字段
運行full-import
mysql配置
下載mysql的JDBC的jar,并拷貝到/lib的文件夾
修改data-config為如下
<dataConfig><dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://ip:3306/dbname" user="username" password="password"/> <document name="products"><entity ......</entity></document></dataConfig>DataImportHandler支持的命令
abort http://<host>:<port>/solr/dataimport?command=abort 終止命令 delta-import http://<host>:<port>/solr/dataimport?command=delta-import 增量 full-import http://<host>:<port>/solr/dataimport?command=full-import 全量 reload-config http://<host>:<port>/solr/dataimport?command=reload-config 重新加載配置 status http://<host>:<port>/solr/dataimport?command=status 狀態(tài)查詢總結
以上是生活随笔為你收集整理的将数据库表导入到solr索引的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ios页面间跳转方式总结
- 下一篇: 基于小波变换的数字图像处理(MATLAB