搭建Solr环境
SpringBoot整合Solr
1、添加整合依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-solr</artifactId>
</dependency>
2、配置application.yml
3、注入Solr对象,实现crud
@RunWith(SpringRunner.class)
@SpringBootTest
class ShopSearchApplicationTests {
@Autowired
private SolrClient solrClient;
/*
* 添加 id 不同为新增
* 修改 id 相同为修改
* */
@Test
public void add() throws IOException, SolrServerException {
//创建document对象
SolrInputDocument document=new SolrInputDocument();
//添加的时候,必须加上id属性,否则报错
//属性值为object类型,传入的属性如果是BigDecimal等其他非Object类型的,需要转为Object类型,否则报错
document.addField("id",2);
document.addField("gname","苹果手机");
document.addField("gimage","http://www.iphone.com");
document.addField("ginfo","外国手机");
document.addField("gprice",6999.88);
document.addField("gsave",3232);
solrClient.add(document);
solrClient.commit();
}
@Test
public void query() throws IOException, SolrServerException {
SolrQuery solrQuery=new SolrQuery();
//查询的属性必须在solr库中有定义 ,否则会报错:
//Error from server at http://192.168.65.128:8080/solr: undefined field gnames
//只针对于gname属性进行查询
//solrQuery.setQuery("gname:苹果手机");
//针对多属性进行查询(注意多条件连接时,之间要有空格)
//solrQuery.setQuery("gname:手机 || ginfo:苹果");
String keyword="手机";
solrQuery.setQuery("gname:"+keyword+" || ginfo:"+keyword);
QueryResponse queryResponse = solrClient.query(solrQuery);
SolrDocumentList results = queryResponse.getResults();
for (SolrDocument document:results) {
String id= (String) document.get("id");
String gname= (String) document.get("gname");
String gimage= (String) document.get("gimage");
String ginfo= (String) document.get("ginfo");
float gprice= (float) document.get("gprice");
int gsave= (int) document.get("gsave");
System.out.println(id+" "+gname+" "+gimage+" "+ginfo+" "+gprice+" "+gsave);
}
}
@Test
public void delete() {
try {
//根据id进行删除
//solrClient.deleteById("7");
//根据查询后的结果进行删除
solrClient.deleteByQuery("gname:安定");
solrClient.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
}
4、添加对象到索引库
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("goods")
public class Goods implements Serializable {
//mybatisPlus默认开启了主键回填策略,只需要在实体类中,指明主键id即可
@TableId(type = IdType.AUTO)
private int id ;
private String gname;
private BigDecimal gprice;
private int gsave;
private String ginfo;
private String gimage;
private int status;
@TableField("create_time")
private Date createTime=new Date();
private int tid;
}
/*
* 添加商品时,将商品添加到搜索库中
* */
@Override
public int insert(Goods goods) {
SolrInputDocument solrInputDocument=new SolrInputDocument();
solrInputDocument.addField("id",goods.getId());
solrInputDocument.addField("gname",goods.getGname());
solrInputDocument.addField("gimage",goods.getGimage());
solrInputDocument.addField("ginfo",goods.getGinfo());
solrInputDocument.addField("gsave",goods.getGsave());
//不能传入BigDecimal类型
solrInputDocument.addField("gprice",goods.getGprice().doubleValue());
try {
solrClient.add(solrInputDocument);
solrClient.commit();
return 1;
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
5、搜索关键字高亮显示
@Autowired
private SolrClient solrClient;
@Override
public List<Goods> searchGoodsByKeyword(String keyword) {
SolrQuery solrQuery=new SolrQuery();
if (keyword==null){
//搜索全部商品
solrQuery.setQuery("*:*");
}else{
//根据条件搜索(注意多条件连接时,之间要有空格)
solrQuery.setQuery("gname:"+keyword+" || ginfo:"+keyword);
}
//设置高亮
solrQuery.setHighlight(true);
solrQuery.setHighlightSimplePre("<font color='red'>");
solrQuery.setHighlightSimplePost("</font>");
//添加需要高亮的字段
solrQuery.addHighlightField("gname");
try {
QueryResponse results = solrClient.query(solrQuery);
//获得高亮的结果
//Map<id,Map<Field,List<String>>>
//id:有高亮的商品id Field:当前商品有高亮的字段 List:高亮的内容
Map<String, Map<String, List<String>>> highlighting = results.getHighlighting();
SolrDocumentList documents = results.getResults();
List<Goods> goodsList=new ArrayList<>();
for (SolrDocument document:documents) {
Goods goods=new Goods();
goods.setId(Integer.parseInt(document.get("id")+""));
goods.setGname(document.get("gname")+"");
goods.setGimage(document.get("gimage")+"");
goods.setGprice(BigDecimal.valueOf(Double.parseDouble(document.get("gprice")+"")));
goods.setGsave(Integer.parseInt(document.get("gsave")+""));
goodsList.add(goods);
//判断当前商品是否有高亮 根据商品id进行判断
if (highlighting.containsKey(goods.getId()+"")){
//获取有高亮的内容
Map<String, List<String>> map = highlighting.get(goods.getId() + "");
//获取高亮的内容
String gname = map.get("gname").get(0);
//将高亮的内容替换到对象中
goods.setGname(gname);
}
}
return goodsList;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
6、thymeleaf页面加载高亮显示的属性
——th:utext(以html的方式解析) 、th:text(以文本的方式解析)
<li th:each="good :${goods}">
<dl>
<dt><a ><img th:src="|http://192.168.65.128/${#strings.setSplit(good.gimage,'|')[0]}|"/></a></dt>
<dd class="title"><a th:utext="${good.gname}">OCIAIZO春装水洗做旧短外套复古磨白短款牛仔外套春01C1417</a></dd>
<dd class="content">
<span class="goods_jiage"><strong th:text="${#numbers.formatCurrency(good.gprice)}">249.00</strong></span>
<span class="goods_chengjiao">库存:<a th:text="${good.gsave}"></a></span>
</dd>
</dl>
</li>
既然看到这里了,不妨点个赞,加个关注把。。。