上一次说了solr添加配置ik分词器及schema文件分析,如果要了解的童鞋,可以查看我的上篇solr(三)solr添加配置ik分词器及schema文件分析,接下啦继续,下面是如何将数据库的数据导入到solr索引库中,具体操作如下~
内容:
1. Solr链接数据库
一:DataImport步骤:
1. 导包
将solr-dataimporthandler-5.3.1.jar和solr-dataimporthandler-extras-5.3.1.jar从solr-5.3.1/dist/文件夹下copy到solr-5.3.1/server/solr-webapp/webapp/WEB-INF/lib当中
2. 修改managed-schema文件,将其修改为schema.xml文件
原因:https://blog.csdn.net/jiangchao858/article/details/52518190
3. 修改solrconfig.xml配置文件
增加一段添加数据库数据的xml,如下:
在solrconfig中添加 data-config时 *********写中文注释会报错编译
<!--insert data-config file-->
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">./data-config.xml</str>
</lst>
</requestHandler>
4. 编写data-config.xml文件
在solrhome下的core中/conf/下创建data-config.xml文件,配置访问数据库的用户名、密码、查询语句,column对应数据库中字段、name对应solr的schema.xml中字段(根据实际情况),文件内容如下:
<dataConfig>
<dataSource driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/库名" user="root" password="root"/>
<document name="salesDoc">
<entity name="shop_information" query="SELECT * from shop_information" >
<field name="ShIfid" column="SH_IF_ID" />
<field name="siSsId" column="SI_SS_ID" />
<field name="siImage" column="SI_IMAGE" />
<field name="siName" column="SI_NAME" />
<field name="siCityCode" column="SI_CITY_CODE" />
<field name="siExpressNum" column="SI_EXPRESS_NUM" />
<field name="siTime" column="SI_TIME" />
</entity>
</document>
</dataConfig>
5. 在schema.xml添加field(根据实际情况),如下
<field name="ShIfid"type="text_ik" indexed="true" stored="true"required="true" />
<fieldname="siSsId" type="text_ik" indexed="true"stored="true" required="true" />
<fieldname="siImage" type="text_ik" indexed="true" stored="true"required="true" />
<fieldname="siName" type="text_ik" indexed="true"stored="true" required="true" />
<fieldname="siCityCode" type="text_ik" indexed="true"stored="true" required="true" />
<fieldname="siExpressNum" type="text_ik" indexed="true"stored="true" required="true" />
<fieldname="siTime" type="text_ik" indexed="true"stored="true" required="true" />
会出现连接数据库时错误,表示找不到JDBC的驱动文件
weebapp下lib 下添加 mysql-connector-java-5.1.30.jar 包
solr import数据时报错 miss mandatoryuniquekey fieId id:
原因:在solr建立索引的时候,如果你提交的doc中没有 id 这个Field,结果Solr在建立索引时候出现如该错误:
主要是因为Solr 的managed-schema配置文件中定义了<uniqueKey>id</uniqueKey>,默认了ID 是唯一的。如果你的索引字段不需要ID,就可以把这个改掉.
过程中可能遇到的问题(整理)(没有遇到的话可忽略):
1、如下,说明在schema.xml重复定义了id,把它屏蔽掉,重启服务
collection: org.apache.solr.common.SolrException:org.apache.solr.common.SolrException: Could not load conf for core collection: [schema.xml] Duplicate field definition for ‘id’ [[[id{type=string,properties=indexed,stored,omitNorms,omitTermFreqAndPositions,sortMissingLast,required, required=true}]]] and [[[id{type=int,properties=indexed,stored,omitNorms,omitTermFreqAndPositions,required, required=true}]]]. Schema file is /Users/user/lcg/Web/solrServer/solr-5.2.1/server/solr/collection/conf/schema.xml
2、如下,说明没有在schema.xml文件中定义name字段
collection: org.apache.solr.common.SolrException:org.apache.solr.common.SolrException: Could not load conf for core collection: default search field ‘name’ not defined or not indexed. Schema file is /Users/user/lcg/Web/solrServer/solr-5.2.1/server/solr/collection/conf/schema.xml
3、<field name="name" type="string" indexed="true" stored="true">
后面少了个/,应该为<field name="name" type="string" indexed="true" stored="true"/>
,修改完之后,重启服务
collection: org.apache.solr.common.SolrException:org.apache.solr.common.SolrException: Could not load conf for core collection: org.xml.sax.SAXParseException; systemId: solrres:/schema.xml; lineNumber: 1028; columnNumber: 12; 元素类型 “field” 的结束标记必须以 ‘>’ 分隔符结束。 Schema file is /Users/user/lcg/Web/solrServer/solr-5.2.1/server/solr/collection/conf/schema.xml