上一節咱們已經經過兩種方式運行了solr,本節演示怎麼配置solr的mysql數據源java
附上:mysql
喵了個咪的博客:http://w-blog.cnweb
Solr官網:http://lucene.apache.org/solr/sql
> PS:8.0.0版本已經發布,本文使用此時較爲穩定的7.7.1版本數據庫
數據表結構apache
CREATE TABLE `app` ( `id` int(11) NOT NULL AUTO_INCREMENT, `app_name` varchar(255) NOT NULL DEFAULT '', `score` decimal(10,5) NOT NULL DEFAULT '0.00000', `downLoadNum` int(10) NOT NULL DEFAULT '0', `top` int(10) NOT NULL DEFAULT '0', `type` int(10) NOT NULL DEFAULT '1', `update_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;
生成一些測試數據json
由於咱們須要使用mysql做爲數據源,咱們須要增長對mysql使用的jar包vim
> cd server/solr-webapp/webapp/WEB-INF/lib/ > wget http://pic.w-blog.cn/mysql-connector-java.jar
> PS:這裏基礎solr命令啓動的程序並未基於tomcat進行配置,後續cloud集羣會使用tomcat進配置tomcat
嘗試增長一個core會提示找不到配置,複製一份默認的配置文件app
> cp -r server/solr/configsets/_default server/solr/new_core
在solrconfig.xml 下添加如下配置,添加位置大約在 680行,SearchHandler 配置上面:
> vim server/solr/new_core/conf/solrconfig.xml <!-- Request Handlers http://wiki.apache.org/solr/SolrRequestHandler Incoming queries will be dispatched to a specific handler by name based on the path specified in the request. If a Request Handler is declared with startup="lazy", then it will not be initialized until the first request that uses it. --> <!-- add property --> <requesthandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"> <lst name="defaults"> <str name="config">data-config.xml</str> </lst> </requesthandler> <!-- SearchHandler http://wiki.apache.org/solr/SearchHandler For processing Search Queries, the primary Request Handler provided with Solr is "SearchHandler" It delegates to a sequent of SearchComponents (see below) and supports distributed queries across multiple shards -->
該文件的配置以下,鏈接的是mysql也支持其餘的數據庫
> vim server/solr/new_core/conf/data-config.xml <!--?xml version="1.0" encoding="UTF-8"?--> <dataconfig> <datasource name="source" type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/appstore" user="root" password="123456" /> <document> <entity name="app" pk="id" datasource="source" query="select * from app" deltaimportquery="select * from app where id = '${dih.delta.id}'" deltaquery="select id from app where update_date > '${dataimporter.last_index_time}' and type = 1"> <field column="id" name="id" /> <field column="update_date" name="update_date" /> </entity> </document> </dataconfig>
在這以後,須要配置managed-schema文件,與數據庫進行映射,在117行附近,添加與數據庫的映射,具體添加規則,不詳細寫了。
> vim server/solr/new_core/conf/managed-schema <!-- add propertity --> <field name="appName" type="string" indexed="true" stored="true" /> <field name="score" type="string" indexed="true" stored="true" /> <field name="downLoadNum" type="string" indexed="true" stored="true" /> <field name="top" type="string" indexed="true" stored="true" /> <field name="type" type="string" indexed="true" stored="true" /> <field name="update_date" type="string" indexed="true" stored="true" />
重啓solr
> solr restart -force
再次增長core發現已經能夠增長成功
初始化數據
初始化完成就能夠進行查詢了
若是修改了能夠觸發更新操做
固然也能夠經過請求URL的方式進行數據更新,這裏也方便索引的更新和程序相結合
http://172.16.3.148:8983/solr/new_core/dataimport?command=delta-import&clean=%20false&commit=true&wt=json&indent=true&verbose=false&optimize=false&debug=false