fix #795 use highlight tags

This commit is contained in:
Shinsuke Sugaya 2016-11-23 22:49:02 +09:00
parent 837d3b7bf0
commit 8805206256
6 changed files with 76 additions and 69 deletions

View file

@ -942,8 +942,9 @@ public class FessEsClient implements Client {
}
final QueryHelper queryHelper = ComponentUtil.getQueryHelper();
final FessConfig fessConfig = ComponentUtil.getFessConfig();
if (offset > queryHelper.getMaxSearchResultOffset()) {
if (offset > fessConfig.getQueryMaxSearchResultOffsetAsInteger()) {
throw new ResultOffsetExceededException("The number of result size is exceeded.");
}
@ -970,7 +971,7 @@ public class FessEsClient implements Client {
// highlighting
queryHelper.highlightedFields(stream -> stream.forEach(hf -> searchRequestBuilder.addHighlightedField(hf,
queryHelper.getHighlightFragmentSize())));
fessConfig.getQueryHighlightFragmentSizeAsInteger())));
// facets
if (facetInfo != null) {

View file

@ -110,7 +110,8 @@ public class QueryHelper {
protected String[] sortFields;
protected int highlightFragmentSize = 100;
@Deprecated
public int highlightFragmentSize = 100;
protected String additionalQuery;
@ -120,7 +121,8 @@ public class QueryHelper {
protected Map<String, String[]> requestParameterMap = new HashMap<>();
protected int maxSearchResultOffset = 100000;
@Deprecated
public int maxSearchResultOffset = 100000;
protected SortBuilder[] defaultSortBuilders;
@ -682,7 +684,6 @@ public class QueryHelper {
public void highlightedFields(final Consumer<Stream<String>> stream) {
stream(highlightedFields).of(stream);
;
}
/**
@ -745,20 +746,6 @@ public class QueryHelper {
highlightFieldSet.add(field);
}
/**
* @return the highlightFragmentSize
*/
public int getHighlightFragmentSize() {
return highlightFragmentSize;
}
/**
* @param highlightFragmentSize the highlightFragmentSize to set
*/
public void setHighlightFragmentSize(final int highlightFragmentSize) {
this.highlightFragmentSize = highlightFragmentSize;
}
/**
* @return the additionalQuery
*/
@ -801,14 +788,6 @@ public class QueryHelper {
return requestParameterMap.entrySet();
}
public int getMaxSearchResultOffset() {
return maxSearchResultOffset;
}
public void setMaxSearchResultOffset(final int maxSearchResultOffset) {
this.maxSearchResultOffset = maxSearchResultOffset;
}
public void addDefaultSort(final String fieldName, final String order) {
final List<SortBuilder> list = new ArrayList<>();
if (defaultSortBuilders != null) {

View file

@ -25,11 +25,9 @@ import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -99,6 +97,7 @@ public class ViewHelper {
@Resource
protected UserAgentHelper userAgentHelper;
@Deprecated
public int descriptionLength = 200;
public int titleLength = 50;
@ -111,6 +110,7 @@ public class ViewHelper {
public String[] highlightedFields = new String[] { "hl_content", "digest" };
@Deprecated
public boolean useHighlight = false;
public String originalHighlightTagPre = "<em>";
@ -139,10 +139,8 @@ public class ViewHelper {
@PostConstruct
public void init() {
if (useHighlight) {
escapedHighlightPre = LaFunctions.h(originalHighlightTagPre);
escapedHighlightPost = LaFunctions.h(originalHighlightTagPost);
}
escapedHighlightPre = LaFunctions.h(originalHighlightTagPre);
escapedHighlightPost = LaFunctions.h(originalHighlightTagPost);
}
public String getContentTitle(final Map<String, Object> document) {
@ -159,24 +157,10 @@ public class ViewHelper {
}
public String getContentDescription(final Map<String, Object> document) {
final Set<String> queries = new HashSet<>();
LaRequestUtil.getOptionalRequest().ifPresent(request -> {
@SuppressWarnings("unchecked")
final Set<String> set = (Set<String>) request.getAttribute(Constants.HIGHLIGHT_QUERIES);
if (set != null) {
queries.addAll(set);
}
});
final int size = descriptionLength;
for (final String field : highlightedFields) {
final String text = DocumentUtil.getValue(document, field, String.class);
if (StringUtil.isNotBlank(text)) {
if (useHighlight) {
return escapeHighlight(text);
} else {
return highlight(LaFunctions.h(StringUtils.abbreviate(removeHighlightTag(text), size)), queries);
}
return escapeHighlight(text);
}
}
@ -184,28 +168,13 @@ public class ViewHelper {
}
protected String escapeHighlight(final String text) {
return LaFunctions.h(text).replaceAll(escapedHighlightPre, originalHighlightTagPre)
.replaceAll(escapedHighlightPost, originalHighlightTagPost);
return LaFunctions.h(text).replaceAll(escapedHighlightPre, highlightTagPre).replaceAll(escapedHighlightPost, highlightTagPost);
}
protected String removeHighlightTag(final String str) {
return str.replaceAll(originalHighlightTagPre, StringUtil.EMPTY).replaceAll(originalHighlightTagPost, StringUtil.EMPTY);
}
@Deprecated
protected String highlight(final String content, final Set<String> queries) {
if (StringUtil.isBlank(content) || queries.isEmpty()) {
return content;
}
String newContent = content;
for (final String query : queries) {
newContent =
Pattern.compile(Pattern.quote(query), Pattern.CASE_INSENSITIVE).matcher(newContent)
.replaceAll(highlightTagPre + query + highlightTagPost);
}
return newContent;
}
public String getUrlLink(final Map<String, Object> document) {
// file protocol
String url = DocumentUtil.getValue(document, "url", String.class);

View file

@ -51,6 +51,7 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
-XX:+UseParNewGC
-XX:+UseTLAB
-XX:+DisableExplicitGC
-XX:+HeapDumpOnOutOfMemoryError
-XX:-OmitStackTraceInFastThrow
-Djcifs.smb.client.connTimeout=60000
-Djcifs.smb.client.soTimeout=35000
@ -70,6 +71,7 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
-XX:+UseParNewGC
-XX:+UseTLAB
-XX:+DisableExplicitGC
-XX:+HeapDumpOnOutOfMemoryError
-Dgroovy.use.classvalue=true
*/
String JVM_SUGGEST_OPTIONS = "jvm.suggest.options";
@ -427,6 +429,12 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
/** The key of the configuration. e.g. true */
String QUERY_REPLACE_TERM_WITH_PREFIX_QUERY = "query.replace.term.with.prefix.query";
/** The key of the configuration. e.g. 100 */
String QUERY_HIGHLIGHT_FRAGMENT_SIZE = "query.highlight.fragment.size";
/** The key of the configuration. e.g. 100000 */
String QUERY_MAX_SEARCH_RESULT_OFFSET = "query.max.search.result.offset";
/** The key of the configuration. e.g. */
String QUERY_ADDITIONAL_RESPONSE_FIELDS = "query.additional.response.fields";
@ -1137,6 +1145,7 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
-XX:+UseParNewGC
-XX:+UseTLAB
-XX:+DisableExplicitGC
-XX:+HeapDumpOnOutOfMemoryError
-XX:-OmitStackTraceInFastThrow
-Djcifs.smb.client.connTimeout=60000
-Djcifs.smb.client.soTimeout=35000
@ -1161,6 +1170,7 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
-XX:+UseParNewGC
-XX:+UseTLAB
-XX:+DisableExplicitGC
-XX:+HeapDumpOnOutOfMemoryError
-Dgroovy.use.classvalue=true
<br>
* @return The value of found property. (NotNull: if not found, exception but basically no way)
@ -2286,6 +2296,36 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
*/
boolean isQueryReplaceTermWithPrefixQuery();
/**
* Get the value for the key 'query.highlight.fragment.size'. <br>
* The value is, e.g. 100 <br>
* @return The value of found property. (NotNull: if not found, exception but basically no way)
*/
String getQueryHighlightFragmentSize();
/**
* Get the value for the key 'query.highlight.fragment.size' as {@link Integer}. <br>
* The value is, e.g. 100 <br>
* @return The value of found property. (NotNull: if not found, exception but basically no way)
* @throws NumberFormatException When the property is not integer.
*/
Integer getQueryHighlightFragmentSizeAsInteger();
/**
* Get the value for the key 'query.max.search.result.offset'. <br>
* The value is, e.g. 100000 <br>
* @return The value of found property. (NotNull: if not found, exception but basically no way)
*/
String getQueryMaxSearchResultOffset();
/**
* Get the value for the key 'query.max.search.result.offset' as {@link Integer}. <br>
* The value is, e.g. 100000 <br>
* @return The value of found property. (NotNull: if not found, exception but basically no way)
* @throws NumberFormatException When the property is not integer.
*/
Integer getQueryMaxSearchResultOffsetAsInteger();
/**
* Get the value for the key 'query.additional.response.fields'. <br>
* The value is, e.g. <br>
@ -5012,6 +5052,22 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
return is(FessConfig.QUERY_REPLACE_TERM_WITH_PREFIX_QUERY);
}
public String getQueryHighlightFragmentSize() {
return get(FessConfig.QUERY_HIGHLIGHT_FRAGMENT_SIZE);
}
public Integer getQueryHighlightFragmentSizeAsInteger() {
return getAsInteger(FessConfig.QUERY_HIGHLIGHT_FRAGMENT_SIZE);
}
public String getQueryMaxSearchResultOffset() {
return get(FessConfig.QUERY_MAX_SEARCH_RESULT_OFFSET);
}
public Integer getQueryMaxSearchResultOffsetAsInteger() {
return getAsInteger(FessConfig.QUERY_MAX_SEARCH_RESULT_OFFSET);
}
public String getQueryAdditionalResponseFields() {
return get(FessConfig.QUERY_ADDITIONAL_RESPONSE_FIELDS);
}

View file

@ -203,6 +203,8 @@ index.indices.timeout=1m
query.max.length=1000
query.geo.fields=location
query.replace.term.with.prefix.query=true
query.highlight.fragment.size=100
query.max.search.result.offset=100000
query.additional.response.fields=
query.additional.api.response.fields=
query.additional.cache.response.fields=

View file

@ -83,7 +83,6 @@ public class ViewHelperTest extends UnitFessTestCase {
public void test_escapeHighlight() {
viewHelper = new ViewHelper();
viewHelper.useHighlight = true;
viewHelper.init();
String text = "";
@ -92,11 +91,12 @@ public class ViewHelperTest extends UnitFessTestCase {
text = "aaa";
assertEquals("aaa", viewHelper.escapeHighlight(text));
text = "<em>aaa</em>";
assertEquals("<em>aaa</em>", viewHelper.escapeHighlight(text));
text = viewHelper.originalHighlightTagPre + "aaa" + viewHelper.originalHighlightTagPost;
assertEquals(viewHelper.highlightTagPre + "aaa" + viewHelper.highlightTagPost, viewHelper.escapeHighlight(text));
text = "<em>aaa</em><b>bbb</b>";
assertEquals("<em>aaa</em>&lt;b&gt;bbb&lt;/b&gt;", viewHelper.escapeHighlight(text));
text = viewHelper.originalHighlightTagPre + "aaa" + viewHelper.originalHighlightTagPost + "<b>bbb</b>";
assertEquals(viewHelper.highlightTagPre + "aaa" + viewHelper.highlightTagPost + "&lt;b&gt;bbb&lt;/b&gt;",
viewHelper.escapeHighlight(text));
}