#2637 query module refactoring
This commit is contained in:
parent
7d19b44aa2
commit
3be34b0dae
11 changed files with 160 additions and 63 deletions
|
@ -15,6 +15,8 @@
|
|||
*/
|
||||
package org.codelibs.fess.query;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.lucene.search.BooleanClause;
|
||||
import org.apache.lucene.search.BooleanQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
|
@ -26,6 +28,7 @@ import org.opensearch.index.query.QueryBuilder;
|
|||
import org.opensearch.index.query.QueryBuilders;
|
||||
|
||||
public class BooleanQueryCommand extends QueryCommand {
|
||||
private static final Logger logger = LogManager.getLogger(BooleanQueryCommand.class);
|
||||
|
||||
@Override
|
||||
protected String getQueryClassName() {
|
||||
|
@ -35,6 +38,9 @@ public class BooleanQueryCommand extends QueryCommand {
|
|||
@Override
|
||||
public QueryBuilder execute(final QueryContext context, final Query query, final float boost) {
|
||||
if (query instanceof final BooleanQuery booleanQuery) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("{}:{}", query, boost);
|
||||
}
|
||||
return convertBooleanQuery(context, booleanQuery, boost);
|
||||
}
|
||||
throw new InvalidQueryException(messages -> messages.addErrorsInvalidQueryUnknown(UserMessages.GLOBAL_PROPERTY_KEY),
|
||||
|
|
|
@ -15,6 +15,8 @@
|
|||
*/
|
||||
package org.codelibs.fess.query;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.lucene.search.BoostQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.codelibs.fess.entity.QueryContext;
|
||||
|
@ -23,6 +25,7 @@ import org.lastaflute.core.message.UserMessages;
|
|||
import org.opensearch.index.query.QueryBuilder;
|
||||
|
||||
public class BoostQueryCommand extends QueryCommand {
|
||||
private static final Logger logger = LogManager.getLogger(BoostQueryCommand.class);
|
||||
|
||||
@Override
|
||||
protected String getQueryClassName() {
|
||||
|
@ -32,6 +35,9 @@ public class BoostQueryCommand extends QueryCommand {
|
|||
@Override
|
||||
public QueryBuilder execute(final QueryContext context, final Query query, final float boost) {
|
||||
if (query instanceof final BoostQuery boostQuery) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("{}:{}", query, boost);
|
||||
}
|
||||
return getQueryProcessor().execute(context, boostQuery.getQuery(), boostQuery.getBoost());
|
||||
}
|
||||
throw new InvalidQueryException(messages -> messages.addErrorsInvalidQueryUnknown(UserMessages.GLOBAL_PROPERTY_KEY),
|
||||
|
|
|
@ -15,6 +15,8 @@
|
|||
*/
|
||||
package org.codelibs.fess.query;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.search.FuzzyQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
|
@ -29,6 +31,7 @@ import org.opensearch.index.query.QueryBuilder;
|
|||
import org.opensearch.index.query.QueryBuilders;
|
||||
|
||||
public class FuzzyQueryCommand extends QueryCommand {
|
||||
private static final Logger logger = LogManager.getLogger(FuzzyQueryCommand.class);
|
||||
|
||||
@Override
|
||||
protected String getQueryClassName() {
|
||||
|
@ -38,6 +41,9 @@ public class FuzzyQueryCommand extends QueryCommand {
|
|||
@Override
|
||||
public QueryBuilder execute(final QueryContext context, final Query query, final float boost) {
|
||||
if (query instanceof final FuzzyQuery fuzzyQuery) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("{}:{}", query, boost);
|
||||
}
|
||||
return convertFuzzyQuery(context, fuzzyQuery, boost);
|
||||
}
|
||||
throw new InvalidQueryException(messages -> messages.addErrorsInvalidQueryUnknown(UserMessages.GLOBAL_PROPERTY_KEY),
|
||||
|
@ -47,7 +53,7 @@ public class FuzzyQueryCommand extends QueryCommand {
|
|||
protected QueryBuilder convertFuzzyQuery(final QueryContext context, final FuzzyQuery fuzzyQuery, final float boost) {
|
||||
final FessConfig fessConfig = ComponentUtil.getFessConfig();
|
||||
final Term term = fuzzyQuery.getTerm();
|
||||
final String field = getSearchField(context, term.field());
|
||||
final String field = getSearchField(context.getDefaultField(), term.field());
|
||||
// TODO fuzzy value
|
||||
if (Constants.DEFAULT_FIELD.equals(field)) {
|
||||
context.addFieldLog(field, term.text());
|
||||
|
|
|
@ -15,6 +15,8 @@
|
|||
*/
|
||||
package org.codelibs.fess.query;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.lucene.search.MatchAllDocsQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.codelibs.fess.entity.QueryContext;
|
||||
|
@ -22,6 +24,7 @@ import org.opensearch.index.query.QueryBuilder;
|
|||
import org.opensearch.index.query.QueryBuilders;
|
||||
|
||||
public class MatchAllQueryCommand extends QueryCommand {
|
||||
private static final Logger logger = LogManager.getLogger(MatchAllQueryCommand.class);
|
||||
|
||||
@Override
|
||||
protected String getQueryClassName() {
|
||||
|
@ -30,6 +33,9 @@ public class MatchAllQueryCommand extends QueryCommand {
|
|||
|
||||
@Override
|
||||
public QueryBuilder execute(final QueryContext context, final Query query, final float boost) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("{}:{}", query, boost);
|
||||
}
|
||||
return QueryBuilders.matchAllQuery();
|
||||
}
|
||||
|
||||
|
|
|
@ -17,6 +17,8 @@ package org.codelibs.fess.query;
|
|||
|
||||
import static org.codelibs.core.stream.StreamUtil.stream;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.search.PhraseQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
|
@ -26,6 +28,7 @@ import org.lastaflute.core.message.UserMessages;
|
|||
import org.opensearch.index.query.QueryBuilder;
|
||||
|
||||
public class PhraseQueryCommand extends QueryCommand {
|
||||
private static final Logger logger = LogManager.getLogger(PhraseQueryCommand.class);
|
||||
|
||||
@Override
|
||||
protected String getQueryClassName() {
|
||||
|
@ -35,6 +38,9 @@ public class PhraseQueryCommand extends QueryCommand {
|
|||
@Override
|
||||
public QueryBuilder execute(final QueryContext context, final Query query, final float boost) {
|
||||
if (query instanceof final PhraseQuery phraseQuery) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("{}:{}", query, boost);
|
||||
}
|
||||
return convertPhraseQuery(context, phraseQuery, boost);
|
||||
}
|
||||
throw new InvalidQueryException(messages -> messages.addErrorsInvalidQueryUnknown(UserMessages.GLOBAL_PROPERTY_KEY),
|
||||
|
|
|
@ -17,6 +17,8 @@ package org.codelibs.fess.query;
|
|||
|
||||
import java.util.Locale;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.lucene.search.PrefixQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.codelibs.fess.Constants;
|
||||
|
@ -29,6 +31,8 @@ import org.opensearch.index.query.QueryBuilder;
|
|||
import org.opensearch.index.query.QueryBuilders;
|
||||
|
||||
public class PrefixQueryCommand extends QueryCommand {
|
||||
private static final Logger logger = LogManager.getLogger(PrefixQueryCommand.class);
|
||||
|
||||
protected boolean lowercaseWildcard = true;
|
||||
|
||||
@Override
|
||||
|
@ -39,6 +43,9 @@ public class PrefixQueryCommand extends QueryCommand {
|
|||
@Override
|
||||
public QueryBuilder execute(final QueryContext context, final Query query, final float boost) {
|
||||
if (query instanceof final PrefixQuery prefixQuery) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("{}:{}", query, boost);
|
||||
}
|
||||
return convertPrefixQuery(context, prefixQuery, boost);
|
||||
}
|
||||
throw new InvalidQueryException(messages -> messages.addErrorsInvalidQueryUnknown(UserMessages.GLOBAL_PROPERTY_KEY),
|
||||
|
@ -47,7 +54,7 @@ public class PrefixQueryCommand extends QueryCommand {
|
|||
|
||||
protected QueryBuilder convertPrefixQuery(final QueryContext context, final PrefixQuery prefixQuery, final float boost) {
|
||||
final FessConfig fessConfig = ComponentUtil.getFessConfig();
|
||||
final String field = getSearchField(context, prefixQuery.getField());
|
||||
final String field = getSearchField(context.getDefaultField(), prefixQuery.getField());
|
||||
if (Constants.DEFAULT_FIELD.equals(field)) {
|
||||
context.addFieldLog(field, prefixQuery.getPrefix().text());
|
||||
return buildDefaultQueryBuilder(
|
||||
|
@ -57,12 +64,12 @@ public class PrefixQueryCommand extends QueryCommand {
|
|||
if (!isSearchField(field)) {
|
||||
final String query = prefixQuery.getPrefix().toString();
|
||||
final String origQuery = toLowercaseWildcard(query);
|
||||
context.addFieldLog(Constants.DEFAULT_FIELD, query);
|
||||
context.addFieldLog(Constants.DEFAULT_FIELD, query + "*");
|
||||
context.addHighlightedQuery(origQuery);
|
||||
return buildDefaultQueryBuilder((f, b) -> QueryBuilders.matchPhrasePrefixQuery(f, origQuery).boost(b * boost)
|
||||
.maxExpansions(fessConfig.getQueryPrefixExpansionsAsInteger()).slop(fessConfig.getQueryPrefixSlopAsInteger()));
|
||||
}
|
||||
context.addFieldLog(field, prefixQuery.getPrefix().text());
|
||||
context.addFieldLog(field, prefixQuery.getPrefix().text() + "*");
|
||||
if (getQueryFieldConfig().notAnalyzedFieldSet.contains(field)) {
|
||||
return QueryBuilders.prefixQuery(field, toLowercaseWildcard(prefixQuery.getPrefix().text())).boost(boost);
|
||||
}
|
||||
|
|
|
@ -117,9 +117,9 @@ public abstract class QueryCommand {
|
|||
return QueryBuilders.matchPhraseQuery(f, text);
|
||||
}
|
||||
|
||||
protected String getSearchField(final QueryContext context, final String field) {
|
||||
if (Constants.DEFAULT_FIELD.equals(field) && context.getDefaultField() != null) {
|
||||
return context.getDefaultField();
|
||||
protected String getSearchField(final String defaultField, final String field) {
|
||||
if (Constants.DEFAULT_FIELD.equals(field) && defaultField != null) {
|
||||
return defaultField;
|
||||
}
|
||||
return field;
|
||||
}
|
||||
|
|
|
@ -16,7 +16,12 @@
|
|||
package org.codelibs.fess.query;
|
||||
|
||||
import static org.codelibs.core.stream.StreamUtil.split;
|
||||
import static org.codelibs.fess.Constants.DEFAULT_FIELD;
|
||||
import static org.codelibs.fess.query.QueryFieldConfig.INURL_FIELD;
|
||||
import static org.codelibs.fess.query.QueryFieldConfig.SITE_FIELD;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.search.PrefixQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
|
@ -35,6 +40,9 @@ import org.opensearch.index.query.QueryBuilders;
|
|||
import org.opensearch.search.sort.SortOrder;
|
||||
|
||||
public class TermQueryCommand extends QueryCommand {
|
||||
private static final Logger logger = LogManager.getLogger(TermQueryCommand.class);
|
||||
|
||||
private static final String SORT_FIELD = "sort";
|
||||
|
||||
@Override
|
||||
protected String getQueryClassName() {
|
||||
|
@ -44,6 +52,9 @@ public class TermQueryCommand extends QueryCommand {
|
|||
@Override
|
||||
public QueryBuilder execute(final QueryContext context, final Query query, final float boost) {
|
||||
if (query instanceof final TermQuery termQuery) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("{}:{}", query, boost);
|
||||
}
|
||||
return convertTermQuery(context, termQuery, boost);
|
||||
}
|
||||
throw new InvalidQueryException(messages -> messages.addErrorsInvalidQueryUnknown(UserMessages.GLOBAL_PROPERTY_KEY),
|
||||
|
@ -51,70 +62,103 @@ public class TermQueryCommand extends QueryCommand {
|
|||
}
|
||||
|
||||
protected QueryBuilder convertTermQuery(final QueryContext context, final TermQuery termQuery, final float boost) {
|
||||
final String field = getSearchField(context, termQuery.getTerm().field());
|
||||
final String field = getSearchField(context.getDefaultField(), termQuery.getTerm().field());
|
||||
final String text = termQuery.getTerm().text();
|
||||
final FessConfig fessConfig = ComponentUtil.getFessConfig();
|
||||
return convertTermQuery(fessConfig, context, termQuery, boost, field, text);
|
||||
}
|
||||
|
||||
protected QueryBuilder convertTermQuery(final FessConfig fessConfig, final QueryContext context, final TermQuery termQuery,
|
||||
final float boost, final String field, final String text) {
|
||||
if (fessConfig.getQueryReplaceTermWithPrefixQueryAsBoolean() && text.length() > 1 && text.endsWith("*")) {
|
||||
return getQueryProcessor().execute(context, new PrefixQuery(new Term(field, text.substring(0, text.length() - 1))), boost);
|
||||
return convertPrefixQuery(fessConfig, context, termQuery, boost, field, text);
|
||||
}
|
||||
if (Constants.DEFAULT_FIELD.equals(field)) {
|
||||
context.addFieldLog(field, text);
|
||||
context.addHighlightedQuery(text);
|
||||
return buildDefaultTermQueryBuilder(boost, text);
|
||||
if (DEFAULT_FIELD.equals(field)) {
|
||||
return convertDefaultTermQuery(fessConfig, context, termQuery, boost, field, text);
|
||||
}
|
||||
if ("sort".equals(field)) {
|
||||
split(text, ",").of(stream -> stream.filter(StringUtil::isNotBlank).forEach(t -> {
|
||||
final String[] values = t.split("\\.");
|
||||
if (values.length > 2) {
|
||||
throw new InvalidQueryException(
|
||||
messages -> messages.addErrorsInvalidQuerySortValue(UserMessages.GLOBAL_PROPERTY_KEY, text),
|
||||
"Invalid sort field: " + termQuery);
|
||||
}
|
||||
final String sortField = values[0];
|
||||
if (!getQueryFieldConfig().isSortField(sortField)) {
|
||||
throw new InvalidQueryException(
|
||||
messages -> messages.addErrorsInvalidQueryUnsupportedSortField(UserMessages.GLOBAL_PROPERTY_KEY, sortField),
|
||||
"Unsupported sort field: " + termQuery);
|
||||
}
|
||||
SortOrder sortOrder;
|
||||
if (values.length == 2) {
|
||||
sortOrder = SortOrder.DESC.toString().equalsIgnoreCase(values[1]) ? SortOrder.DESC : SortOrder.ASC;
|
||||
if (sortOrder == null) {
|
||||
throw new InvalidQueryException(
|
||||
messages -> messages.addErrorsInvalidQueryUnsupportedSortOrder(UserMessages.GLOBAL_PROPERTY_KEY, values[1]),
|
||||
"Invalid sort order: " + termQuery);
|
||||
}
|
||||
} else {
|
||||
sortOrder = SortOrder.ASC;
|
||||
}
|
||||
context.addSorts(createFieldSortBuilder(sortField, sortOrder));
|
||||
}));
|
||||
return null;
|
||||
if (SORT_FIELD.equals(field)) {
|
||||
return convertSortQuery(fessConfig, context, termQuery, boost, field, text);
|
||||
}
|
||||
if (QueryFieldConfig.INURL_FIELD.equals(field) || (StringUtil.equals(field, context.getDefaultField())
|
||||
if (SITE_FIELD.equals(field)) {
|
||||
return convertSiteQuery(fessConfig, context, termQuery, boost, field, text);
|
||||
}
|
||||
if (INURL_FIELD.equals(field) || (StringUtil.equals(field, context.getDefaultField())
|
||||
&& fessConfig.getIndexFieldUrl().equals(context.getDefaultField()))) {
|
||||
return QueryBuilders.wildcardQuery(fessConfig.getIndexFieldUrl(), "*" + text + "*").boost(boost);
|
||||
}
|
||||
if (QueryFieldConfig.SITE_FIELD.equals(field)) {
|
||||
return convertSiteQuery(context, text, boost);
|
||||
return convertWildcardQuery(fessConfig, context, termQuery, boost, field, text);
|
||||
}
|
||||
if (!isSearchField(field)) {
|
||||
final String origQuery = termQuery.toString();
|
||||
context.addFieldLog(Constants.DEFAULT_FIELD, origQuery);
|
||||
context.addHighlightedQuery(origQuery);
|
||||
return buildDefaultQueryBuilder((f, b) -> buildMatchPhraseQuery(f, origQuery).boost(b * boost));
|
||||
return convertDefaultTermQuery(fessConfig, context, termQuery, boost, DEFAULT_FIELD, origQuery);
|
||||
}
|
||||
if (getQueryFieldConfig().notAnalyzedFieldSet.contains(field)) {
|
||||
return convertKeywordQuery(fessConfig, context, termQuery, boost, field, text);
|
||||
}
|
||||
return convertTextQuery(fessConfig, context, termQuery, boost, field, text);
|
||||
}
|
||||
|
||||
protected QueryBuilder convertTextQuery(final FessConfig fessConfig, final QueryContext context, final TermQuery termQuery,
|
||||
final float boost, final String field, final String text) {
|
||||
context.addFieldLog(field, text);
|
||||
context.addHighlightedQuery(text);
|
||||
if (getQueryFieldConfig().notAnalyzedFieldSet.contains(field)) {
|
||||
return QueryBuilders.termQuery(field, text).boost(boost);
|
||||
}
|
||||
return buildMatchPhraseQuery(field, text).boost(boost);
|
||||
}
|
||||
|
||||
protected QueryBuilder buildDefaultTermQueryBuilder(final float boost, final String text) {
|
||||
protected QueryBuilder convertKeywordQuery(final FessConfig fessConfig, final QueryContext context, final TermQuery termQuery,
|
||||
final float boost, final String field, final String text) {
|
||||
context.addFieldLog(field, text);
|
||||
context.addHighlightedQuery(text);
|
||||
return QueryBuilders.termQuery(field, text).boost(boost);
|
||||
}
|
||||
|
||||
protected QueryBuilder convertWildcardQuery(final FessConfig fessConfig, final QueryContext context, final TermQuery termQuery,
|
||||
final float boost, final String field, final String text) {
|
||||
final String urlField = fessConfig.getIndexFieldUrl();
|
||||
final String queryString = "*" + text + "*";
|
||||
context.addFieldLog(urlField, queryString);
|
||||
context.addHighlightedQuery(text);
|
||||
return QueryBuilders.wildcardQuery(urlField, queryString).boost(boost);
|
||||
}
|
||||
|
||||
protected QueryBuilder convertPrefixQuery(final FessConfig fessConfig, final QueryContext context, final TermQuery termQuery,
|
||||
final float boost, final String field, final String text) {
|
||||
return getQueryProcessor().execute(context, new PrefixQuery(new Term(field, text.substring(0, text.length() - 1))), boost);
|
||||
}
|
||||
|
||||
protected QueryBuilder convertSortQuery(final FessConfig fessConfig, final QueryContext context, final TermQuery termQuery,
|
||||
final float boost, final String field, final String text) {
|
||||
split(text, ",").of(stream -> stream.filter(StringUtil::isNotBlank).forEach(t -> {
|
||||
final String[] values = t.split("\\.");
|
||||
if (values.length > 2) {
|
||||
throw new InvalidQueryException(messages -> messages.addErrorsInvalidQuerySortValue(UserMessages.GLOBAL_PROPERTY_KEY, text),
|
||||
"Invalid sort field: " + termQuery);
|
||||
}
|
||||
final String sortField = values[0];
|
||||
if (!getQueryFieldConfig().isSortField(sortField)) {
|
||||
throw new InvalidQueryException(
|
||||
messages -> messages.addErrorsInvalidQueryUnsupportedSortField(UserMessages.GLOBAL_PROPERTY_KEY, sortField),
|
||||
"Unsupported sort field: " + termQuery);
|
||||
}
|
||||
SortOrder sortOrder;
|
||||
if (values.length == 2) {
|
||||
sortOrder = SortOrder.DESC.toString().equalsIgnoreCase(values[1]) ? SortOrder.DESC : SortOrder.ASC;
|
||||
if (sortOrder == null) {
|
||||
throw new InvalidQueryException(
|
||||
messages -> messages.addErrorsInvalidQueryUnsupportedSortOrder(UserMessages.GLOBAL_PROPERTY_KEY, values[1]),
|
||||
"Invalid sort order: " + termQuery);
|
||||
}
|
||||
} else {
|
||||
sortOrder = SortOrder.ASC;
|
||||
}
|
||||
context.addSorts(createFieldSortBuilder(sortField, sortOrder));
|
||||
}));
|
||||
return null;
|
||||
}
|
||||
|
||||
protected QueryBuilder convertDefaultTermQuery(final FessConfig fessConfig, final QueryContext context, final TermQuery termQuery,
|
||||
final float boost, final String field, final String text) {
|
||||
context.addFieldLog(field, text);
|
||||
context.addHighlightedQuery(text);
|
||||
final BoolQueryBuilder boolQuery = buildDefaultQueryBuilder((f, b) -> buildMatchPhraseQuery(f, text).boost(b * boost));
|
||||
final FessConfig fessConfig = ComponentUtil.getFessConfig();
|
||||
final Integer fuzzyMinLength = fessConfig.getQueryBoostFuzzyMinLengthAsInteger();
|
||||
if (fuzzyMinLength >= 0 && text.length() >= fuzzyMinLength) {
|
||||
boolQuery.should(QueryBuilders.fuzzyQuery(fessConfig.getIndexFieldTitle(), text)
|
||||
|
@ -133,12 +177,15 @@ public class TermQueryCommand extends QueryCommand {
|
|||
return boolQuery;
|
||||
}
|
||||
|
||||
protected QueryBuilder convertSiteQuery(final QueryContext context, final String text, final float boost) {
|
||||
return QueryBuilders.prefixQuery(ComponentUtil.getFessConfig().getIndexFieldSite(), text).boost(boost);
|
||||
protected QueryBuilder convertSiteQuery(final FessConfig fessConfig, final QueryContext context, final TermQuery termQuery,
|
||||
final float boost, final String field, final String text) {
|
||||
final String siteField = fessConfig.getIndexFieldSite();
|
||||
context.addFieldLog(siteField, text + "*");
|
||||
context.addHighlightedQuery(text);
|
||||
return QueryBuilders.prefixQuery(siteField, text).boost(boost);
|
||||
}
|
||||
|
||||
interface DefaultQueryBuilderFunction {
|
||||
QueryBuilder apply(String field, float boost);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -15,6 +15,8 @@
|
|||
*/
|
||||
package org.codelibs.fess.query;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.TermRangeQuery;
|
||||
import org.apache.lucene.util.BytesRef;
|
||||
|
@ -27,6 +29,7 @@ import org.opensearch.index.query.QueryBuilders;
|
|||
import org.opensearch.index.query.RangeQueryBuilder;
|
||||
|
||||
public class TermRangeQueryCommand extends QueryCommand {
|
||||
private static final Logger logger = LogManager.getLogger(TermRangeQueryCommand.class);
|
||||
|
||||
@Override
|
||||
protected String getQueryClassName() {
|
||||
|
@ -36,6 +39,9 @@ public class TermRangeQueryCommand extends QueryCommand {
|
|||
@Override
|
||||
public QueryBuilder execute(final QueryContext context, final Query query, final float boost) {
|
||||
if (query instanceof final TermRangeQuery termRangeQuery) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("{}:{}", query, boost);
|
||||
}
|
||||
return convertTermRangeQuery(context, termRangeQuery, boost);
|
||||
}
|
||||
throw new InvalidQueryException(messages -> messages.addErrorsInvalidQueryUnknown(UserMessages.GLOBAL_PROPERTY_KEY),
|
||||
|
@ -43,7 +49,7 @@ public class TermRangeQueryCommand extends QueryCommand {
|
|||
}
|
||||
|
||||
protected QueryBuilder convertTermRangeQuery(final QueryContext context, final TermRangeQuery termRangeQuery, final float boost) {
|
||||
final String field = getSearchField(context, termRangeQuery.getField());
|
||||
final String field = getSearchField(context.getDefaultField(), termRangeQuery.getField());
|
||||
if (!isSearchField(field)) {
|
||||
final String origQuery = termRangeQuery.toString();
|
||||
context.addFieldLog(Constants.DEFAULT_FIELD, origQuery);
|
||||
|
|
|
@ -17,6 +17,8 @@ package org.codelibs.fess.query;
|
|||
|
||||
import java.util.Locale;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.WildcardQuery;
|
||||
import org.codelibs.fess.Constants;
|
||||
|
@ -27,6 +29,8 @@ import org.opensearch.index.query.QueryBuilder;
|
|||
import org.opensearch.index.query.QueryBuilders;
|
||||
|
||||
public class WildcardQueryCommand extends QueryCommand {
|
||||
private static final Logger logger = LogManager.getLogger(WildcardQueryCommand.class);
|
||||
|
||||
protected boolean lowercaseWildcard = true;
|
||||
|
||||
@Override
|
||||
|
@ -37,6 +41,9 @@ public class WildcardQueryCommand extends QueryCommand {
|
|||
@Override
|
||||
public QueryBuilder execute(final QueryContext context, final Query query, final float boost) {
|
||||
if (query instanceof final WildcardQuery wildcardQuery) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("{}:{}", query, boost);
|
||||
}
|
||||
return convertWildcardQuery(context, wildcardQuery, boost);
|
||||
}
|
||||
throw new InvalidQueryException(messages -> messages.addErrorsInvalidQueryUnknown(UserMessages.GLOBAL_PROPERTY_KEY),
|
||||
|
@ -44,7 +51,7 @@ public class WildcardQueryCommand extends QueryCommand {
|
|||
}
|
||||
|
||||
protected QueryBuilder convertWildcardQuery(final QueryContext context, final WildcardQuery wildcardQuery, final float boost) {
|
||||
final String field = getSearchField(context, wildcardQuery.getField());
|
||||
final String field = getSearchField(context.getDefaultField(), wildcardQuery.getField());
|
||||
if (Constants.DEFAULT_FIELD.equals(field)) {
|
||||
context.addFieldLog(field, wildcardQuery.getTerm().text());
|
||||
return buildDefaultQueryBuilder(
|
||||
|
@ -55,9 +62,9 @@ public class WildcardQueryCommand extends QueryCommand {
|
|||
return QueryBuilders.wildcardQuery(field, toLowercaseWildcard(wildcardQuery.getTerm().text())).boost(boost);
|
||||
}
|
||||
final String query = wildcardQuery.getTerm().toString();
|
||||
final String origQuery = toLowercaseWildcard(query);
|
||||
context.addFieldLog(Constants.DEFAULT_FIELD, query);
|
||||
context.addHighlightedQuery(origQuery);
|
||||
final String origQuery = "*" + toLowercaseWildcard(query) + "*";
|
||||
context.addFieldLog(Constants.DEFAULT_FIELD, origQuery);
|
||||
context.addHighlightedQuery(query);
|
||||
return buildDefaultQueryBuilder((f, b) -> QueryBuilders.wildcardQuery(f, origQuery).boost(b * boost));
|
||||
}
|
||||
|
||||
|
|
|
@ -75,7 +75,7 @@ public class TermQueryCommandTest extends UnitFessTestCase {
|
|||
"{\"match_phrase\":{\"content\":{\"query\":\"aaa\",\"slop\":0,\"zero_terms_query\":\"NONE\",\"boost\":1.0}}}", //
|
||||
"content:aaa");
|
||||
assertQueryBuilder(BoolQueryBuilder.class,
|
||||
"{\"bool\":{\"should\":[{\"match_phrase\":{\"title\":{\"query\":\"xxx:aaa\",\"slop\":0,\"zero_terms_query\":\"NONE\",\"boost\":0.5}}},{\"match_phrase\":{\"content\":{\"query\":\"xxx:aaa\",\"slop\":0,\"zero_terms_query\":\"NONE\",\"boost\":0.05}}}],\"adjust_pure_negative\":true,\"boost\":1.0}}",
|
||||
"{\"bool\":{\"should\":[{\"match_phrase\":{\"title\":{\"query\":\"xxx:aaa\",\"slop\":0,\"zero_terms_query\":\"NONE\",\"boost\":0.5}}},{\"match_phrase\":{\"content\":{\"query\":\"xxx:aaa\",\"slop\":0,\"zero_terms_query\":\"NONE\",\"boost\":0.05}}},{\"fuzzy\":{\"title\":{\"value\":\"xxx:aaa\",\"fuzziness\":\"AUTO\",\"prefix_length\":0,\"max_expansions\":10,\"transpositions\":true,\"boost\":0.01}}},{\"fuzzy\":{\"content\":{\"value\":\"xxx:aaa\",\"fuzziness\":\"AUTO\",\"prefix_length\":0,\"max_expansions\":10,\"transpositions\":true,\"boost\":0.005}}}],\"adjust_pure_negative\":true,\"boost\":1.0}}",
|
||||
"xxx:aaa");
|
||||
assertQueryBuilder(WildcardQueryBuilder.class, //
|
||||
"{\"wildcard\":{\"url\":{\"wildcard\":\"*aaa*\",\"boost\":1.0}}}", //
|
||||
|
|
Loading…
Add table
Reference in a new issue