在solr4.10已经安装完成的情况下,多核的模式下;
1、需要准备的jar包;数据库驱动jar包 mysql-connector-java-5.1.17-bin.jar,solr-dataimporthandler-4.10.0.jar;
solrconfig.xml 中引入
<lib dir="F:/SolrHome/multicore/lib" regex=".*\.jar" />
2、在配置文件F:\SolrHome\multicore\core1\conf\solrconfig.xml 中添加请求处理类,同时加载数据源的配置;
<requestHandler name = "/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"> <lst name ="defaults"> <str name = "config">data-config.xml </str> </lst> </requestHandler>
3、配置数据源,新建文件 F:\SolrHome\multicore\core1\conf\data-config.xml
配置如下
<dataConfig> <dataSource password="root" user="root" url="jdbc:mysql://127.0.0.1:3306/jiaju" driver="com.mysql.jdbc.Driver" type="JdbcDataSource"/> <document> <entity query="select aid,title,author from article" name="article"> </entity> </document> </dataConfig>
4、配置各字段和中文分词器;在F:\SolrHome\multicore\core1\conf\schema.xml
<schema version="1.1" name="example core one"> <fieldtype name="string" omitNorms="true" sortMissingLast="true" class="solr.StrField"/> <fieldType name="int" omitNorms="true" class="solr.TrieIntField" positionIncrementGap="0" precisionStep="0"/> <fieldType name="long" class="solr.TrieLongField" positionIncrementGap="0" precisionStep="0"/> <fieldType name="text" class="solr.TextField"> <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/> </fieldType> <!-- general --> <field name="aid" required="true" stored="true" indexed="true" type="int"/> <field name="title" stored="true" indexed="true" type="text"/> <field name="author" stored="true" indexed="true" type="string"/> <field name="_version_" stored="true" indexed="true" type="long"/> <!-- field to use to determine and enforce document uniqueness. --> <uniqueKey>aid</uniqueKey> <!-- field for the QueryParser to use when an explicit fieldname is absent --> <defaultSearchField>title</defaultSearchField> <!-- SolrQueryParser configuration: defaultOperator="AND|OR" --> <solrQueryParser defaultOperator="OR"/> </schema>
5、启动tomcat ,http://localhost:8983/solr/#/core1/dataimport//dataimport ,选择entity article ,然后execute;结果如截图;说明数据库的数据添加索引成功;