在上一篇博客中小编向大家简单介绍了一下如何搭建单机版的Solr服务,这样我们的搭建完成了Solr服务,但是如何使用呢?Solr服务中有我们要用的索引库,所以首先要做的就是向索引库中导入数据,那在java中又要如何操作呢?小编在这篇博客中,向大家介绍一下如何使用Solrj向索引库中导入数据。
solrJ是Java连接solr进行查询检索和索引更新维护的jar包。
我们可以通过solrj,对solr进行操作。就是这么简单。从数据库中根据sql语句查询数据,遍历数据创建文档对象,把文档对象写入索引库。
<dependency>
<groupId>org.apache.solrgroupId>
<artifactId>solr-solrjartifactId>
dependency>
因为需要自己写sql语句,就不能用Mybatis的逆向工程生成的了,所以需要自己写一个Mapper。
mapper文件:
<mapper namespace="com.taotao.search.mapper.ItemMapper" >
<select id="getItemList" resultType="com.taotao.search.pojo.SearchItem">
SELECT
a.id,
a.title,
a.sell_point,
a.price,
a.image,
b.`name` category_name,
c.item_desc
FROM
tb_item a
LEFT JOIN tb_item_cat b ON a.cid = b.id
LEFT JOIN tb_item_desc c ON a.id = c.item_id
WHERE
a.`status` = 1
select>
mapper>
Mapper接口:
package com.taotao.search.mapper;
import java.util.List;
import com.taotao.search.pojo.SearchItem;
public interface ItemMapper {
List getItemList();
}
取出需要的商品信息,创建文档对象,把对象写入索引库。要操作索引库需要SolrService对象,可以把SolrService放到spring容器中,注入到Service。
下面给出了单机版和集群版的注入方式:
<bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
<constructor-arg name="baseURL" value="http://192.168.137.13:8080/solr"/>
bean>
使用solrj.SolrServer导入数据:
package com.taotao.search.service.impl;
import java.io.IOException;
import java.util.List;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
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.search.mapper.ItemMapper;
import com.taotao.search.pojo.SearchItem;
import com.taotao.search.service.ItemService;
/**
* 商品导入service
* @author Ares
*
*/
@Service
public class ItemServiceImpl implements ItemService {
@Autowired
private SolrServer solrServer;
@Autowired
private ItemMapper itemMapper;
@Override
public TaotaoResult importItems() throws Exception{
//查询数据库获得商品列表
List itemList = itemMapper.getItemList();
for (SearchItem 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();
return TaotaoResult.ok();
}
}
导入:
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.common.util.ExceptionUtil;
import com.taotao.search.service.ItemService;
/**
* 导入商品数据Controller
* @author Ares
*
*/
@Controller
public class ItemController {
@Autowired
private ItemService itemService;
@RequestMapping("/importall")
@ResponseBody
public TaotaoResult importAll(){
try {
return itemService.importItems();
} catch (Exception e) {
e.printStackTrace();
return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
}
}
}
这样我们的服务就写好了,项目跑起来,直接运行服务,http://localhost:8080/search/importall ,运行的时候可能会遇到如下错误:
解决方案:
访问solr,进行全文查询,就可以查看到我们导入的数据:
通过对Solr导入就可以很深入的数据是如何处理的了,当我们把数据导入后,就可以进行下一步的查询工作了。所以在下一篇博客中,小编向大家介绍如何进行查询工作。敬请期待。