Solr(批量导入数据)
准备工作:数据库建表
步骤一:导包
将solr-4.10.3 -> dist中的 solr-dataimporthandler-4.10.3.jar和solr-dataimporthandler-extras-4.10.3.jar导入到索引库的lib目录中,如果没有lib目录自行创建lib目录。
注意:还需要连接mysql数据库的jar包
步骤二、在colletion1的核心配置文件中,添加请求处理器
配置:
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"> <lst name="defaults"> <str name="config">data-config.xml</str> </lst> </requestHandler>
步骤三、在conf目中新建data-config.xml文件,该文件用来连接数据库,用于导入时完成数据库字段和域的映射,并在其中写入如下配置:
<?xml version="1.0" encoding="UTF-8" ?> <dataConfig> <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/lucene" user="root" password="123456"/> <document> <entity name="product" query="SELECT pid,name,catalog_name,price,description,picture FROM products "> <field column="pid" name="id"/> <field column="name" name="product_name"/> <field column="catalog_name" name="product_catalog_name"/> <field column="price" name="product_price"/> <field column="description" name="product_description"/> <field column="picture" name="product_picture"/> </entity> </document> </dataConfig>
注意:data-config.xml中的field标签中的name值,在schema.xml中一定要存在。否则会出错。
扩充:在schema.xml中添加field配置
<!--product--> <field name="product_name" type="text_ik" indexed="true" stored="true"/> <field name="product_price" type="float" indexed="true" stored="true"/> <field name="product_description" type="text_ik" indexed="true" stored="false" /> <field name="product_picture" type="string" indexed="false" stored="true" /> <field name="product_catalog_name" type="string" indexed="true" stored="true" /> <field name="product_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/> <copyField source="product_name" dest="product_keywords"/> <copyField source="product_description" dest="product_keywords"/>