将商品表的数据导入solr库中
1 商品表
2根据商品表,对配置文件进行修改
3启动
1》看分词器是否有用
2》有一些黄色的警告
扫描二维码关注公众号,回复:
3930398 查看本文章
修改:(倒依赖的地方,没用的时候注释掉)
4导数据
1》itcast-solrj工程
2》
4》搜索
1-1>建工程
2》倒依赖
3》配置文件
4》导入静态文件
5》controller:
package com.taotao.search.controller;
import java.util.List;
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.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.taotao.common.bean.EasyUiResult;
import com.taotao.search.pojo.Item;
import com.taotao.search.service.SearchService;
@Controller
public class SearchController {
@Autowired
private SearchService searchService;
/**
* http://search.taotao.com/search.html?keyword=iphonex
* 同步 跳转到搜索系统 搜索页 search.jsp
* 参数 keyword搜索关键字 page 当前页
* 结果 query:搜索关键字 ; itemList:搜索的商品集合; page:页码; pages:总页数
*/
@RequestMapping(value="search")
public ModelAndView search(@RequestParam(value="q") String keywords,
@RequestParam(value="page",defaultValue="1") Integer page){
Integer pageSize = 32;
ModelAndView mv = new ModelAndView("search");
try {
//解决搜索参数乱码问题
keywords = new String(keywords.getBytes("ISO-8859-1"), "UTF-8");
mv.addObject("query",keywords);
EasyUiResult result = this.searchService.search(keywords,page,pageSize);
@SuppressWarnings("unchecked")
List<Item> itemList = (List<Item>) result.getRows();
//搜索的商品集合
mv.addObject("itemList", itemList);
//页码
mv.addObject("page", page);
//总页数 = 总记录数 % 每页显示条数 == 0 ? 总记录数 / 每页显示条数 : 总记录数/每页显示条数 + 1
Long total = result.getTotal();
Integer itemTotal = Integer.valueOf(total.toString());
Integer pages = itemTotal % pageSize == 0 ? itemTotal / pageSize : itemTotal / pageSize + 1;
mv.addObject("pages", pages);
} catch (Exception e) {
e.printStackTrace();
}
return mv;
}
}
6》service
package com.taotao.search.service;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.taotao.common.bean.EasyUiResult;
import com.taotao.search.pojo.Item;
@Service
public class SearchService {
@Autowired
private HttpSolrServer httpSolrServer;
public EasyUiResult search(String keywords, Integer page, Integer rows) throws SolrServerException {
SolrQuery solrQuery = new SolrQuery(); //构造搜索条件
solrQuery.setQuery(keywords); //搜索关键词
// 设置分页 start=0就是从0开始,,rows=5当前返回5条记录,第二页就是变化start这个值为5就可以了。
solrQuery.setStart((Math.max(page, 1) - 1) * rows);
solrQuery.setRows(rows);
//排序
solrQuery.setSort("id", ORDER.asc);
//是否需要高亮
boolean isHighlighting = !StringUtils.equals("*", keywords) && StringUtils.isNotEmpty(keywords);
if (isHighlighting) {
// 设置高亮
solrQuery.setHighlight(true); // 开启高亮组件
solrQuery.addHighlightField("title");// 高亮字段
solrQuery.setHighlightSimplePre("<em color='red'>");// 标记,高亮关键字前缀
solrQuery.setHighlightSimplePost("</em>");// 后缀
}
// 执行查询
QueryResponse queryResponse = this.httpSolrServer.query(solrQuery);
List<Item> items = queryResponse.getBeans(Item.class);
if (isHighlighting) {
// 将高亮的标题数据写回到数据对象中
Map<String, Map<String, List<String>>> map = queryResponse.getHighlighting();
for (Map.Entry<String, Map<String, List<String>>> highlighting : map.entrySet()) {
for (Item item : items) {
if (!highlighting.getKey().equals(item.getId().toString())) {
continue;
}
item.setTitle(StringUtils.join(highlighting.getValue().get("title"), ""));
break;
}
}
}
EasyUiResult result = new EasyUiResult(queryResponse.getResults().getNumFound(), items);
return result;
}
}
package com.taotao.search.pojo;
import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.beans.Field;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Item {
@Field("id")
private Long id;
@Field("title")
private String title;
@Field("sellPoint")
private String sellPoint;
@Field("price")
private Long price;
@Field("image")
private String image;
@Field("cid")
private Long cid;
// @Field("cname")
private String cname;
@Field("status")
private Integer status;
// @Field("created")
private Long created;
@Field("updated")
private Long updated;
public Long getCreated() {
return created;
}
public void setCreated(Long created) {
this.created = created;
}
public Long getUpdated() {
return updated;
}
public void setUpdated(Long updated) {
this.updated = updated;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSellPoint() {
return sellPoint;
}
public void setSellPoint(String sellPoint) {
this.sellPoint = sellPoint;
}
public Long getPrice() {
return price;
}
public void setPrice(Long price) {
this.price = price;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public Long getCid() {
return cid;
}
public void setCid(Long cid) {
this.cid = cid;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String[] getImages(){
return StringUtils.split(this.getImage(), ",");
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
@Override
public String toString() {
return "Item [id=" + id + ", title=" + title + ", sellPoint=" + sellPoint + ", price=" + price
+ ", image=" + image + ", cid=" + cid + ", status=" + status + "]";
}
}
7>将 HttpSolrServer对象注入到spring中
applicationContext-solr.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<bean class="org.apache.solr.client.solrj.impl.HttpSolrServer">
<constructor-arg index="0" value="${solr.url}"/>
<!--设置响应解析器 -->
<property name="parser">
<bean class="org.apache.solr.client.solrj.impl.XMLResponseParser"/>
</property>
<!-- 设置重试次数,推荐设置为1 -->
<property name="maxRetries" value="${solr.maxRetries}"/>
<!-- 建立连接的最长时间 -->
<property name="connectionTimeout" value="${solr.connectionTimeout}"/>
</bean>
</beans>
solr.properties
solr.url=http://solr.taotao.com/taotao
solr.maxRetries=1
solr.connectionTimeout=500