一.數據庫單表有百萬條數據,走數據庫查詢速度比較慢,決定搭一臺搜索服務器,數據放到搜索服務器上,接口從搜索引擎中去查詢。項目中採用的是solr 搜索引擎。
Solr的優缺點
優點
- Solr有一個更大、更成熟的用戶、開發和貢獻者社區。
- 支持添加多種格式的索引,如:HTML、PDF、微軟 Office 系列軟件格式以及 JSON、XML、CSV 等純文本格式。
- Solr比較成熟、穩定。
- 不考慮建索引的同時進行搜索,速度更快。
缺點
- 建立索引時,搜索效率下降,實時索引搜索效率不高。
二.項目中採用的是solr7.2版本,maven管理項目,直接導入solr的依賴即可。
三. 項目中的實際使用
SolrQuery query = new SolrQuery();
// 設置參數
query.setQuery(queryParam); // 設置查詢參數
query.set("group","true"); // 可以分組
query.set("group.field","mallId"); // 分組字段爲 mallId
query.set("group.ngroups","true"); // 分組後 返回分組後的總條數
query.addFacetField("mallId"); //
query.set("sort","mallName asc"); // 設置排序
query.setStart(req.getLimitStart()); // 設置搜索的起始順序
query.setRows(req.getPageSize()); // 設置搜索的數量
Map<String, Object> searchMap = req.getSearchMap();
StringBuilder builder = new StringBuilder("");
// 封裝搜索參數
for(String param : searchMap.keySet()) {
Object value = searchMap.get(param);
builder.append(" AND ");
if("floorName".equals(param)){
builder.append("floor").append(":").append("\"" + value + "\"");
continue;
}
builder.append(param).append(":").append("\"" + value + "\"");
} // 如果是多參數請求, 封裝請求參數 中間用 " AND "隔開
String queryParam = builder.length()> 0?builder.substring(" AND ".length(),builder.length()):"*:*";
創建連接,封裝工具類
public class SolrUtil {
// url 在properties配置
private final static String solrUrl = SystemConstants.getSolrUrl();
public static HttpSolrClient getSolrClient(){
return new HttpSolrClient.Builder(solrUrl)
.withConnectionTimeout(10000)
.withSocketTimeout(60000)
.build();
}
}
創建連接,並賦值代碼如下
try{
//創建連接
HttpSolrClient server = SolrUtil.getSolrClient();
rsp = server.query("yunpu",query); // yunpu 是指solr中的collection名稱
} catch(Exception e){
e.printStackTrace();
}
GroupResponse groupResponse = rsp.getGroupResponse();
if(groupResponse != null){
List<GroupCommand> commands = groupResponse.getValues();
if(commands != null) {
for(GroupCommand command: commands){
// 返回總記錄數
pageResp.setTotal(command.getNGroups());
List<Group> groups = command.getValues();
if(groups !=null){
for(Group group:groups){
SolrDocumentList solrDocument = group.getResult();
Iterator<SolrDocument> documentList = solrDocument.iterator();
while(documentList.hasNext()){
PopupDpMallInfo mallInfo = new PopupDpMallInfo();
// 如果mallId爲 null跳出循環
SolrDocument document=documentList.next();
if(document.get("mallId") == null){
continue;
}
mallInfo.setMallId((Integer) document.get("mallId"));
mallInfo.setMallName((String) document.get("mallName"));
mallInfo.setAddress((String) document.get("address"));
mallInfo.setProvinceName((String) document.get("provinceName"));
mallInfo.setCityName((String) document.get("cityName"));
mallInfo.setDistrictName((String) document.get("districtName"));
mallInfo.setDpUrl((String) document.get("dpUrl"));
// 查詢一個商業體下所有的商鋪
SolrQuery queryStore = new SolrQuery();
queryStore.setQuery("mallId:"+mallInfo.getMallId());
queryStore.set("start", 0);
queryStore.set("rows",Integer.MAX_VALUE);
queryStore.set("sort","floor asc");
QueryResponse storeRsp = null;
try {
storeRsp = SolrUtil.getSolrClient().query("yunpu",queryStore);
} catch (Exception e) {
e.printStackTrace();
}
SolrDocumentList storeDocumentList =storeRsp.getResults();
List<PopupDpShopInfoVo> shopInfoVoList = getShopInfoVoList(storeDocumentList);
mallInfo.setStoreNum(storeDocumentList.size());
mallInfo.setShopInfoVoList(shopInfoVoList);
dpMallInfoList.add(mallInfo);
}
}
}
}
}
中間摸索了一陣子,基本實現了需求。先記錄一下子