要使用solr實現網站中商品搜索,需要將mysql數據庫中數據在solr中創建索引.
1.需要在solr的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"/>
然后將數據庫中的數據導入到solr中:
需要將
這個三個jar包導入到lib下.這三個jar包分別dataimportHandler插件.和mysql數據庫的數據庫驅動
將數據庫中
改成%,才能.
配置solrconfig.xml文件,添加一個requestHhander.
<requestHandler name="/dataimport"
class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requestHandler>
第三步:創建一個data-config.xml,保存到coolection1\conf\目錄下
<?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="root"/> <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>
這里需要修改ip地址和數據庫名字
我這里使用ip為192.168.200.1,因為我是在虛擬機中安裝的這個軟件.
最后就是需要重啟tomcat,還有就是mysql的服務,也需要重新啟動.