首页 > 代码库 > Solr(五)Solr实现简单的类似百度搜索高亮功能-2代码
Solr(五)Solr实现简单的类似百度搜索高亮功能-2代码
Solr高亮
一 在配置文件中,配置支持Ik分词器的Field
修改文件:
cd /usr/local/tomcat/apache-tomcat-8.5.13/solr_home/Test/conf/
vi managed-schema
红色部分是支持Ik分词器高亮显示的Field
二 配置完成之后,重启Tomcat,编写代码进行测试,对于环境搭建本文不做过多赘述。
1 与Solr服务器连接获取数据的类SolrRepository
package com.sgcc.ge.solr;import java.io.IOException;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.Map;import org.apache.solr.client.solrj.SolrQuery;import org.apache.solr.client.solrj.SolrServerException;import org.apache.solr.client.solrj.impl.HttpSolrClient;import org.apache.solr.client.solrj.response.QueryResponse;import org.apache.solr.common.SolrDocument;import org.apache.solr.common.SolrDocumentList;import org.springframework.stereotype.Service;import com.sgcc.ge.article.bean.ArticleBean;@Service("solrRepository")public class SolrRepository { /** * * *<p>Description:Get data from solr core return Json</p> * * @author:SongJia * * @date: 2017-8-9下午7:56:04 * * @param tag * @return * @throws SolrServerException * @throws IOException */ public List<ArticleBean> getSolrDataJson(String tag) throws SolrServerException, IOException{ HttpSolrClient client = new HttpSolrClient("http://192.168.3.208:8080/solr/Test"); QueryResponse resp=null; SolrQuery query = new SolrQuery("art_title:" +"*"+tag+"*" + " or art_tag:" + "*"+tag+"*"+" or art_abstract:"+"*"+tag+"*"); query.setHighlight(true); query.addHighlightField("art_title"); query.addHighlightField("art_tag"); query.addHighlightField("art_abstract"); query.setHighlightSimplePre("<font color=\"red\">"); query.setHighlightSimplePost("</font>"); query.setHighlightFragsize(300); resp = client.query(query); SolrDocumentList solrDocumentList = resp.getResults(); //获取高亮的字段 Map<String, Map<String, List<String>>> highlighting = resp.getHighlighting(); List<ArticleBean> list =new ArrayList<ArticleBean>(); //List<Map> result = new ArrayList<Map>(); //取出数据并封装到Map里面 for (int i = 0; i < solrDocumentList.size(); i++) { SolrDocument sd = solrDocumentList.get(i); ArticleBean bean = new ArticleBean(); String art_title = (String) sd.getFieldValue("art_title"); String id = (String) sd.getFieldValue("id"); String art_tag = (String) sd.getFieldValue("art_tag"); String art_abstract = (String) sd.getFieldValue("art_abstract"); String art_time = (String) sd.getFieldValue("art_time"); bean.setArtTag(art_tag); bean.setArtTime(art_time); bean.setArtTitle(art_title); bean.setArtAbstract(art_abstract); Iterator<Map.Entry<String, Map<String, List<String>>>> it = highlighting.entrySet().iterator(); while (it.hasNext()) { Map.Entry<String, Map<String, List<String>>> entry = it.next(); String key = entry.getKey(); Map<String, List<String>> value = http://www.mamicode.com/entry.getValue();"art_abstract"||keyin.equals("art_abstract")){ bean.setArtAbstract(valuein.get(0)); }else if(keyin=="art_tag"||keyin.equals("art_tag")){ bean.setArtTag(valuein.get(0)); }else if(keyin=="art_title"||keyin.equals("art_title")){ bean.setArtTitle(valuein.get(0)); } } } } list.add(bean); } return list; }}
2 控制器类ArticleController
package com.sgcc.ge.article;import java.util.List;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import com.google.gson.Gson;import com.google.gson.reflect.TypeToken;import com.sgcc.ge.article.bean.ArticleBean;import com.sgcc.ge.article.bean.MessageBean;import com.sgcc.ge.solr.SolrRepository;@Controller@RequestMapping("/article") public class ArticleController { /** * * *<p>Description:在Solr中获取数据并转成Json</p> * * @author:SongJia * * @date: 2017-8-9下午7:54:23 * * @param parameter * @return */ @RequestMapping("/getSolrJson") @ResponseBody public String getJosnFromSolr(String parameter){ System.out.println(parameter); Gson gson = new Gson(); String json=""; SolrRepository queryResult = new SolrRepository(); MessageBean<ArticleBean> bean = new MessageBean<ArticleBean>(); try { List<ArticleBean> solrDataJson = queryResult.getSolrDataJson(parameter); bean.setCode("0"); bean.setCode("Success"); bean.setData(solrDataJson); json = gson.toJson(bean,new TypeToken<MessageBean<ArticleBean>>() { }.getType()); } catch (Exception e) { bean.setCode("1"); bean.setCode("Failed"); json = gson.toJson(bean,new TypeToken<MessageBean<ArticleBean>>() { }.getType()); } return json; } }
3 文章实体类ArticleBean
package com.sgcc.ge.article.bean;public class ArticleBean { private String artId; private String artTitle; private String artContent; private String artTag; private String artTime; private String artAbstract; public String getArtAbstract() { return artAbstract; } public void setArtAbstract(String artAbstract) { this.artAbstract = artAbstract; } public String getArtId() { return artId; } public void setArtId(String artId) { this.artId = artId; } public String getArtTitle() { return artTitle; } public void setArtTitle(String artTitle) { this.artTitle = artTitle; } public String getArtContent() { return artContent; } public void setArtContent(String artContent) { this.artContent = artContent; } public String getArtTag() { return artTag; } public void setArtTag(String artTag) { this.artTag = artTag; } public String getArtTime() { return artTime; } public void setArtTime(String artTime) { this.artTime = artTime; }}
4 返回给客户端格式化数据的消息类MessageBean
package com.sgcc.ge.article.bean;import java.util.List;public class MessageBean<T> { private String code; private String message; private List<T> data; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public List<T> getData() { return data; } public void setData(List<T> data) { this.data = http://www.mamicode.com/data;>
5 页面代码
<script type="text/javascript"> function getData(){ var parameter = $("#parameter").val(); $("#article").empty(); $.ajax({ type: "POST", data: {parameter:parameter}, url: "http://localhost:9000/ene/global_energy_run_analysis/rest/article/getSolrJson", success: function(data){ var html=""; var obj =eval(‘(‘+data+‘)‘); for(var i=0;i<obj.data.length;i++){ var artTitle=obj.data[i].artTitle; var artTag=obj.data[i].artTag; var artTime=obj.data[i].artTime; var artAbstract=obj.data[i].artAbstract; html+="<ul>文章标题:"+artTitle+"</ul><ul>文章标签:"+artTag+"</ul><ul>文章摘要:"+artAbstract+"</ul><ul>文章时间:"+artTime+"</ul>"; html+="<ul>********************************************************************</ul>"; } $("#article").append(html); }, error:function(msg){ } }); } </script>
6 页面代码
<body> <div style="text-align: center">---------------------------------------------------------------</div> <div style="text-align: center"> <input type="text" id="parameter" placeholder="请输入要查询内容"> <input type="button" onclick="getData()" value="http://www.mamicode.com/查询"> </div> <div style="text-align: center">---------------------------------------------------------------</div> <div id="article"></div></body>
7 程序运行结果
Solr(五)Solr实现简单的类似百度搜索高亮功能-2代码
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。