欢迎投稿

今日深度:

【solr】——数据导入,

【solr】——数据导入,


开篇

             在《solr配置中文分析器》博客中说到document时solr进行搜索的数据源,这个数据源是我们上传到solr中,上传方法才博客中也介绍到有多种,本篇博客介绍在java中使用solrj编程将数据库中的字段上传到solr中作为数据源。


正文

             项目使用技术:spring+springMVC+mysql+mybites

              整体思路:从数据库中根据sql语句查询数据,遍历数据创建文档对象,把文档对象写入索引库。

             首先在项目中添加solrj的jar包,然后需要根据自己系统的业务需求进行分析。本实例中的业务如下:

             电商项目,根据商品的商品名、卖点描述、所属类型名、价格等进行搜索,最终返回结果列表在页面显示。

             数据库表: tb_item 商品信息表,tb_item_cat商品分类表,tb_item_desc商品卖点描述。

 

第一步:修改配置文件,重启tomcat

             在schema.xml中添加如下配置,在博客《solr配置文件简单学习》中解释了这俩个配置的含义,在结合数据库使用时每个filed配置对应于数据库表中的某一列,配置如下:

<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"/>

             注意:其中type="text_ik",text_ik是我们配置的中文分析器的name。


第二步:编写java程序,向solr中导入数据

        一、建立SearchItem类,包含要使用到的字段。

public class SearchItem {
         private String id;
         private String title;//名称
         private String sell_point;//卖点
         private Long price;//价格
         private String image;//图片
         private String category_name;//类别名称
         private String item_desc;//商品描述
}


        二、建立mapper,添加查询所需数据的方法

            接口:

package com.tt.search.mapper;
public interface ItemMapper {
         List<SearchItem> getItemList();
}

             Xml:

<mapper namespace="com.tt.search.mapper.ItemMapper" >
         <select id="getItemList" resultType="com.tt.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>


        三、在spring的配置文件中添加如下配置

<!--单机版的sole客户端  -->
<bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
         <constructor-arg name="baseURL" value="http://192.168.169.30:8080/solr"/>
</bean>


        四、编写导入数据的service

@Service
public class ItemServiceImpl implements ItemService {
 
         @Autowired
         private SolrServer solrServer;
         @Autowired
         private ItemMapper itemMapper;
        
         @Override
         public TaotaoResult importItems() throws Exception {
                   //查询数据库获得商品列表
                   List<SearchItem> 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();
         }
 
}
             注意:在配置文件中配置的是HttpSolrServer,它是SolrServer的子类,配置好后使用SolrServer时它就会自动的注入到SolrServer对象属性中里。


        
五、编写Controller层             

@Controller
public class ItemController {
        
         @Autowired
         private ItemService itemService;
        
         @RequestMapping("/importall")
         @ResponseBody
         public TaotaoResult importAll() {
                   try {
                            TaotaoResult result = itemService.importItems();
                            return result;
                   } catch (Exception e) {
                            e.printStackTrace();
                            return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
                   }
         }
 
}
        

        六、启动程序调用Controller向solr中导入数据。

              未导入之前:

              成功导入后:

上图中数据左侧是solr查询时会使用的一些参数,下面是一些见解,方便理解操作:

        qt: query type,指定查询使用的QueryHandler,默认是standard。

 

        q: query,查询字符串(必须)。如果查询所有*:* ,根据指定字段查询(item_title:苹果手机 AND item_sell_point:北京)

 

        fq: filter query,作用:在q查询符合结果中同时是fq查询符合的,例如:q= item_title:苹果手机&fq=date_time:[20081001TO 20091031],找关键字title包含苹果手机,并且date_time是20081001到20091031之间的。

 

        Sort:排序,格式:sort=<field name>+<desc|asc>[,<fieldname>+<desc|asc>] 。示例:(score desc, price asc)表示先“score”降序, 再“price”升序,默认是相关性降序。

 

        Start:用于分页,定义结果起始记录数。默认值为 0。start=15,从第 15 个返回结果。

 

        rows:用于分页,定义结果每页返回记录数,默认为10

 

        fl: 是逗号分隔的列表,用来指定文档结果中应返回的 Field 集。默认为“*”,指所有的字段。

 

        df:默认的查询字段,一般默认指定。

        

        wt: writer type,指定输出格式,可以有xml, json, php, phps。

        

小结


        本篇博客在java中使用solrj将数据库中的数据导入到了solr中当做以后搜索的源数据,简单的对solrj进行了应用,有了数据就可以进行搜索了,下篇博客通过实例看一下在程序中如何使用solrj完成搜索。

www.htsjk.Com true http://www.htsjk.com/solr/37910.html NewsArticle 【solr】——数据导入, 开篇               在《solr配置中文分析器》博客中说到document时solr进行搜索的数据源,这个数据源是我们上传到solr中,上传方法才博客中也介绍到有多种,...
相关文章
    暂无相关文章
评论暂时关闭