赞
踩
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 {
/**
*
*
*
Description:Get data from solr core return Json
*
* @author:SongJia
*
* @date: 2017-8-9下午7:56:04
*
* @param tag
* @return
* @throws SolrServerException
* @throws IOException
*/
public List 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("");
query.setHighlightSimplePost("");
query.setHighlightFragsize(300);
resp = client.query(query);
SolrDocumentList solrDocumentList = resp.getResults();
//获取高亮的字段
Map>> highlighting = resp.getHighlighting();
List list =new ArrayList();
//List result = new ArrayList();
//取出数据并封装到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>>> it = highlighting.entrySet().iterator();
while (it.hasNext()) {
Map.Entry>> entry = it.next();
String key = entry.getKey();
Map> value = entry.getValue();
if(key==id||key.equals(id)){
for (Map.Entry> inentry : value.entrySet()) {
String keyin = inentry.getKey();
List valuein = inentry.getValue();
if(keyin=="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 {
/**
*
*
*
Description:在Solr中获取数据并转成Json
*
* @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 bean = new MessageBean();
try {
List solrDataJson = queryResult.getSolrDataJson(parameter);
bean.setCode("0");
bean.setCode("Success");
bean.setData(solrDataJson);
json = gson.toJson(bean,new TypeToken>() { }.getType());
} catch (Exception e) {
bean.setCode("1");
bean.setCode("Failed");
json = gson.toJson(bean,new TypeToken>() { }.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 {
private String code;
private String message;
private List 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 getData() {
return data;
}
public void setData(List data) {
this.data = data;
}
}
5 页面代码
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
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+="
html+="
}
$("#article").append(html);
},
error:function(msg){
}
});
}
6 页面代码
7 程序运行结果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。