springboot集成solr 基础应用
1.首先导入solr依赖
2.配置application.yml文件
实体类代码
@Entity
@Table(name = "item")
public class Item implements Serializable{
@Field
private Long id;
@Field("item_name")
private String item_name;
@Field("item_price")
private double item_price;
@Field("item_starttime")
@JsonFormat(pattern = "yyyy-MM-dd")
private Date item_starttime;
@Field("item_endtime")
@JsonFormat(pattern = "yyyy-MM-dd")
private Date item_endtime;
@Field("item_brand")
private String item_brand;
@Field("item_content")
private String item_content;
@Field("item_uid")
private String uid;
/**
*get和set方法省略
*/
控制层代码
@RestController
public class ItemController {
@Autowired
private ItemInterface itemInterface;
//注入solr
@Autowired
private SolrClient solrClient;
/**
* 添加一条数据
* @throws IOException
* @throws SolrServerException
*/
@RequestMapping("addOne")
public void addOne() throws IOException, SolrServerException {
Item item = new Item();
item.setId(123L);
item.setItem_name("三星手机");
item.setItem_brand("三星");
item.setItem_content("会爆炸");
item.setItem_price(5999.99);
item.setItem_starttime(new Date());
item.setItem_endtime(new Date());
item.setUid(new Random().nextInt(999999999)+"");
solrClient.addBean(item);
solrClient.commit();
}
/**
* 将数据库的数据导入solr
*/
@RequestMapping("selectAll")
public void selectAll(){
List<Item> items = itemInterface.itemList();
System.out.println(items);
try {
solrClient.addBeans(items);
solrClient.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 删除全部数据
*/
@RequestMapping("deleteAll")
public void deleteAll(){
try {
solrClient.deleteByQuery("*:*");
solrClient.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 根据id查询数据
* @param item_id
*/
@RequestMapping("selectOne/{item_id}")
public void deleteAll(@PathVariable("item_id") String item_id){
try {
SolrDocument byId = solrClient.getById(item_id);
Map<String, Object> fieldValueMap = byId.getFieldValueMap();
System.out.println("fieldValueMap========="+fieldValueMap);
System.out.println(byId);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 多条件查询+高亮展示
* @param brand
* @return
* @throws Exception
*/
@RequestMapping("selectHighLight")
public List<Map<String, Object>> selectHighLight(String brand) throws Exception {
//创建查询
SolrQuery solrQuery = new SolrQuery();
//判断传入的条件是否为空,如果为空就不设置高亮字段
if(brand!=""){
solrQuery.set("q", "item_brand:*"+brand+"*");
//查询条件
solrQuery.setSort("id", SolrQuery.ORDER.asc);
//查询开始
solrQuery.setStart(0);
//设置查询条数
solrQuery.setRows(50);
//开启高亮
solrQuery.setHighlight(true);
solrQuery.addHighlightField("item_brand");
solrQuery.setHighlightSimplePre("<span style='color:red'>");
solrQuery.setHighlightSimplePost("</span>");
//查询高亮
QueryResponse response = solrClient.query("collection1", solrQuery);
//返回高亮结果集
SolrDocumentList results = response.getResults();
//返回高亮显示结果
Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
System.out.println("highlighting=========="+highlighting);
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> m;
for (SolrDocument highLights: results) {
String id = highLights.getFirstValue("id").toString();
String item_name = highLights.getFirstValue("item_name").toString();
String item_price = highLights.getFirstValue("item_price").toString();
String item_starttime = highLights.getFirstValue("item_starttime").toString();
String item_endtime = highLights.getFirstValue("item_endtime").toString();
String item_brand = highLights.getFirstValue("item_brand").toString();
String item_content = highLights.getFirstValue("item_content").toString();
String item_uid = highLights.getFirstValue("item_uid").toString();
Map<String, List<String>> itemMap = highlighting.get(highLights.get("id"));
String itemBrand = itemMap.get("item_brand").get(0);
m = new HashMap<String,Object>();
//m.put("highlighting",highlighting);
m.put("id",id);
m.put("item_name",item_name);
m.put("item_price",item_price);
m.put("item_starttime",item_starttime);
m.put("item_endtime",item_endtime);
m.put("item_brand",itemBrand);
m.put("item_content",item_content);
m.put("item_uid",item_uid);
list.add(m);
}
System.out.println("list=========="+list);
return list;
}
//查询全部列表的情况下
SolrQuery params = new SolrQuery();
// 查询条件
params.set("q", "*:*");
// 排序
params.addSort("id", SolrQuery.ORDER.asc);
// 分页
params.setStart(0);
params.setRows(50);
// solr数据库是collection1
QueryResponse queryResponse = solrClient.query("collection1", params);
SolrDocumentList results = queryResponse.getResults();
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> m;
for (SolrDocument collection:results) {
String id = collection.getFirstValue("id").toString();
String item_name = collection.getFirstValue("item_name").toString();
String item_price = collection.getFirstValue("item_price").toString();
String item_starttime = collection.getFirstValue("item_starttime").toString();
String item_endtime = collection.getFirstValue("item_endtime").toString();
String item_brand = collection.getFirstValue("item_brand").toString();
String item_content = collection.getFirstValue("item_content").toString();
String item_uid = collection.getFirstValue("item_uid").toString();
m = new HashMap<String,Object>();
m.put("id",id);
m.put("item_name",item_name);
m.put("item_price",item_price);
m.put("item_starttime",item_starttime);
m.put("item_endtime",item_endtime);
m.put("item_brand",item_brand);
m.put("item_content",item_content);
m.put("item_uid",item_uid);
list.add(m);
}
System.out.println(list);
return list;
}
}
前台页面(这里我用的是angularJs)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>商品</title>
<script src="angular.js"></script>
<link rel="stylesheet" href="myresource/css/index1.css">
</head>
<body ng-app="myApp" ng-controller="myController">
<input type="text" placeholder="请输入要查询的品牌" ng-model="brand">
<input type="button" value="查询" ng-click="findBrand()">
<table>
<tr>
<td>商品ID</td>
<td>商品名称</td>
<td>商品价格</td>
<td>上架时间</td>
<td>下架时间</td>
<td>品牌</td>
<td>商品简介</td>
<td>用户ID</td>
</tr>
<tr ng-repeat="b in brandList">
<td>{{b.id}}</td>
<td>{{b.item_name}}</td>
<td>{{b.item_price}}</td>
<td>{{b.item_starttime}}</td>
<td>{{b.item_endtime}}</td>
//由于angularjs会拦截高亮展示的字段
//加入这段代码让angularjs成为受信任的HTML
<td ng-bind-html="b.item_brand | trustHtml"></td>
<td>{{b.item_content}}</td>
<td>{{b.item_uid}}</td>
</tr>
</table>
</body>
<script>
var app = angular.module("myApp",[]);
app.controller("myController",function ($http,$scope) {
$scope.brand=$scope.brand==undefined?"":$scope.brand;
$scope.findBrand = function(){
$http.get("/selectHighLight?brand="+$scope.brand).then(function(response) {
console.log(response.data)
$scope.brandList = response.data
})
}
$scope.findBrand();
})
app.filter('trustHtml',['$sce',function($sce){
return function(data){
return $sce.trustAsHtml(data);
}
}]);
</script>
</html>
效果展示
品牌字段已经高亮
此文章只面向入门选手参考,大佬勿喷