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; }}
阅读全文
0 0
- solr索引库添加
- Solr添加文档到索引
- solr 对数据添加索引
- Solr 管理索引库
- solr索引库添加新的索引,使用json文件或者xml文件的数据
- Solr学习总结-命令行添加索引
- solr中添加索引时报错
- solr批量添加索引数据报错
- 跟我学solr---在solr admin中添加索引
- Solr使用:3.Solr添加文档到索引
- solr索引库的维护
- solr索引
- 3.跟我学solr---使用solrj添加索引
- 7.跟我学solr---对文件数据添加索引
- solr_4.5.0_07:从数据库向 solr 中添加索引
- Lucene&Solr(之二)-索引库CRUD、Solr的安装
- 2.跟我学solr---在solr admin中添加索引
- 使用solrj操作solr索引库
- RecyclerViewPool
- java8新特征
- 一个“错误”的vuex写法:vuex刷新了数据但是视图没有刷新
- 正则方式-取文本中间
- CF 845D Jury Meeting 枚举断点+前缀后缀
- solr索引库添加
- pyTorch学习(01)
- 关于Tesseract OCR 中文训练识别小试(java调用Tess4j)
- 对象的共享
- 【Linux】Shell
- Cause: java.sql.SQLException: ORA-01461: 仅能绑定要插入 LONG 列的 LONG 值
- MySQL 常用语法总结
- TCP传输数据。上传文件
- 用socket.io发私信