數(shù)據(jù)庫中的數(shù)據(jù)導(dǎo)入索引庫
導(dǎo)入solr索引庫需要先在Linux中安裝solr并且配置solr
這是在Linux中配置solr的詳細(xì)步驟地址
https://mp.csdn.net/postedit/80472436
模塊劃分
SearchItemServiceImpl.java代碼實(shí)現(xiàn)
/**
* 商品數(shù)據(jù)索引庫Service
*/
@Service
public class SearchItemServiceImpl implements SearchItemService {
@Autowired
private ItemMapper itemMapper;
@Autowired
private SolrServer solrServer;
/**
* 將刪商品數(shù)據(jù)導(dǎo)入索引庫
* @return
*/
@Override
public E3Result importItems() {
try {
//查詢商品列表
List《SearchItem》 itemList = itemMapper.getItemList();
//導(dǎo)入到索引庫
for (SearchItem item :itemList) {
//創(chuàng)建文檔對(duì)象
SolrInputDocument document=new SolrInputDocument();
//向文檔添加域
document.addField(“id”,item.getId());
document.addField(“item_title”,item.getTitle());
document.addField(“item_sell_point”,item.getSell_point());
document.addField(“item_price”,item.getPrice());
document.addField(“item_image”,item.getImage());
document.addField(“item_category_name”,item.getCategory_name());
//寫入索引庫
solrServer.add(document);
}
//提交
solrServer.commit();
//返回成功
return E3Result.ok();
}catch (Exception e){
e.printStackTrace();
return E3Result.build(500,“商品導(dǎo)入失??!”);
}
}
}
Controller代碼
然而我們?cè)谔砑由唐返臅r(shí)候solr庫沒有添加到該商品的索引,導(dǎo)致我們?cè)谒阉鲿r(shí)搜索不到該商品的信息,想要同步只有每次添加一個(gè)商品,我們就只有去調(diào)用點(diǎn)擊后臺(tái)的一鍵導(dǎo)入solr庫才可以將該商品添加到solr庫中。這樣就感覺很麻煩而且效率非常的低,可用性較差。我們就可以使用activemq消息的方式來解決該方法實(shí)現(xiàn)自動(dòng)同步的效果。每次添加一個(gè)商品發(fā)送一個(gè)消息在添加該商品的信息到solr庫中。
實(shí)現(xiàn)功能的代碼。
需要修改其中商品管理模塊的步驟
添加一個(gè)applicationConten-activemq.xml配置文件
2.修改添加實(shí)現(xiàn)類中的方法在插入商品信息到數(shù)據(jù)庫中的時(shí)候我們就發(fā)送一個(gè)消息
//發(fā)送一個(gè)商品添加信息
jmsTemplate.send(topicDestination, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
//設(shè)置消息內(nèi)容為商品id值需要轉(zhuǎn)換一下類型為字符串
TextMessage textMessage = session.createTextMessage(itemId+“”);
return textMessage;
}
});
搜索模塊
添加一個(gè)監(jiān)聽器監(jiān)聽隨時(shí)監(jiān)聽隨時(shí)接收消息
ItemAddMessageListener.java
添加applicationConten-activemq.xml配置文件
《?xml version=“1.0” encoding=“UTF-8”?》
《beans xmlns=“http://www.springframework.org/schema/beans”
xmlns:context=“http://www.springframework.org/schema/context” xmlns:p=“http://www.springframework.org/schema/p”
xmlns:aop=“http://www.springframework.org/schema/aop” xmlns:tx=“http://www.springframework.org/schema/tx”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=“http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd”》
《!-- 真正可以產(chǎn)生Connection的ConnectionFactory,由對(duì)應(yīng)的 JMS服務(wù)廠商提供 --》
《bean id=“targetConnectionFactory” class=“org.apache.activemq.ActiveMQConnectionFactory”》
《property name=“brokerURL” value=“tcp://192.168.25.128:61616” /》
《/bean》
《!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --》
《bean id=“connectionFactory”
class=“org.springframework.jms.connection.SingleConnectionFactory”》
《!-- 目標(biāo)ConnectionFactory對(duì)應(yīng)真實(shí)的可以產(chǎn)生JMS Connection的ConnectionFactory --》
《property name=“targetConnectionFactory” ref=“targetConnectionFactory” /》
《/bean》
《!--這個(gè)是隊(duì)列目的地,點(diǎn)對(duì)點(diǎn)的 --》
《bean id=“queueDestination” class=“org.apache.activemq.command.ActiveMQQueue”》
《constructor-arg》
《value》spring-queue《/value》
《/constructor-arg》
《/bean》
《!--這個(gè)是主題目的地,一對(duì)多的 --》
《bean id=“topicDestination” class=“org.apache.activemq.command.ActiveMQTopic”》
《constructor-arg value=“itemAddTopic” /》
《/bean》
《!-- 接收消息 --》
《!-- 配置監(jiān)聽器 --》
《bean id=“myMessageListener” class=“com.e3mall.search.activemq.listener.MyMessageListener” /》
《!-- 消息監(jiān)聽容器 --》
《bean class=“org.springframework.jms.listener.DefaultMessageListenerContainer”》
《property name=“connectionFactory” ref=“connectionFactory” /》
《property name=“destination” ref=“queueDestination” /》
《property name=“messageListener” ref=“myMessageListener” /》
《/bean》
《!-- 配置監(jiān)聽器 --》
《bean id=“itemAddMessageListener” class=“com.e3mall.search.activemq.listener.ItemAddMessageListener” /》
《!-- 消息監(jiān)聽容器監(jiān)聽商品添加消息同步索引庫 --》
《bean class=“org.springframework.jms.listener.DefaultMessageListenerContainer”》
《property name=“connectionFactory” ref=“connectionFactory” /》
《!--設(shè)置我需要的發(fā)送消息方式--》
《property name=“destination” ref=“topicDestination” /》
《property name=“messageListener” ref=“itemAddMessageListener” /》
《/bean》
《/beans》
applicationContent-service.xml發(fā)布服務(wù)
將數(shù)據(jù)庫中的數(shù)據(jù)導(dǎo)入Solr索引庫
在大部分應(yīng)用中,主要還是使用的是數(shù)據(jù)庫中的數(shù)據(jù),因此,這一步還是非常重要的。
現(xiàn)在目錄結(jié)構(gòu)如圖所示:
在solr后臺(tái)管理界面中
dataimport 負(fù)責(zé)將數(shù)據(jù)庫中數(shù)據(jù)導(dǎo)入到索引庫中,不過在導(dǎo)入之前,還需要一些相關(guān)配置。
1、需要的jar包
還需要mysql的驅(qū)動(dòng)包
將這3個(gè)jar包 放入 E:\solr\solrhome\collection1\lib 下
2.在solrconfig.xml中最后面添加一個(gè)requesthandler節(jié)點(diǎn)
《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 是指關(guān)于要導(dǎo)入的數(shù)據(jù)庫的配置信息。
2、在E:\solr\solrhome\collection1\conf 下創(chuàng)建
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=“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》
數(shù)據(jù)庫和賬戶密碼根據(jù)自己的實(shí)際情況來寫
可以看出,可以在
query=“SELECT pid,name,catalog_name,price,description,picture FROM products ”
指定自己索引庫中要存的索引,
《field column=“pid” name=“id”/》
《field column=“name” name=“product_name”/》
可以配置索引中的域名 和 數(shù)據(jù)庫字段的映射關(guān)系 ,其中column為字段名,name為域名。
3、在schema.xml中配置自定義域
首先要配置好ik分詞器,不會(huì)的可以參考下面這篇文章
solr的安裝與使用(二)
然后在后面增加自定義域
《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”/》
這里面name 和 data-config.xml中的 name一致。type使用ik分詞器定義的type
type=“text_ik”
4、重啟tomcat
選中collection1 點(diǎn)擊dataimport
5、點(diǎn)擊執(zhí)行,就可以將數(shù)據(jù)中數(shù)據(jù)導(dǎo)入索引庫了。
評(píng)論
查看更多