前面介绍了《给Compass搜索添加高亮(highlight)》,不过就是有点问题。索引的内容很多都是html格式的,比如myblog里文章的内容,highlight出来的内容是包括了html代码,所以直接输出highlight的内容的话一些没有关闭的标签,不匹配的标签都会把你的页面撑乱,或者变成其他样式了。
我们就来处理一下这个问题,我选择的方法是highlight之后把highlight的内容先替换成特殊的内容,然后去掉html代码,然后在将高亮内容替换回去。
我包装了一下原来的CompassHit:
/**
* @author <a href="mailto:rory.cn@gmail.com">somebody</a>
* @since Sep 1, 2007 10:20:49 PM
* @version $Id CompassHitWapper.java$
*/
public class CompassHitWapper {
private CompassHit compassHit;
public CompassHitWapper(CompassHit compassHit) {
this.compassHit = compassHit;
}
public String getAlias() {
return compassHit.getAlias();
}
public Object getData() {
return compassHit.getData();
}
public Resource getResource() {
return compassHit.getResource();
}
public float getScore() {
return compassHit.getScore();
}
public String getHighlightedText(String propertyName) {
String text = "";
if (compassHit.getHighlightedText()!=null) {
text = compassHit.getHighlightedText().getHighlightedText(propertyName);
}
if (StringUtils.isBlank(text)) {
return "";
}
text = text.replaceAll("<span class=\"highlight\">([^<>]+)</span>", "_@$1@_");
text = MyblogUtil.removeHTML(text);
text = TextUtil.escapeHTML(text);
return text.replaceAll("_@([^<>@]+)@_", "<span class=\"highlight\">$1</span>");
}
}
这里先拿到高亮的内容然后替换成 _@xx@_ ,去掉html,转换html,然后替换回来。这样就ok了。这里还包装了一下原来的CompassSearchResults。
/**
* @author <a href="mailto:rory.cn@gmail.com">somebody</a>
* @since Sep 2, 2007 10:24:34 AM
* @version $Id CompassSearchResultsWrapper.java$
*/
public class CompassSearchResultsWrapper {
private CompassSearchResults searchResults;
private List<CompassHitWapper> hits;
public CompassSearchResultsWrapper(CompassSearchResults searchResults) {
this.searchResults = searchResults;
}
public List<CompassHitWapper> getHits() {
return hits;
}
public void setHits(List<CompassHitWapper> hits) {
this.hits = hits;
}
public long getSearchTime() {
return this.searchResults.getSearchTime();
}
public int getTotalHits() {
return this.searchResults.getTotalHits();
}
public Page[] getPages() {
return this.searchResults.getPages();
}
}
修改原来的searchController:
/**
* @author <a href="mailto:rory.cn@gmail.com">somebody</a>
* @since Sep 2, 2007 10:24:34 AM
* @version $Id CompassSearchResultsWrapper.java$
*/
public class CompassSearchResultsWrapper {
private CompassSearchResults searchResults;
private List<CompassHitWapper> hits;
public CompassSearchResultsWrapper(CompassSearchResults searchResults) {
this.searchResults = searchResults;
}
public List<CompassHitWapper> getHits() {
return hits;
}
public void setHits(List<CompassHitWapper> hits) {
this.hits = hits;
}
public long getSearchTime() {
return this.searchResults.getSearchTime();
}
public int getTotalHits() {
return this.searchResults.getTotalHits();
}
public Page[] getPages() {
return this.searchResults.getPages();
}
}
这样就ok了,最终效果请查看本站搜索,http://jdkcn.com/search/java http://jdkcn.com/search.html?query=java+AND+spring
完整源代码请参考myblog1.5 http://myblog.googlecode.com/svn/branches/myblog-1.5/
Like others





Friday, September 07, 2007, 11:47:06 AM (CST)
java写的blog?牛!
把你加进link了,嘿嘿
Friday, September 07, 2007, 12:06:51 PM (CST)
今天才去你那里看了,好酷的站,你的连接也加好了。
Saturday, September 22, 2007, 10:06:58 AM (CST)
给你来个直接替换的:
@Test
public void testHighlighBrownAndColor() {
String s = "<span style='color:brown;'>"
+ "The quick brown fox jumps over the lazy dog, another brown fox. "
+ "Hmm... brown color.</span>";
String s1 = s.replaceAll("((brown|color)(?![^<]*>))",
"<span style='color:red;'>$1</span>");
assertEquals(
"<span style='color:brown;'>"
+ "The quick <span style='color:red;'>brown</span> "
+ "fox jumps over the lazy dog, "
+ "another <span style='color:red;'>brown</span> fox. "
+ "Hmm... <span style='color:red;'>brown</span> <span style='color:red;'>color</span>.</span>",
s1);
}