生活随笔
收集整理的這篇文章主要介紹了
SolrJ添加商品数据
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
添加商品數據
Spring容器
由Spring容器,來管理SolrServer
將SolrServer注入Spring容器
添加配置文件
applicationContext-solr.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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsdhttp://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.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"><bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer"><constructor-arg name="baseURL" value="http://192.168.25.154:8080/solr/collection1"/></bean></beans>
ServerImpl
業務邏輯
1、查詢所有商品數據
2、創建一個SolrServer對象
3、為每個商品創建一個SolrInputDocument對象
4、為文檔添加域
5、向索引庫中添加文檔
6、返回TaotaoResult
@Service
public class SearchItemServiceImpl implements SearchItemService {@Autowiredprivate SearchItemMapper searchItemMapper;
@Autowiredprivate SolrServer solrServer;
@Overridepublic TaotaoResult
importItemsToIndex() {
try {List<SearchItem> itemList = searchItemMapper.getItemList();
for (SearchItem searchItem : itemList) {SolrInputDocument document =
new SolrInputDocument();document.addField(
"id", searchItem.getId());document.addField(
"item_title", searchItem.getTitle());document.addField(
"item_sell_point", searchItem.getSell_point());document.addField(
"item_price", searchItem.getPrice());document.addField(
"item_image", searchItem.getImage());document.addField(
"item_category_name", searchItem.getCategory_name());document.addField(
"item_desc", searchItem.getItem_desc());solrServer.add(document);}solrServer.commit();}
catch (Exception e) {e.printStackTrace();
return TaotaoResult.build(
500,
"數據導入失敗");}
return TaotaoResult.ok();}}
如果,采用Dubbo分布式服務
需要發布服務
<dubbo:service interface="com.taotao.search.service.SearchItemService" ref="searchItemServiceImpl" timeout="300000"/>
調用的模塊,引用服務
<dubbo:reference interface="com.taotao.search.service.SearchItemService" id="searchItemService" />
總結
以上是生活随笔為你收集整理的SolrJ添加商品数据的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。