作者:沈丶小浪_171 | 来源:互联网 | 2023-10-09 23:34
添加商品数据Spring容器由Spring容器,来管理SolrServer将SolrServer注入Spring容器添加配置文件applicationConte
添加商品数据
Spring容器
由Spring容器,来管理SolrServer
将SolrServer注入Spring容器
添加配置文件
applicationContext-solr.xml
<beans xmlns&#61;"http://www.springframework.org/schema/beans"xmlns:context&#61;"http://www.springframework.org/schema/context" xmlns:p&#61;"http://www.springframework.org/schema/p"xmlns:aop&#61;"http://www.springframework.org/schema/aop" xmlns:tx&#61;"http://www.springframework.org/schema/tx"xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation&#61;"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&#61;"httpSolrServer" class&#61;"org.apache.solr.client.solrj.impl.HttpSolrServer"><constructor-arg name&#61;"baseURL" value&#61;"http://192.168.25.154:8080/solr/collection1"/>bean>beans>
ServerImpl
业务逻辑
1、查询所有商品数据
2、创建一个SolrServer对象
3、为每个商品创建一个SolrInputDocument对象
4、为文档添加域
5、向索引库中添加文档
6、返回TaotaoResult
&#64;Service
public class SearchItemServiceImpl implements SearchItemService {&#64;Autowiredprivate SearchItemMapper searchItemMapper;&#64;Autowiredprivate SolrServer solrServer;&#64;Overridepublic TaotaoResult importItemsToIndex() {try {List itemList &#61; searchItemMapper.getItemList();for (SearchItem searchItem : itemList) {SolrInputDocument document &#61; 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();}}
如果&#xff0c;采用Dubbo分布式服务
需要发布服务
<dubbo:service interface&#61;"com.taotao.search.service.SearchItemService" ref&#61;"searchItemServiceImpl" timeout&#61;"300000"/>
调用的模块&#xff0c;引用服务
<dubbo:reference interface&#61;"com.taotao.search.service.SearchItemService" id&#61;"searchItemService" />