Skip to content

Commit

Permalink
fix #2832 Auto-switch search operator to OR when hit count is below t…
Browse files Browse the repository at this point in the history
…hreshold
  • Loading branch information
marevol committed Jul 22, 2024
1 parent fd90007 commit bc55977
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/codelibs/fess/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ public class Constants extends CoreLibConstants {

public static final Pattern LUCENE_RANGE_FIELD_RESERVED_PATTERN = Pattern.compile("([!\\(\\){}\\[\\]\"~\\\\:\\p{Zs}]|(&&)|(\\|\\|))");

public static final String DEFAULT_QUERY_OPERATOR = "fess.DefaultQueryOperator";

public static final String SEARCH_LOG_ACCESS_TYPE = "searchLogAccessType";

public static final String SEARCH_LOG_ACCESS_TYPE_JSON = "json";
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/org/codelibs/fess/helper/SearchHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.codelibs.fess.mylasta.action.FessUserBean;
import org.codelibs.fess.mylasta.direction.FessConfig;
import org.codelibs.fess.query.QueryFieldConfig;
import org.codelibs.fess.rank.fusion.RankFusionProcessor;
import org.codelibs.fess.util.BooleanFunction;
import org.codelibs.fess.util.ComponentUtil;
import org.codelibs.fess.util.QueryResponseList;
Expand Down Expand Up @@ -160,7 +161,22 @@ public void search(final SearchRequestParams params, final SearchRenderData data

protected List<Map<String, Object>> searchInternal(final String query, final SearchRequestParams params,
final OptionalThing<FessUserBean> userBean) {
return ComponentUtil.getRankFusionProcessor().search(query, params, userBean);
final RankFusionProcessor rankFusionProcessor = ComponentUtil.getRankFusionProcessor();
final List<Map<String, Object>> documentItems = rankFusionProcessor.search(query, params, userBean);
if (documentItems instanceof QueryResponseList queryResponseList) {
final FessConfig fessConfig = ComponentUtil.getFessConfig();
if (queryResponseList.getAllRecordCount() <= fessConfig.getQueryOrsearchMinHitCountAsInteger()) {
return LaRequestUtil.getOptionalRequest().map(request -> {
request.setAttribute(Constants.DEFAULT_QUERY_OPERATOR, "OR");
if (logger.isDebugEnabled()) {
logger.debug("The number of hits is {}<={}. Searching again with OR operator.",
queryResponseList.getAllRecordCount(), fessConfig.getQueryOrsearchMinHitCountAsInteger());
}
return rankFusionProcessor.search(query, params, userBean);
}).orElse(queryResponseList);
}
}
return documentItems;
}

public long scrollSearch(final SearchRequestParams params, final BooleanFunction<Map<String, Object>> cursor,
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/org/codelibs/fess/mylasta/direction/FessConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,9 @@ 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. 0 */
String QUERY_ORSEARCH_MIN_HIT_COUNT = "query.orsearch.min.hit.count";

/** The key of the configuration. e.g. u0021u002Cu002Eu003Fu0589u061Fu06D4u0700u0701u0702u0964u104Au104Bu1362u1367u1368u166Eu1803u1809u203Cu203Du2047u2048u2049u3002uFE52uFE57uFF01uFF0EuFF1FuFF61 */
String QUERY_HIGHLIGHT_TERMINAL_CHARS = "query.highlight.terminal.chars";

Expand Down Expand Up @@ -4400,6 +4403,21 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
*/
boolean isQueryReplaceTermWithPrefixQuery();

/**
* Get the value for the key 'query.orsearch.min.hit.count'. <br>
* The value is, e.g. 0 <br>
* @return The value of found property. (NotNull: if not found, exception but basically no way)
*/
String getQueryOrsearchMinHitCount();

/**
* Get the value for the key 'query.orsearch.min.hit.count' as {@link Integer}. <br>
* The value is, e.g. 0 <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 getQueryOrsearchMinHitCountAsInteger();

/**
* Get the value for the key 'query.highlight.terminal.chars'. <br>
* The value is, e.g. u0021u002Cu002Eu003Fu0589u061Fu06D4u0700u0701u0702u0964u104Au104Bu1362u1367u1368u166Eu1803u1809u203Cu203Du2047u2048u2049u3002uFE52uFE57uFF01uFF0EuFF1FuFF61 <br>
Expand Down Expand Up @@ -9121,6 +9139,14 @@ public boolean isQueryReplaceTermWithPrefixQuery() {
return is(FessConfig.QUERY_REPLACE_TERM_WITH_PREFIX_QUERY);
}

public String getQueryOrsearchMinHitCount() {
return get(FessConfig.QUERY_ORSEARCH_MIN_HIT_COUNT);
}

public Integer getQueryOrsearchMinHitCountAsInteger() {
return getAsInteger(FessConfig.QUERY_ORSEARCH_MIN_HIT_COUNT);
}

public String getQueryHighlightTerminalChars() {
return get(FessConfig.QUERY_HIGHLIGHT_TERMINAL_CHARS);
}
Expand Down Expand Up @@ -11183,6 +11209,7 @@ protected java.util.Map<String, String> prepareGeneratedDefaultMap() {
defaultMap.put(FessConfig.QUERY_GEO_FIELDS, "location");
defaultMap.put(FessConfig.QUERY_BROWSER_LANG_PARAMETER_NAME, "browser_lang");
defaultMap.put(FessConfig.QUERY_REPLACE_TERM_WITH_PREFIX_QUERY, "true");
defaultMap.put(FessConfig.QUERY_ORSEARCH_MIN_HIT_COUNT, "0");
defaultMap.put(FessConfig.QUERY_HIGHLIGHT_TERMINAL_CHARS,
"u0021u002Cu002Eu003Fu0589u061Fu06D4u0700u0701u0702u0964u104Au104Bu1362u1367u1368u166Eu1803u1809u203Cu203Du2047u2048u2049u3002uFE52uFE57uFF01uFF0EuFF1FuFF61");
defaultMap.put(FessConfig.QUERY_HIGHLIGHT_FRAGMENT_SIZE, "60");
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/org/codelibs/fess/query/parser/QueryParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.lucene.search.TermQuery;
import org.codelibs.fess.Constants;
import org.codelibs.fess.exception.QueryParseException;
import org.lastaflute.web.util.LaRequestUtil;

public class QueryParser {

Expand Down Expand Up @@ -58,7 +59,15 @@ public Query parse(final String query) {
protected org.apache.lucene.queryparser.classic.QueryParser createQueryParser() {
final LuceneQueryParser parser = new LuceneQueryParser(defaultField, analyzer);
parser.setAllowLeadingWildcard(allowLeadingWildcard);
parser.setDefaultOperator(defaultOperator);
LaRequestUtil.getOptionalRequest().ifPresent(req -> {
if (req.getAttribute(Constants.DEFAULT_QUERY_OPERATOR) instanceof String op) {
parser.setDefaultOperator(Operator.valueOf(op));
} else {
parser.setDefaultOperator(defaultOperator);
}
}).orElse(() -> {
parser.setDefaultOperator(defaultOperator);
});
return parser;
}

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/fess_config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ query.track.total.hits=10000
query.geo.fields=location
query.browser.lang.parameter.name=browser_lang
query.replace.term.with.prefix.query=true
query.orsearch.min.hit.count=0
query.highlight.terminal.chars=u0021u002Cu002Eu003Fu0589u061Fu06D4u0700u0701u0702u0964u104Au104Bu1362u1367u1368u166Eu1803u1809u203Cu203Du2047u2048u2049u3002uFE52uFE57uFF01uFF0EuFF1FuFF61
query.highlight.fragment.size=60
query.highlight.number.of.fragments=2
Expand Down

0 comments on commit bc55977

Please sign in to comment.