上一节咱们已经经过两种方式运行了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