当前位置:   article > 正文

将数据导入索引库(solr)

导入索引库
  1. <!-- solr客户端 -->
  2. <dependency>
  3. <groupId>org.apache.solr</groupId>
  4. <artifactId>solr-solrj</artifactId>
  5. </dependency>

 service:

  1. @Service
  2. public class SearchItemServiceImpl implements SearchItemService {
  3. @Autowired
  4. private ItemMapper itemMapper;
  5. @Autowired
  6. private SolrServer solrServer;
  7. @Override
  8. public String importItmes() {
  9. try {
  10. //查询商品列表
  11. List<SearchItem> itemList = itemMapper.getItemList();
  12. //导入索引库
  13. for (SearchItem searchItem : itemList) {
  14. //创建文档对象
  15. SolrInputDocument document = new SolrInputDocument();
  16. //向文档中添加域
  17. document.addField("id", searchItem.getId());
  18. document.addField("item_title", searchItem.getTitle());
  19. document.addField("item_sell_point", searchItem.getSell_point());
  20. document.addField("item_price", searchItem.getPrice());
  21. document.addField("item_image", searchItem.getImage());
  22. document.addField("item_category_name", searchItem.getCategory_name());
  23. //写入索引库
  24. solrServer.add(document);
  25. }
  26. //提交
  27. solrServer.commit();
  28. //返回成功
  29. return "success";
  30. } catch (Exception e) {
  31. e.printStackTrace();
  32. return "导入失败";
  33. }
  34. }
  35. }

 SolrServer的配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
  8. 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
  9. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
  10. <bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
  11. <constructor-arg index="0" value="http://192.168.25.130:8080/solr/collection1"/>
  12. </bean>
  13. </beans>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/789021
推荐阅读
  

闽ICP备14008679号