solr索引库添加

来源:互联网 发布:火影手游网络连接不上 编辑:程序博客网 时间:2024/06/05 14:40

1.solr的安装与配置
参见
http://blog.csdn.net/apologize_zhang/article/details/51177491
http://blog.csdn.net/Jason763/article/details/72877416

2.分析需要导入索引库的数据信息,将这些属性重新封装成一个POJO类
同时别忘了依赖SOLR架包

3.编写Mapper.java和Mapper.xml
由于是多表查询,原来逆向工程的Mapper无法使用,所以需要自己写
Mapper.java中写接口,
Mapper.xml中实现如下

<mapper namespace="com.taotao.search.mapper.SearchItemMapper" >    <select id="getItemList" resultType="com.taotao.common.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>

4.在schea.xml配置文件中配置IK分词方法在第一步中其实已经做了

 <!-- IKAnalyzer--><fieldType name="text_ik" class="solr.TextField">  <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/></fieldType><field name="item_title" type="text_ik" indexed="true" stored="true"/><field name="item_sell_point" type="text_ik" indexed="true" stored="true"/><field name="item_price"  type="long" indexed="true" stored="true"/><field name="item_image" type="string" indexed="false" stored="true" /><field name="item_category_name" type="string" indexed="true" stored="true" /><field name="item_desc" type="text_ik" indexed="true" stored="false" /><field name="item_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/><copyField source="item_title" dest="item_keywords"/><copyField source="item_sell_point" dest="item_keywords"/><copyField source="item_category_name" dest="item_keywords"/><copyField source="item_desc" dest="item_keywords"/>

5.编写单机版solr的配置文件

<!-- 单机版solr的连接 -->    <bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">        <constructor-arg name="baseURL" value="http://127.0.0.1:8090/solr/collection1"/>    </bean>

6.实现

《1》编写接口
TaotaoResult importItemsToIndex();
《2》实现接口

/** * 商品数据导入索引库 * <p>Title: SearchItemServiceImpl</p> * <p>Description: </p> * <p>Company: www.itcast.cn</p>  * @version 1.0 */@Servicepublic class SearchItemServiceImpl implements SearchItemService {    @Autowired    private SearchItemMapper searchItemMapper;    @Autowired    private SolrServer solrServer;    @Override    public TaotaoResult importItemsToIndex() {        try {            //1、先查询所有商品数据            List<SearchItem> itemList = searchItemMapper.getItemList();            //2、遍历商品数据添加到索引库            for (SearchItem searchItem : itemList) {                //创建文档对象                SolrInputDocument document = 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);            }            //3、提交            solrServer.commit();        } catch (Exception e) {            e.printStackTrace();            return TaotaoResult.build(500, "数据导入失败");        }        //4、返回添加成功        return TaotaoResult.ok();    }}

《3》控制层

@Controllerpublic class IndexManagerController {    @Autowired    private SearchItemService searchItemService;    @RequestMapping("/index/import")    @ResponseBody    public TaotaoResult importIndex() {        TaotaoResult taotaoResult = searchItemService.importItemsToIndex();        return taotaoResult;    }}
原创粉丝点击