Solr单集代码调用案例
生活随笔
收集整理的這篇文章主要介紹了
Solr单集代码调用案例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
實現代碼
1、配置pom,在maven添加solr的依賴
配置如下內容:
配置要連接的solrcore
applicationContext.xml的代碼如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:task="http://www.springframework.org/schema/task"xmlns:mongo="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation=" http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/data/mongohttp://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd"><!-- 打開注解 --><context:annotation-config /><!-- <aop:aspectj-autoproxy/> --><!-- 打開自動掃描 --><context:component-scan base-package="cn.com.hbny.docdetection" /><!-- 定時器驅動 --><task:annotation-driven/><!-- 引入jdbc配置文件 --><context:property-placeholder location="classpath:jdbc.properties,classpath:mongodb.properties,classpath:solr.properties" /><!-- 配置solr服務器的地址 --><bean class="org.apache.solr.client.solrj.impl.HttpSolrServer"><constructor-arg value="${solr.host}"></constructor-arg></bean></beans>DocInfoSolrService代碼實現:
package cn.com.hbny.docdetection.solr;import cn.com.hbny.docdetection.solr.po.ResultModel;public interface DocInfoSolrService {/*** 通過命名空間,檢測庫Id,文檔內容,查找符合條件的元素* @param isPreciseSearch :是否是精確查找 true表示的是精確查找,false表示的是粗略查找* @param ns :報名* @param propertyId :屬性分類的* @param propertyType :屬性分類名稱* @param majorId :專業的id* @param title :標題* @param keyword :關鍵詞* @param sentences :句子內容* @param pageNum :頁碼數* @param pageSize :每頁的大小* @return* @attention 方法的使用注意事項 * @author toto* @date 2017年4月4日 * @note begin modify by 涂作權 2017年4月4日 原始創建*/public ResultModel findDocInfoBySolr(Boolean isPreciseSearch,String ns,String propertyId,String propertyType,String majorId,String title,String keyword,String sentences,Integer pageNum,Integer pageSize); }DocInfoSolrServiceImpl 代碼實現如下:
package cn.com.hbny.docdetection.solr.impl;import java.util.ArrayList; import java.util.List; import java.util.Map;import org.apache.commons.lang.StringUtils; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrQuery.ORDER; 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.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import com.ctc.wstx.util.StringUtil;import cn.com.hbny.docdetection.mongodb.beans.SentenceInfo; import cn.com.hbny.docdetection.server.ExtendedServerConfig; import cn.com.hbny.docdetection.solr.DocInfoSolrService; import cn.com.hbny.docdetection.solr.po.ResultModel; import cn.com.hbny.docdetection.utils.CosineSimilarAlgorithm; import cn.com.hbny.docdetection.utils.HtmlUtil;@Service public class DocInfoSolrServiceImpl implements DocInfoSolrService {@Autowiredprivate HttpSolrServer server;public String escapeQueryChars(String s) {StringBuilder sb = new StringBuilder();for (int i = 0; i < s.length(); i++) {char c = s.charAt(i);// These characters are part of the query syntax and must be escapedif (c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')' || c == ':' || c == '^'|| c == '[' || c == ']' || c == '\"' || c == '{' || c == '}' || c == '~' || c == '*' || c == '?'|| c == '|' || c == '&' || c == ';' || c == '/' || Character.isWhitespace(c)) {sb.append('\\');}sb.append(c);}return sb.toString();}/*** 相似的句子* @param sentences 要找出的相似的句子的原文* @param htmlPrefix 高亮的前綴* @param htmlSufix 高亮的后綴* @return* @attention 相似的句子* @author toto* @date 2017年4月15日 * @note begin modify by 涂作權 2017年4月15日 原始創建*/public String gainSimilarSentence(String sentences,String htmlPrefix,String htmlSufix) {if (StringUtils.isBlank(sentences)) return null;String similarSentence = "";try {//開始的位置int beginIndex = 0;int endIndex = 0;//最終要返回的相似的句子while(sentences.indexOf(htmlPrefix,endIndex) != -1) {beginIndex = sentences.indexOf(htmlPrefix,endIndex) + htmlPrefix.length();endIndex = sentences.indexOf(htmlSufix, beginIndex);if(beginIndex != 0) {similarSentence += sentences.substring(beginIndex, endIndex) + ";";} else {similarSentence += sentences.substring(beginIndex, endIndex);}}//去掉最后一個分號if (StringUtils.isNotBlank(similarSentence) && similarSentence.length() >= 2) {similarSentence = similarSentence.substring(0, similarSentence.length() - 1);}} catch(Exception e) {e.printStackTrace();}return similarSentence;}/*** 獲取單個句子的相似度的值* @param sentenceOriginal :作為搜索條件的句子* @param sentenceFromSolr :帶有高亮信息的從搜索庫中搜索出來的句子* @return* @attention 方法的使用注意事項 * @author toto* @date 2017年4月15日 * @note begin modify by 涂作權 2017年4月15日 原始創建*/public double gianSimilarityValue(String sentenceOriginal,String sentenceFromSolr) {//如果其中任何一個有一個參數值是0,這獲取到的相似度值為空0if (StringUtils.isBlank(sentenceOriginal) || StringUtils.isBlank(sentenceFromSolr)) return 0f;double similarityValue = 0f;try {//獲取到的不帶有html的句子String sentenceWithNoHtml = sentenceFromSolr;sentenceWithNoHtml = sentenceFromSolr.replaceAll("<font style=\"color:#006600;\">", "");sentenceWithNoHtml = sentenceWithNoHtml.replaceAll("</font>", ""); // System.out.println("================================================================"); // System.out.println("sentenceFromSolr = " + sentenceFromSolr); // System.out.println("sentenceWithNoHtml = " + sentenceWithNoHtml); // System.out.println("sentenceOriginal = " + sentenceOriginal); // System.out.println("================================================================");similarityValue = (float) CosineSimilarAlgorithm.getSimilarity(sentenceOriginal, sentenceWithNoHtml);} catch (Exception e) {e.printStackTrace();}return (similarityValue * 100);}/*** 通過命名空間,檢測庫Id,文檔內容,查找符合條件的元素* @param isPreciseSearch :是否是精確查找 true表示的是精確查找,false表示的是粗略查找* @param ns :報名* @param propertyId :屬性分類的* @param propertyType :屬性分類名稱* @param title :標題* @param keyword :關鍵詞* @param sentences :句子內容* @param pageNum :頁碼數* @param pageSize :每頁的大小* @return* @attention 方法的使用注意事項 * @author toto* @date 2017年4月4日 * @note begin modify by 涂作權 2017年4月4日 原始創建*/public ResultModel findDocInfoBySolr(Boolean isPreciseSearch,String ns, String propertyId,String propertyType,String majorId,String title,String keyword,String sentences,Integer pageNum,Integer pageSize) {//創建solrQuery對象try {//創建solrQuery對象SolrQuery query = new SolrQuery();//設置edismax的權重值query.set("defType", "edismax");query.set("fl", "*,score");query.setSort("score",ORDER.desc);//設置標題的最小匹配的百分比String solrMMValueForTitleAndKeyword = ExtendedServerConfig.getInstance().getStringProperty("SolrMMValueForTitleAndKeyword").trim();//設置句子最小匹配的百分比String SolrMMValue = ExtendedServerConfig.getInstance().getStringProperty("SolrMMValue").trim();//設置q,查詢條件StringBuilder params = new StringBuilder("ns:docdetection.hbny.SentenceInfo");//根據屬性id來進行查詢 // if (StringUtils.isNotBlank(propertyId)) { // propertyId = escapeQueryChars(propertyId); // params.append(" AND propertyId:" + propertyId); // }//根據專業id來進行查詢 // if (StringUtils.isNotBlank(majorId)) { // majorId = escapeQueryChars(majorId); // params.append(" AND majorId:" + majorId); // }//根據文檔的名稱來進行查詢if (StringUtils.isNotBlank(title)) {String titleNew = escapeQueryChars(title);params.append(" AND title:" + titleNew);if (StringUtils.isNotBlank(solrMMValueForTitleAndKeyword)) {query.set("mm", solrMMValueForTitleAndKeyword + "%");} else {query.set("mm", "67%");}}//關鍵詞if (StringUtils.isNotBlank(keyword)) {String keywordNew = escapeQueryChars(keyword);params.append(" AND sentences:" + keywordNew);if (StringUtils.isNotBlank(solrMMValueForTitleAndKeyword)) {query.set("mm", solrMMValueForTitleAndKeyword + "%");} else {query.set("mm", "67%");}}//根據句子if (StringUtils.isNotBlank(sentences)) {String sentencesNew = escapeQueryChars(sentences);params.append(" AND sentences:" + sentencesNew);if (StringUtils.isNotBlank(SolrMMValue)) {query.set("mm", SolrMMValue + "%");} else {query.set("mm", "30%");}}query.setQuery(params.toString());if (pageNum == null) {pageNum = 1;}if (pageSize == null) {pageSize = 1000;}//設置start、rows,分頁信息//通過當前頁面和煤業的條數去計算其實際記錄的下標query.setStart((pageNum - 1) * pageSize);query.setRows(pageSize);query.setHighlight(true); //開啟高亮組件query.addHighlightField("sentences"); //高亮字段query.addHighlightField("title"); //給標題也添加高亮query.setHighlightSimplePre("<font style=\"color:#006600;\">"); //標記query.setHighlightSimplePost("</font>");query.setHighlightSnippets(2); //結果分片數,默認為1query.setHighlightFragsize(300); //每個分片的最大長度,默認為100server.setSoTimeout(15000);server.setConnectionTimeout(1000);server.setDefaultMaxConnectionsPerHost(1000);server.setMaxTotalConnections(1000);QueryResponse response = server.query(query);SolrDocumentList list = response.getResults();//獲取文檔總數long count = list.getNumFound();//創建文檔心里列表List<SentenceInfo> sentenceInfos = new ArrayList<SentenceInfo>();//獲取高亮信息Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();ResultModel rm = new ResultModel();//總的相似度的值for(SolrDocument solrDocument : list) {SentenceInfo sentenceInfo = new SentenceInfo();sentenceInfo.setId(solrDocument.get("_id").toString());//sentenceInfo.setDocInfoId(solrDocument.get("docInfoId").toString());sentenceInfo.setDocLibrayId(solrDocument.get("docLibrayId").toString());sentenceInfo.setOriginalDocPath(solrDocument.get("originalDocPath").toString());sentenceInfo.setHtmlDocPath(solrDocument.get("htmlDocPath").toString());sentenceInfo.setOriginalFileName(solrDocument.get("originalFileName").toString());//sentenceInfo.setMajorId(solrDocument.get("majorId").toString());//sentenceInfo.setMajorName(solrDocument.get("majorName").toString());sentenceInfo.setPropertyId(solrDocument.get("propertyId").toString());sentenceInfo.setPropertyName(solrDocument.get("propertyName").toString());String titleValue = solrDocument.get("title").toString();sentenceInfo.setTitle(titleValue);//sentenceInfo.setKeyword(solrDocument.get("keyword").toString());sentenceInfo.setWordNum(Integer.parseInt(solrDocument.get("wordNum").toString()));sentenceInfo.setParagNum(Integer.parseInt(solrDocument.get("paragNum").toString()));sentenceInfo.setSentenceNum(Integer.parseInt(solrDocument.get("sentenceNum").toString()));sentenceInfo.setAuthor(solrDocument.get("author").toString());try {sentenceInfo.setInstitution(solrDocument.get("institution").toString());} catch (Exception e) {e.printStackTrace();}String sentencesValue = "";try {Object object = solrDocument.get("sentences");if (null != object) {sentencesValue = object.toString();} else {sentencesValue = null;}//sentencesValue = solrDocument.get("sentences").toString();} catch (Exception e) {e.printStackTrace();}List<String> list2 = highlighting.get(solrDocument.get("_id")).get("sentences");if (list2 != null) {sentencesValue = list2.get(0);}//設置文檔內容的信息sentenceInfo.setSentences(sentencesValue);String similarSentence = this.gainSimilarSentence(sentencesValue, "<font style=\"color:#006600;\">", "</font>");sentenceInfo.setSimilarSentence(similarSentence);//屬性分類名稱sentenceInfo.setPropertyName(propertyType);//通過下面的方式計算通過句子算出的各句的相似度值if (StringUtils.isNotBlank(sentences)) {//設置相似度的值double similarityValue = this.gianSimilarityValue(sentences, sentencesValue);sentenceInfo.setSimilarityValue((float)similarityValue);//totalSimilarityValue += similarityValue;if (null != rm.getMostSimilarSentenceInfo()) {if (rm.getMostSimilarSentenceInfo().getSimilarityValue() < similarityValue) {rm.setMostSimilarSentenceInfo(sentenceInfo);rm.setTotalSimilarityValue((float) similarityValue);}} else {rm.setMostSimilarSentenceInfo(sentenceInfo);rm.setTotalSimilarityValue((float) similarityValue);}}//通過下面計算通過標題計算出來的標題相似度值if (StringUtils.isNotBlank(title)) {//設置相似度的值double similarityValue = this.gianSimilarityValue(title, titleValue);sentenceInfo.setSimilarityValue((float)similarityValue);//totalSimilarityValue += similarityValue;if (null != rm.getMostSimilarSentenceInfo()) {if (rm.getMostSimilarSentenceInfo().getSimilarityValue() < similarityValue) {rm.setMostSimilarSentenceInfo(sentenceInfo);rm.setTotalSimilarityValue((float) similarityValue);}} else {rm.setMostSimilarSentenceInfo(sentenceInfo);rm.setTotalSimilarityValue((float) similarityValue);}}//算出帶有關鍵字的所有的相似度值if (StringUtils.isNotBlank(keyword)) {//設置相似度的值double similarityValue = this.gianSimilarityValue(keyword, sentencesValue);sentenceInfo.setSimilarityValue((float)similarityValue);//totalSimilarityValue += similarityValue;if (null != rm.getMostSimilarSentenceInfo()) {if (rm.getMostSimilarSentenceInfo().getSimilarityValue() < similarityValue) {rm.setMostSimilarSentenceInfo(sentenceInfo);rm.setTotalSimilarityValue((float) similarityValue);}} else {rm.setMostSimilarSentenceInfo(sentenceInfo);rm.setTotalSimilarityValue((float) similarityValue);}}if (StringUtils.isBlank(sentences) && StringUtils.isBlank(title) && StringUtils.isBlank(keyword)) {rm.setMostSimilarSentenceInfo(sentenceInfo);rm.setTotalSimilarityValue((float) 0.0f);}//System.out.println(solrDocument.get("score"));sentenceInfos.add(sentenceInfo);}//設置句子列表rm.setSentenceInfoList(sentenceInfos); // if (null != sentenceInfos && !sentenceInfos.isEmpty()) { // rm.setTotalSimilarityValue((float) (totalSimilarityValue / sentenceInfos.size())); // } else { // rm.setTotalSimilarityValue(0.0f); // }//設置當前頁rm.setRecordCount(count);//設置總頁數 = 文檔總數 / pageSize,如果有余則加1int totalPages = (int) (count / pageSize);if (count % pageSize > 0) {totalPages++;}rm.setPageCount(totalPages);return rm;} catch (Exception e) {e.printStackTrace();}return null;}}DocInfoSolrController的代碼如下:
package cn.com.hbny.docdetection.solr.controller;import java.util.HashMap; import java.util.Map;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody;import cn.com.hbny.docdetection.controller.base.BaseController; import cn.com.hbny.docdetection.solr.DocInfoSolrService; import cn.com.hbny.docdetection.solr.po.ResultModel;/*** @brief 文檔檢查對應的接口* @attention * mongo-connector -m localhost:27017 --auto-commit-interval=0 -t http://localhost:8983/solr/docdetection -d solr_doc_manager* * 訪問方式:http://localhost:8080/docdetection/solr/docInfoSolrController/gainDocInfoBySolr.action* @author toto* @date 2017年4月4日* @note begin modify by 涂作權 2017年4月4日 原始創建*/ @Controller @RequestMapping(value = "/solr/docInfoSolrController",method = {RequestMethod.GET,RequestMethod.POST}) public class DocInfoSolrController extends BaseController {private static final Logger logger = Logger.getLogger(DocInfoSolrController.class);@Autowiredprivate DocInfoSolrService docInfoSolrService;/*** \brief 通過內容和其它條件進行搜索出所需要的內容* @param model * @param ns :命名空間* @param propertyId :屬性id* @param propertyType :屬性分類的名稱* @param majorId :專業id* @param title :標題名稱* @param keyword :關鍵字* @param sentences :句子的內容* @param pageNum :當前頁碼數* @param pageSize :每頁的大小* @param request * @param response* @return* @attention 訪問方式是:http://localhost:8080/docdetection/solr/docInfoSolrController/gainDocInfosByContentAndOtherCondition.action* @author toto* @date 2017年4月4日 * @note begin modify by 涂作權 2017年4月4日 原始創建*/@RequestMapping(value = "/gainDocInfosByContentAndOtherCondition")@ResponseBodypublic Map<String, Object> gainDocInfosByContentAndOtherCondition(Model model,Boolean isPreciseSearch,String ns, String propertyId,String propertyType,String majorId,String title,String keyword,String sentences,@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,@RequestParam(value = "paseSize",required = false,defaultValue = "2000000000") Integer pageSize,HttpServletRequest request,HttpServletResponse response) {Map<String, Object> resultMap = new HashMap<String, Object>();try {ResultModel resultModel = docInfoSolrService.findDocInfoBySolr(isPreciseSearch,ns, propertyId,propertyType,majorId,title, keyword, sentences, pageNum, pageSize);logger.info(resultModel.getSentenceInfoList());resultMap.put("resultModel", resultModel);} catch (Exception e) {e.printStackTrace();}return resultMap;} }總結
以上是生活随笔為你收集整理的Solr单集代码调用案例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一键借多久能到账
- 下一篇: 建行信用卡账单分期后能不能最低还款