作者:张程Louis | 来源:互联网 | 2023-05-19 18:17
packagecom.qf.solrUtil;importjava.io.IOException;importjava.util.ArrayList;importjava.
package com.qf.solrUtil;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CloudSolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
public class solrUtli {
@Autowired
private HttpSolrServer solrServer;//单台solr检索服务器时使用,其他地方不用变
private CloudSolrServer solrServer2;//solr集群时使用,其他地方和单台的一样不用
private SolrServer solrServer3;//HttpSolrServer、CloudSolrServer的父类,单台solr或solr集群都可使用
public Page queryGoods(Page page,String keyword){
//搜索索引库
SolrQuery query = new SolrQuery();
query.setQuery("goods:"+keyword);
//设置高亮
query.setHighlight(true);//开启高亮
//添加需要高亮的字段
query.addHighlightField("gname");
query.addHighlightField("gdesc");
//设置高亮字段的样式
query.setHighlightSimplePre("");
query.setHighlightSimplePost("");
query.setHighlightSnippets(2);//分段的数量,默认为1
query.setHighlightFragsize(3);//每段的长度
//执行分页
query.setStart((page.getCurrentPage()-1)*page.getPageSize());
query.setRows(page.getPageSize());
//执行搜索
ArrayList goodsList = new ArrayList();
try {
QueryResponse respOnse= solrServer.query(query);
Map>> highlights = response.getHighlighting();
for(Entry>> entry : highlights.entrySet()){
System.out.println("============");
System.out.println("id:"+entry.getKey());
System.out.println("content:"+entry.getValue());
}
//获得结果集
SolrDocumentList results = response.getResults();
//获得结果总条数
page.setTotalCount((int)results.getNumFound());
for (SolrDocument document : results) {
Goods good = new Goods();
good.setId(Integer.parseInt(document.getFieldValue("id").toString()));
good.setPrice(Double.parseDouble(document.getFieldValue("gprice").toString()));
good.setGimage(document.getFieldValue("gimage").toString());
//判断是否有高亮内容
Map> maps = highlights.get(document.getFieldValue("id"));
if(maps.containsKey("gname")){
good.setGname(maps.get("gname").get(0));
}else {
good.setGname(document.getFieldValue("gname").toString());
}
goodsList.add(good);
}
page.setPageInfoEs(goodsList);
} catch (SolrServerException e) {
e.printStackTrace();
}
return page;
}
/**
* 数据库和索引库的同步
*/
public void syncData(){
//1.从数据库中查询所有商品
List goods = goodsDao.queryAll();
//2.构建索引库的数据集合
List documents = new ArrayList();
for (Goods good : goods) {
SolrInputDocument document = new SolrInputDocument();
document.addField("gname",good.getGname());
document.addField("gdesc", good.getGdesc());
document.addField("gprice", good.getPrice());
document.addField("gimage", good.getGimage());
documents.add(document);
}
try {
solrServer.add(documents);
solrServer.commit();
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 添数据到索引库
*/
public void addData(){
SolrInputDocument document = new SolrInputDocument();
document.addField("id", 1000);
document.addField("gname", "波轮洗衣机");
document.addField("gdesc", "可以煎蛋的洗衣机");
document.addField("gprice", 5000);
document.addField("gimage", "http://www.baidu.com");
try {
solrServer.add(document);
solrServer.commit();
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}