培森的Blog (五) Solr实现简单的类似百度搜索高亮功能-2代码

(五) Solr实现简单的类似百度搜索高亮功能-2代码

Solr高亮 一 在配置文件中,配置支持Ik分词器的Field 修改文件: 1 cd /usr/local/t…

Solr高亮

一 在配置文件中,配置支持Ik分词器的Field

修改文件:

1
cd /usr/local/tomcat/apache-tomcat-8.5.13/solr_home/Test/conf/<br>vi managed-schema

红色部分是支持Ik分词器高亮显示的Field

 

二 配置完成之后,重启Tomcat,编写代码进行测试,对于环境搭建本文不做过多赘述。

1 与Solr服务器连接获取数据的类SolrRepository

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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 = entry.getValue();
                if(key==id||key.equals(id)){
                    for (Map.Entry<String, List<String>> inentry : value.entrySet()) {
                        String keyin = inentry.getKey();
                        List<String> 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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 = data;
    }
     
}

5 页面代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<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 页面代码

1
2
3
4
5
6
7
8
9
<body>
    <div style="text-align: center">---------------------------------------------------------------</div>
    <div style="text-align: center">
        <input type="text" id="parameter" placeholder="请输入要查询内容">
        <input type="button" onclick="getData()" value="查询">
    </div>
    <div style="text-align: center">---------------------------------------------------------------</div>
    <div id="article"></div>
</body>

7 程序运行结果

本文来自网络,不代表培森的Blog立场,转载请注明出处:https://blog.xupeisen.com/archives/52

作者: 培森

联系我们

联系我们

13262951234

在线咨询: QQ交谈

邮箱: admin@xupeisen.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部