配置好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;
    }
}
原文地址:https://www.cnblogs.com/libin6505/p/9791032.html