Solr高亮显示,通过高亮显示,来将重要的数据显示出来。高亮显示,在WEB的查询和检索中,会经常的用到,我们需要通过高亮的操作来显示关键字。操作的时候,将高亮的数据处理后,封装成对象后,再返回给调用者。
案例
通过SolrQuery 设定高亮的效果,设定高亮的效果可以是加红,或则是粗体等。
public static void highlight() throws Exception {
SolrServer server = new HttpSolrServer(SOLR_PATH);
SolrQuery solrQuery = new SolrQuery();
solrQuery.add("q", "content:关系");
// 开启高亮
solrQuery.setHighlight(true);
// 高亮显示的格式
solrQuery.setHighlightSimplePre("<font color='red'>");
solrQuery.setHighlightSimplePost("</font>");
// 我需要那几个字段进行高亮
solrQuery.setParam("hl.fl", "content");
QueryResponse response = server.query(solrQuery);
// 直接获取的反回结果
SolrDocumentList docs = response.getResults();
for (SolrDocument doc : docs) {
String author = doc.get("author").toString();
String content = doc.get("content").toString();
System.out.println(author);
System.out.println(content);
}
System.out.println("-------------------高亮显示效果---------------------------");
// 高亮显示的反回结果
Map<String, Map<String, List<String>>> maplist = response.getHighlighting();
// 返回高亮之后的结果..
for (SolrDocument solrDocument : docs) {
Object id = solrDocument.get("id");
Map<String, List<String>> fieldMap = maplist.get(id);
List<String> stringlist = fieldMap.get("content");
System.out.println(stringlist);
}
}
运行结果
完整代码
package com.yellowcong.day12_05;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
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.apache.solr.common.SolrInputDocument;
/**
*
* 作者:yellowcong <br/>
* 日期:2017/12/05 <br/>
* 時間:14:33:28 <br/>
* 描述:
*/
public class Demo {
// solr的服务器地址
private static final String SOLR_PATH = "http://192.168.66.100:8080/solr";
public static void main(String[] args) throws Exception {
//删除所有数据
deleteAll();
//添加索引
index();
//查询数据
highlight();
}
/**
* 作者:yellowcong <br/>
* 日期:2017/12/05 <br/>
* 時間:14:58:31 <br/>
* 描述:添加索引
* @throws Exception
* @throws SolrServerException
*/
public static void index() throws SolrServerException, Exception {
SolrServer server = new HttpSolrServer(SOLR_PATH);
SolrInputDocument doc1 = new SolrInputDocument();
doc1.addField("id", "1");
doc1.addField("author", "张三");
doc1.addField("title", "Java");
doc1.addField("content", "Java是xx");
doc1.addField("add_date", new Date());
SolrInputDocument doc2 = new SolrInputDocument();
doc2.addField("id", "2");
doc2.addField("author", "李四");
doc2.addField("title", "Oralce");
doc2.addField("content", "关系型数据库");
doc2.addField("add_date", new Date());
SolrInputDocument doc3 = new SolrInputDocument();
doc3.addField("id", "3");
doc3.addField("author", "王武");
doc3.addField("title", "Mysql");
doc3.addField("content", "老子也是关系型数据库");
doc3.addField("add_date", new Date());
SolrInputDocument doc4 = new SolrInputDocument();
doc4.addField("id", "4");
doc4.addField("author", "赵六");
doc4.addField("title", "Redis");
doc4.addField("content", "Nosql数据库");
doc4.addField("add_date", new Date());
SolrInputDocument doc5 = new SolrInputDocument();
doc5.addField("id", "5");
doc5.addField("author", "张三");
doc5.addField("title", "Mongodb");
doc5.addField("content", "Nosql数据库");
doc5.addField("add_date", new Date());
//添加文档
// 集合添加多个的方式
List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
docs.add(doc1);
docs.add(doc2);
docs.add(doc3);
docs.add(doc4);
docs.add(doc5);
//添加数据到服务器
server.add(docs);
//提交事务
server.commit();
System.out.println("-------------------添加索引成功--------------------");
}
/**
* 作者:yellowcong <br/>
* 日期:2017/12/05 <br/>
* 時間:14:48:59 <br/>
* 描述:清空Solr里面的所有数据
*/
public static void deleteAll() throws Exception {
SolrServer server = new HttpSolrServer(SOLR_PATH);
// 清空数据
server.deleteByQuery("*:*");
server.commit();
}
/**
*
* 作者:yellowcong <br/>
* 日期:2017/12/05 <br/>
* 時間:14:47:27 <br/>
* 描述:高亮显示
*/
public static void highlight() throws Exception {
SolrServer server = new HttpSolrServer(SOLR_PATH);
SolrQuery solrQuery = new SolrQuery();
solrQuery.add("q", "content:关系");
// 开启高亮
solrQuery.setHighlight(true);
// 高亮显示的格式
solrQuery.setHighlightSimplePre("<font color='red'>");
solrQuery.setHighlightSimplePost("</font>");
// 我需要那几个字段进行高亮
solrQuery.setParam("hl.fl", "content");
QueryResponse response = server.query(solrQuery);
// 直接获取的反回结果
SolrDocumentList docs = response.getResults();
for (SolrDocument doc : docs) {
String author = doc.get("author").toString();
String content = doc.get("content").toString();
System.out.println(author);
System.out.println(content);
}
System.out.println("-------------------高亮显示效果---------------------------");
// 高亮显示的反回结果
Map<String, Map<String, List<String>>> maplist = response.getHighlighting();
// 返回高亮之后的结果..
for (SolrDocument solrDocument : docs) {
Object id = solrDocument.get("id");
Map<String, List<String>> fieldMap = maplist.get(id);
List<String> stringlist = fieldMap.get("content");
System.out.println(stringlist);
}
}
}