fix #163 add useSolrHighlight parameter.
This commit is contained in:
parent
64f1ec2ee2
commit
9b0a0aab4c
3 changed files with 61 additions and 11 deletions
|
@ -44,6 +44,7 @@ import org.apache.commons.lang.StringUtils;
|
|||
import org.codelibs.core.CoreLibConstants;
|
||||
import org.codelibs.core.util.DynamicProperties;
|
||||
import org.codelibs.core.util.StringUtil;
|
||||
import org.seasar.framework.container.annotation.tiger.InitMethod;
|
||||
import org.seasar.framework.util.URLUtil;
|
||||
import org.seasar.robot.util.CharUtil;
|
||||
import org.seasar.struts.taglib.S2Functions;
|
||||
|
@ -75,9 +76,9 @@ public class ViewHelper implements Serializable {
|
|||
@Resource
|
||||
protected DynamicProperties crawlerProperties;
|
||||
|
||||
public int pcDescriptionLength = 200;
|
||||
public int descriptionLength = 200;
|
||||
|
||||
public int pcTitleLength = 50;
|
||||
public int titleLength = 50;
|
||||
|
||||
public boolean encodeUrlLink = false;
|
||||
|
||||
|
@ -85,6 +86,12 @@ public class ViewHelper implements Serializable {
|
|||
|
||||
public String[] highlightingFields = new String[] { "hl_content", "digest" };
|
||||
|
||||
public boolean useSolrHighlight = false;
|
||||
|
||||
public String solrHighlightTagPre = "<em>";
|
||||
|
||||
public String solrHighlightTagPost = "</em>";
|
||||
|
||||
public String highlightTagPre = "<em>";
|
||||
|
||||
public String highlightTagPost = "</em>";
|
||||
|
@ -103,6 +110,18 @@ public class ViewHelper implements Serializable {
|
|||
|
||||
public String cacheTemplateName = "cache";
|
||||
|
||||
private String escapedSolrHighlightPre = null;
|
||||
|
||||
private String escapedSolrHighlightPost = null;
|
||||
|
||||
@InitMethod
|
||||
public void init() {
|
||||
if (useSolrHighlight) {
|
||||
escapedSolrHighlightPre = S2Functions.h(solrHighlightTagPre);
|
||||
escapedSolrHighlightPost = S2Functions.h(solrHighlightTagPost);
|
||||
}
|
||||
}
|
||||
|
||||
private String getString(final Map<String, Object> doc, final String key) {
|
||||
final Object value = doc.get(key);
|
||||
if (value == null) {
|
||||
|
@ -112,7 +131,7 @@ public class ViewHelper implements Serializable {
|
|||
}
|
||||
|
||||
public String getContentTitle(final Map<String, Object> document) {
|
||||
final int size = pcTitleLength;
|
||||
final int size = titleLength;
|
||||
|
||||
String title;
|
||||
if (StringUtil.isNotBlank(getString(document, "title"))) {
|
||||
|
@ -127,22 +146,32 @@ public class ViewHelper implements Serializable {
|
|||
final HttpServletRequest request = RequestUtil.getRequest();
|
||||
final String[] queries = request == null ? StringUtil.EMPTY_STRINGS
|
||||
: (String[]) request.getAttribute(Constants.HIGHLIGHT_QUERIES);
|
||||
final int size = pcDescriptionLength;
|
||||
final int size = descriptionLength;
|
||||
|
||||
for (final String field : highlightingFields) {
|
||||
final String text = getString(document, field);
|
||||
if (StringUtil.isNotBlank(text)) {
|
||||
return highlight(S2Functions.h(StringUtils.abbreviate(
|
||||
removeEmTag(text), size)), queries);
|
||||
if (useSolrHighlight) {
|
||||
return escapeHighlight(text);
|
||||
} else {
|
||||
return highlight(S2Functions.h(StringUtils.abbreviate(
|
||||
removeSolrHighlightTag(text), size)), queries);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return StringUtil.EMPTY;
|
||||
}
|
||||
|
||||
protected String removeEmTag(final String str) {
|
||||
return str.replaceAll("<em>", StringUtil.EMPTY).replaceAll("</em>",
|
||||
StringUtil.EMPTY);
|
||||
protected String escapeHighlight(String text) {
|
||||
return S2Functions.h(text)
|
||||
.replaceAll(escapedSolrHighlightPre, solrHighlightTagPre)
|
||||
.replaceAll(escapedSolrHighlightPost, solrHighlightTagPost);
|
||||
}
|
||||
|
||||
protected String removeSolrHighlightTag(final String str) {
|
||||
return str.replaceAll(solrHighlightTagPre, StringUtil.EMPTY)
|
||||
.replaceAll(solrHighlightTagPost, StringUtil.EMPTY);
|
||||
}
|
||||
|
||||
protected String highlight(final String content, final String[] queries) {
|
||||
|
|
|
@ -198,8 +198,9 @@
|
|||
</initMethod>
|
||||
<!--
|
||||
<property name="encodeUrlLink">false</property>
|
||||
<property name="pcDescriptionLength">200</property>
|
||||
<property name="pcTitleLength">50</property>
|
||||
<property name="useSolrHighlight">false</property>
|
||||
<property name="descriptionLength">200</property>
|
||||
<property name="titleLength">50</property>
|
||||
<property name="urlLinkEncoding">"UTF-8"</property>
|
||||
<property name="highlightingFields">new String[] { "hl_content", "digest" }</property>
|
||||
<initMethod name="addInitFacetParam">
|
||||
|
|
|
@ -85,4 +85,24 @@ public class ViewHelperTest extends S2TestCase {
|
|||
viewHelper.replaceHighlightQueries(text, queries));
|
||||
}
|
||||
|
||||
public void test_escapeHighlight() {
|
||||
viewHelper = new ViewHelper();
|
||||
viewHelper.useSolrHighlight = true;
|
||||
viewHelper.init();
|
||||
|
||||
String text = "";
|
||||
assertEquals("", viewHelper.escapeHighlight(text));
|
||||
|
||||
text = "aaa";
|
||||
assertEquals("aaa", viewHelper.escapeHighlight(text));
|
||||
|
||||
text = "<em>aaa</em>";
|
||||
assertEquals("<em>aaa</em>", viewHelper.escapeHighlight(text));
|
||||
|
||||
text = "<em>aaa</em><b>bbb</b>";
|
||||
assertEquals("<em>aaa</em><b>bbb</b>",
|
||||
viewHelper.escapeHighlight(text));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue