improve query handling

This commit is contained in:
Shinsuke Sugaya 2016-01-31 16:42:56 +09:00
parent ed1bd007e0
commit 24d6c85d35
5 changed files with 45 additions and 2 deletions

View file

@ -49,6 +49,7 @@ import org.codelibs.fess.entity.GeoInfo;
import org.codelibs.fess.entity.PingResponse;
import org.codelibs.fess.entity.QueryContext;
import org.codelibs.fess.exception.FessSystemException;
import org.codelibs.fess.exception.InvalidQueryException;
import org.codelibs.fess.exception.ResultOffsetExceededException;
import org.codelibs.fess.exception.SearchQueryException;
import org.codelibs.fess.helper.QueryHelper;
@ -121,6 +122,7 @@ import org.elasticsearch.action.search.ClearScrollResponse;
import org.elasticsearch.action.search.MultiSearchRequest;
import org.elasticsearch.action.search.MultiSearchRequestBuilder;
import org.elasticsearch.action.search.MultiSearchResponse;
import org.elasticsearch.action.search.SearchPhaseExecutionException;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
@ -162,6 +164,7 @@ import org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuil
import org.elasticsearch.search.aggregations.bucket.terms.Terms.Order;
import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
import org.elasticsearch.threadpool.ThreadPool;
import org.lastaflute.core.message.UserMessages;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -535,7 +538,12 @@ public class FessEsClient implements Client {
}
}
searchResponse = searchRequestBuilder.execute().actionGet();
try {
searchResponse = searchRequestBuilder.execute().actionGet();
} catch (SearchPhaseExecutionException e) {
throw new InvalidQueryException(messages -> messages.addErrorsInvalidQueryParseError(UserMessages.GLOBAL_PROPERTY_KEY),
"Invalid query: " + searchRequestBuilder, e);
}
}
final long execTime = System.currentTimeMillis() - startTime;

View file

@ -395,7 +395,9 @@ public class QueryHelper implements Serializable {
protected QueryBuilder convertTermQuery(final QueryContext context, final TermQuery termQuery) {
final String field = termQuery.getTerm().field();
final String text = termQuery.getTerm().text();
if (Constants.DEFAULT_FIELD.equals(field)) {
if (fessConfig.getQueryReplaceTermWithPrefixQueryAsBoolean() && text.length() > 1 && text.endsWith("*")) {
return convertPrefixQuery(context, new PrefixQuery(new Term(field, text.substring(0, text.length() - 1))));
} else if (Constants.DEFAULT_FIELD.equals(field)) {
context.addFieldLog(field, text);
context.addHighlightedQuery(text);
return buildDefaultQueryBuilder((f, b) -> QueryBuilders.matchPhraseQuery(f, text).boost(b));

View file

@ -302,6 +302,9 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
/** The key of the configuration. e.g. 1000 */
String QUERY_MAX_LENGTH = "query.max.length";
/** 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. 1.6 */
String QUERY_BOOST_TITLE = "query.boost.title";
@ -1392,6 +1395,20 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
*/
Integer getQueryMaxLengthAsInteger();
/**
* Get the value for the key 'query.replace.term.with.prefix.query'. <br>
* The value is, e.g. true <br>
* @return The value of found property. (NotNull: if not found, exception but basically no way)
*/
String getQueryReplaceTermWithPrefixQuery();
/**
* Is the property for the key 'query.replace.term.with.prefix.query' true? <br>
* The value is, e.g. true <br>
* @return The determination, true or false. (if not found, exception but basically no way)
*/
boolean isQueryReplaceTermWithPrefixQuery();
/**
* Get the value for the key 'query.boost.title'. <br>
* The value is, e.g. 1.6 <br>
@ -2734,6 +2751,14 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
return getAsInteger(FessConfig.QUERY_MAX_LENGTH);
}
public String getQueryReplaceTermWithPrefixQuery() {
return get(FessConfig.QUERY_REPLACE_TERM_WITH_PREFIX_QUERY);
}
public boolean isQueryReplaceTermWithPrefixQuery() {
return is(FessConfig.QUERY_REPLACE_TERM_WITH_PREFIX_QUERY);
}
public String getQueryBoostTitle() {
return get(FessConfig.QUERY_BOOST_TITLE);
}

View file

@ -367,4 +367,11 @@ public interface FessProp {
public default String[] getSuggestPopularWordExcludesAsArray() {
return StreamUtil.of(getSuggestPopularWordExcludes().split("\n")).filter(s -> StringUtil.isNotBlank(s)).toArray(n -> new String[n]);
}
String getQueryReplaceTermWithPrefixQuery();
public default boolean getQueryReplaceTermWithPrefixQueryAsBoolean() {
return Boolean.valueOf(getQueryReplaceTermWithPrefixQuery());
}
}

View file

@ -149,6 +149,7 @@ index.document.type=doc
# query
query.max.length=1000
query.replace.term.with.prefix.query=true
# boost
query.boost.title=1.6