您好,登錄后才能下訂單哦!
本篇文章為大家展示了【學習筆記】如何配置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); } } }
上述內容就是【學習筆記】如何配置Solr索引庫,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。