Solr设置高亮

 1     @Test

 2     public void test5(){

 3         try {

 4             SolrQuery query=new SolrQuery("程序");

 5             query.setHighlight(true)

 6                 .setHighlightSimplePre("<span class='highlight'>")//设置开头

 7                 .setHighlightSimplePost("</span>") //设置结尾

 8                 .setStart(0) 

 9                 .setRows(10);//设置行数

10             //设置高亮的哪些区域

11             query.setParam("hl.fl", "news_title,news_content");

12             QueryResponse response=httpSolrServer.query(query);

13             SolrDocumentList list=response.getResults();

14             for(SolrDocument sd:list){

15                 String id=(String) sd.getFieldValue("id");

16                 if(response.getHighlighting().get(id)!=null){

17                     System.out.println(response.getHighlighting().get(id).get("news_title"));

18 

19                     System.out.println(response.getHighlighting().get(id).get("news_content"));

20                 }

21             }

22         } catch (SolrServerException e) {

23             e.printStackTrace();

24         }

 

你可能感兴趣的:(Solr)