高亮方法:
方法:
solrconf.xml下配置:
<requestHandler name="/browse" class="solr.SearchHandler" useParams="query,facets,velocity,browse">
<lst name="defaults">
<str name="echoParams">explicit</str>
<!-- Highlighting defaults -->
<str name="hl">on</str>
<str name="hl.fl">name content titleShow</str>
<str name="hl.preserveMulti">true</str>
<str name="hl.encoder">html</str>
<str name="hl.simple.pre"><b></str>
<str name="hl.simple.post"></b></str>
<str name="f.content.hl.snippets">3</str>
<str name="f.content.hl.fragsize">200</str>
<str name="f.content.hl.alternateField">content</str>
<str name="f.content.hl.maxAlternateFieldLength">750</str>
</lst>
</requestHandler>
在manageschema下配置:
<field name="name" type="text_general" multiValued="true" indexed="true" stored="true"/>
solrj查询方法:
public static void search() throws SolrServerException {
HttpSolrClient server = new HttpSolrClient(url);
SolrQuery query = new SolrQuery();
// 以下给两个字段开启了高亮,分别是name,description,
query.setQuery("multifunctfsfion");
query.setHighlight(true);
query.addHighlightField("name");
query.addHighlightField("titleShow");
// 以下两个方法主要是在高亮的关键字前后加上html代码
query.setHighlightSimplePre("<font color=\"red\">");
query.setHighlightSimplePost("</font>");
QueryResponse response = server.query(query);
Map<String, Map<String, List<String>>> map = response.getHighlighting();
SolrDocumentList docs = response.getResults();
System.out.println("文档个数:" + docs.getNumFound());
System.out.println("查询时间:" + response.getQTime());
for (SolrDocument doc : docs) {
System.out.println("id: " + doc.getFieldValue("id"));
System.out.println("name: " + doc.getFieldValue("name"));
System.out.println("titleShow: " + doc.getFieldValue("titleShow"));
System.out.println(map);
}
}