配置好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;
}
}
配置好solr搜索引擎服务器后java后台如何将商品信息导入索引库的更多相关文章
- JAVAEE——宜立方商城07:Linux上搭建Solr服务、数据库导入索引库、搜索功能的实现
1. 学习计划 1.Solr服务搭建 2.Solrj使用测试 3.把数据库中的数据导入索引库 4.搜索功能的实现 2. Solr服务搭建 2.1. Solr的环境 Solr是java开发. 需要安装j ...
- Solr搜索引擎服务器学习笔记
Solr简介 采用Java5开发,基于Lucene的全文搜索服务器.同时对其进行了扩展,提供了比Lucene更为丰富的查询语言,同时实现了可配置.可扩展并对查询性能进行了优化,并且提供了一个完善的功能 ...
- encodeURIComponent编码后java后台的解码 (AJAX中文解决方案)
encodeURIComponent编码后java后台的解码 (AJAX中文解决方案) 同学的毕业设计出现JavaScript用encodeURIComponentt编码后无法再后台解码的问题. 原来 ...
- 【java】JDK安装后,没有配置环境变量,也可以java -version查看到版本信息
JDK安装后,没有配置环境变量,也可以java -version查看到版本信息 原因是:jdk安装过程,java.javaw.javaws三个命令被复制到C:\windows\system32目录下 ...
- 【Solr】数据库数据导入索引库
目录 分析框图 配置数据库与solrconfig.xml 回到顶部 分析框图 框图画的粗糙!勿喷啊!勿喷啊! 回到顶部 配置数据库与solrconfig.xml Dataimport插件 可以批量把数 ...
- 【Java Web】简易商品信息管理系统——首个Web项目
正文之前 在学习了一段时间的Java Web的内容之后,当然需要有个项目来练练手,我相信大多数人的首选项目都是信息管理系统吧,所以我选择了商品信息管理系统 目前项目源码已全部上传至GitHub,欢迎大 ...
- Java Swing设计简单商品信息管理系统(java swing+mysql+eclipse)
一.概述 为了管理好商店库存信息,提升店铺管理工作效率,结合实际工作需要,设计和开发本系统,主要用于商店商品信息维护出入库等.包含商品库存信息查看.商品信息修改,新增商品信息,删除信息等功能. 二.功 ...
- encodeURIComponent编码后java后台的解码
解决方法一: JavaScript: window.self.location="searchbytext.action?searchtext="+encodeURICompone ...
- dedecms织梦网站本地迁移到服务器后,后台更新栏目文档提示模板文件不存在,无法解析文档!的解决办法
解决办法: 1.系统设置-系统基本参数-站点设置-网页主页链接,替换为空 2.系统设置-系统基本参数-核心设置-DedeCMS安装目录,替换为空
随机推荐
- python 终级篇 django ---ORM操作
一般操作 必会的 ...
- OpenMPI源码剖析4:rte.h 头文件的说明信息
上一篇文章中说道,我们在 rte.h 中发现了有价值的说明: 我们一块一块来分析,首先看到第一块,关于 Process name Object: * (a) Process name objects ...
- Sharepoint 2013与Sharepoint 2016的功能对比
开发人员功能 SharePoint Foundation 2013 SharePoint Server 2013 Standard CAL SharePoint Server 2013 Enterpr ...
- 数独:dfs+剪枝+位运算+排除冗余+优化搜索顺序(未完)
和蓝桥杯以前一个题一样,但是数据加强了,博主水平有限,没做出来,先在这里记录一下,这里正解,下面是博主的超时做法.最近准备考研,不能深入学习了. 题目描述 数独是一种传统益智游戏,你需要把一个9 × ...
- Paper Reading - Show, Attend and Tell: Neural Image Caption Generation with Visual Attention ( ICML 2015 )
Link of the Paper: https://arxiv.org/pdf/1502.03044.pdf Main Points: Encoder-Decoder Framework: Enco ...
- [转载]Tensorflow中reduction_indices 的用法
Tensorflow中reduction_indices 的用法 默认时None 压缩成一维
- cenos环境变量配置
Beego环境搭建和bee工具安装使用,以Windows环境为例. 首先,下载并安装好GO并配置好GOROOT和GOPATH环境变量(如果您是用msi包安装的go,那么这些环境变量已经设置好了).并在 ...
- 基于spring-boot、spring-cloud的websocket服务器多点负载均衡改造
背景 为应对更多用户使用socket的场景,准备对存放websocket的服务器进行多点搭建并配置负载均衡. 问题 服务器上了多点负载均衡以后,基于socket的部分功能发生了有规律的失效,查看后台日 ...
- HDU 2491 Priest John's Busiest Day(贪心)(2008 Asia Regional Beijing)
Description John is the only priest in his town. October 26th is the John's busiest day in a year be ...
- ZOJ 2532 Internship(最大流找关键割边)
Description CIA headquarter collects data from across the country through its classified network. Th ...