zoukankan      html  css  js  c++  java
  • 配置好solr搜索引擎服务器后java后台如何将商品信息导入索引库

    首先,在配置文件目录中添加solr 服务器的bean 配置文件

    solr服务器的url可以写在配置文件中:

    url地址其实就是我们网页可以访问的solr地址:

    然后我们写 service

    package com.taotao.search.service.impl;
    
    import java.util.List;
    
    import org.apache.solr.client.solrj.SolrServer;
    import org.apache.solr.common.SolrInputDocument;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.taotao.common.pojo.TaotaoResult;
    import com.taotao.common.utils.ExceptionUtil;
    import com.taotao.search.mapper.ItemMapper;
    import com.taotao.search.pojo.Item;
    import com.taotao.search.service.ItemService;
    
    @Service
    public class ItemServiceImpl implements ItemService{
        @Autowired
        private ItemMapper itemMapper;
        @Autowired
        private SolrServer solrServer;
        @Override
        public TaotaoResult importAllItems() {
            try {
                //查询数据库
                List<Item> itemList = itemMapper.getItemList();
                //拼接Document,添加到solr服务器索引库
                for (Item item : itemList) {
                    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());
                    document.addField("item_desc", item.getItem_desc());
                    solrServer.add(document);
                }
                //提交(注意:一定要提交,否则查询不出来的)
                solrServer.commit();
            } catch (Exception e) {
                e.printStackTrace();
                return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
            }
            return TaotaoResult.ok();
        }
    
    }

    注意:

    代码中,往Document里面添加的字段名:

    需要和solr服务器中的 schema.xml 配置文件中配置的业务字段一致:

    简单说明:在 solr服务器的 schema.xml配置文件中配置好分词器后,就可以配置 业务域字段了,其中商品的id就作为solr索引库中Document的id,所以上图配置业务字段时不用配置id(solr中Document默认就有id字段),然后配置其他业务字段,如 title,sellpoint,price等,其中由于描述 desc只是搜索时用到,通过solr检索出结果后在前台展示时并不需要显示 描述信息 desc,所以上面  item_desc后面的 stored属性值设置为 “false” 。

    另外,最后还设置了一个  item_keywords 域,在它里面是把我们前面配置的 各个业务域字段复制进去了,这是个复制域,它是solr内部为了提高效率搜索引擎优化的一种方式,我们只需要按照上图的套路配置即可。

    Controller层:

    package com.taotao.search.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import com.taotao.common.pojo.TaotaoResult;
    import com.taotao.search.service.ItemService;
    
    @Controller
    @RequestMapping("/manager")
    public class ItemController {
        
        @Autowired
        private ItemService itemService;
        
        @RequestMapping("/importall")
        @ResponseBody
        public TaotaoResult importAllItems() {
            TaotaoResult result = itemService.importAllItems();
            return result;
        }
    }
  • 相关阅读:
    uboot学习之三-----uboot启动第一阶段--start.S之一
    4.7 C语言的存储类,作用域,生命周期,链接属性
    uboot学习之二----主Makefile学习之一----版本号 u_boot_version(U_BOOT_VERSION = $(VERSION).$(PATCHLEVEL).$(SUBLEVEL).$(EXTRAVERSION)) (24-29行)
    uboot学习之二----主Makefile学习之二----环境变量之:主机的操作系统和主机架构(HOSTOS、HOSTARCH)(31-43行)
    uboot学习之二----主Makefile学习之三----静默编译
    uboot学习之二----主Makefile学习之四----两种编译方法:原地编译和单独输出文件夹编译
    Linux下安装交叉工具链&&安装vim
    善变的不只是女人,还有volatile ---偷来的标题名
    东芝半导体最新ARM开发板——TT_M3HQ开箱评测
    STM32F407外部晶体改为25M后检测不到芯片的解决办法
  • 原文地址:https://www.cnblogs.com/libin6505/p/9791032.html
Copyright © 2011-2022 走看看