【學習筆記】Solr索引庫的配置整理
阿新 • • 發佈:2019-07-09
Spring整合Solr
Spring配置:
<?xml version="1.0" encoding="UTF-8"?> <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.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"> <context:component-scan base-package="cn.xing"/> <bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer"> <constructor-arg index="0" value="http://192.168.159.130:8080/solr"/> </bean> </beans>
Java程式碼操作Solr索引庫:
package cn.xing.test; import java.util.ArrayList; import java.util.List; import java.util.Map; 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.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; import org.springframework.stereotype.Service; import cn.xing.pojo.SearchItem; import cn.xing.pojo.SearchResult; /** * 操作solr索引庫 * * @author Xing * */ @Service public class Operation { @Autowired private SolrServer solrServer;// 是配置的httpSolrServer /** * 把所有商品匯入索引庫,商品物件SearchItem必須和solr配置的業務域欄位、型別相同 */ public void imprtSolrItem() { try { List<SearchItem> list = new ArrayList<SearchItem>(); list.add(new SearchItem("1", "湖北", "黃鶴樓", 12, "image1", "省市")); list.add(new SearchItem("2", "河南", "開封", 12, "image2", "省市")); list.add(new SearchItem("3", "3", "3", 12, "image2", "222")); // 把list放入索引庫 for (SearchItem searchItem : list) { SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", searchItem.getId()); doc.addField("item_title", searchItem.getTitle()); doc.addField("item_sell_point", searchItem.getSell_point()); doc.addField("item_price", searchItem.getPrice()); doc.addField("item_image", searchItem.getImage()); doc.addField("item_category_name", searchItem.getCategory_name()); solrServer.add(doc); } solrServer.commit(); System.out.println("資料匯入成功"); } catch (Exception e) { e.printStackTrace(); } } // 根據查詢條件SolrQuery,查詢索引庫 public List<SearchItem> querySolr(SolrQuery query) throws SolrServerException { // 根據條件查詢索引庫 QueryResponse queryResponse = solrServer.query(query); SolrDocumentList results = queryResponse.getResults(); // 取出總記錄數 long num = results.getNumFound(); System.out.println("查詢結果的資料條數:" + num); // 取出商品列表,高亮關鍵詞 // Map<商品id,Map<商品屬性欄位,List<欄位的值>>> Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting(); List<SearchItem> itemList = new ArrayList<>(); for (SolrDocument solrDocument : results) { SearchItem item = new SearchItem(); item.setId((String) solrDocument.get("id")); item.setCategory_name((String) solrDocument.get("item_category_name")); item.setImage((String) solrDocument.get("item_image")); item.setPrice((long) solrDocument.get("item_price")); item.setSell_point((String) solrDocument.get("item_sell_point")); // 取高亮結果,商品標題 List<String> list = highlighting.get(solrDocument.get("id")).get("item_title"); if (list != null && list.size() > 0) { item.setTitle(list.get(0));// 拿到該商品標題 } else { item.setTitle((String) solrDocument.get("item_title")); } itemList.add(item); } return itemList; } /** * 根據關鍵詞在solr索引庫中搜索商品並分頁。 */ public SearchResult search(String keyword, int page, int rows) throws Exception { // 設定查詢條件,呼叫querySolr()方法查詢 SolrQuery query = new SolrQuery(); query.setQuery(keyword);// 設定查詢條件 if (page <= 0) page = 1; query.setStart((page - 1) * rows);// 設定分頁索引 query.setRows(rows);// 設定查詢條數 // 設定預設搜尋域 query.set("df", "item_title"); // 設定高亮顯示 query.setHighlight(true); query.addHighlightField("item_title"); query.setHighlightSimplePre("<em style=\"color:red\">"); query.setHighlightSimplePost("</em>"); // 執行查詢 List<SearchItem> itemList = this.querySolr(query); // 設定返回結果 SearchResult rs = new SearchResult(); rs.setItemList(itemList); /* * // 設定總頁數 int totalpages = rs.getRecourdCount() / rows; if * (rs.getRecourdCount() % rows > 0) totalpages++; rs.setTotalPages((long) * totalpages); */ return rs; } }
分頁查詢資料封裝:
package cn.xing.pojo; import java.io.Serializable; import java.util.List; /** * 搜素商品的結果。對頁面需要的資料的封裝。 * * @author Xing * */ public class SearchResult implements Serializable { private Long totalPages; private Integer recourdCount; private List<SearchItem> itemList; }
Solr資料對應的pojo:
package cn.xing.pojo;
import java.io.Serializable;
import org.apache.commons.lang3.StringUtils;
public class SearchItem implements Serializable {
@Override
public String toString() {
return "SearchItem [id=" + id + ", title=" + title + ", sell_point=" + sell_point + ", price=" + price
+ ", image=" + image + ", category_name=" + category_name + "]";
}
// 型別和solr的中文分析器裡的schema.xml中配置的type必須一樣
private String id;
private String title;
private String sell_point;
private long price;
private String image;
private String category_name;
// 搜尋結果頁面展示一張圖片
public String[] getImages() {
String image2 = this.getImage();
if (StringUtils.isNotBlank(image2)) {
String[] imagesArr = image2.split(",");
return imagesArr;
}
return null;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
Java測試程式碼:
package cn.xing.test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.xing.pojo.SearchItem;
import cn.xing.pojo.SearchResult;
public class test {
public static void main(String[] args) throws Exception {
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
Operation operation = ac.getBean(Operation.class);
// 匯入資料
operation.imprtSolrItem();
// SearchResult search = operation.search("明明就", 1, 1);
SearchResult search = operation.search("3", 1, 10);
List<SearchItem> itemList = search.getItemList();
for (SearchItem searchItem : itemList) {
System.out.println(searchItem);
}
}
}