編輯solrcofnig.xml添加處理器mysql
<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的文件並保存以下內容到conf目錄(也就是solrconfig.xml的目錄)web
<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的詳細信息。sql
將JDBC的jar驅動文件放到
修改data-config.xml,以下所示tomcat
<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。app
運行 http://solr-host:port/dataimpor?command=full-import 創建索引webapp
修改data-config以下:url
<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的字段code
運行full-import
下載mysql的JDBC的jar,並拷貝到
修改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>
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 狀態查詢