add title/content boost
This commit is contained in:
parent
fb899e5421
commit
3fe86cc092
3 changed files with 145 additions and 24 deletions
|
@ -25,7 +25,6 @@ import java.util.Map;
|
|||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
@ -309,7 +308,7 @@ public class QueryHelper implements Serializable {
|
|||
|
||||
protected QueryBuilder convertBooleanQuery(final QueryContext context, final BooleanQuery booleanQuery) {
|
||||
final BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
|
||||
for (final BooleanClause clause : booleanQuery.getClauses()) {
|
||||
for (final BooleanClause clause : booleanQuery.clauses()) {
|
||||
final QueryBuilder queryBuilder = convertQuery(context, clause.getQuery());
|
||||
if (queryBuilder != null) {
|
||||
switch (clause.getOccur()) {
|
||||
|
@ -334,7 +333,7 @@ public class QueryHelper implements Serializable {
|
|||
final String field = wildcardQuery.getField();
|
||||
if (Constants.DEFAULT_FIELD.equals(field)) {
|
||||
context.addFieldLog(field, wildcardQuery.getTerm().text());
|
||||
return buildDefaultQueryBuilder(f -> QueryBuilders.wildcardQuery(f, wildcardQuery.getTerm().text()));
|
||||
return buildDefaultQueryBuilder((f, b) -> QueryBuilders.wildcardQuery(f, wildcardQuery.getTerm().text()).boost(b));
|
||||
} else if (isSearchField(field)) {
|
||||
context.addFieldLog(field, wildcardQuery.getTerm().text());
|
||||
return QueryBuilders.wildcardQuery(field, wildcardQuery.getTerm().text()).boost(wildcardQuery.getBoost());
|
||||
|
@ -342,7 +341,7 @@ public class QueryHelper implements Serializable {
|
|||
final String origQuery = wildcardQuery.getTerm().toString();
|
||||
context.addFieldLog(Constants.DEFAULT_FIELD, origQuery);
|
||||
context.addHighlightedQuery(origQuery);
|
||||
return buildDefaultQueryBuilder(f -> QueryBuilders.wildcardQuery(f, origQuery));
|
||||
return buildDefaultQueryBuilder((f, b) -> QueryBuilders.wildcardQuery(f, origQuery).boost(b));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -350,7 +349,7 @@ public class QueryHelper implements Serializable {
|
|||
final String field = prefixQuery.getField();
|
||||
if (Constants.DEFAULT_FIELD.equals(field)) {
|
||||
context.addFieldLog(field, prefixQuery.getPrefix().text());
|
||||
return buildDefaultQueryBuilder(f -> QueryBuilders.prefixQuery(f, prefixQuery.getPrefix().text()));
|
||||
return buildDefaultQueryBuilder((f, b) -> QueryBuilders.prefixQuery(f, prefixQuery.getPrefix().text()).boost(b));
|
||||
} else if (isSearchField(field)) {
|
||||
context.addFieldLog(field, prefixQuery.getPrefix().text());
|
||||
return QueryBuilders.prefixQuery(field, prefixQuery.getPrefix().text()).boost(prefixQuery.getBoost());
|
||||
|
@ -358,7 +357,7 @@ public class QueryHelper implements Serializable {
|
|||
final String origQuery = prefixQuery.getPrefix().toString();
|
||||
context.addFieldLog(Constants.DEFAULT_FIELD, origQuery);
|
||||
context.addHighlightedQuery(origQuery);
|
||||
return buildDefaultQueryBuilder(f -> QueryBuilders.prefixQuery(f, origQuery));
|
||||
return buildDefaultQueryBuilder((f, b) -> QueryBuilders.prefixQuery(f, origQuery).boost(b));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -368,8 +367,8 @@ public class QueryHelper implements Serializable {
|
|||
// TODO fuzzy value
|
||||
if (Constants.DEFAULT_FIELD.equals(field)) {
|
||||
context.addFieldLog(field, term.text());
|
||||
return buildDefaultQueryBuilder(f -> QueryBuilders.fuzzyQuery(f, term.text()).fuzziness(
|
||||
Fuzziness.fromEdits(fuzzyQuery.getMaxEdits())));
|
||||
return buildDefaultQueryBuilder((f, b) -> QueryBuilders.fuzzyQuery(f, term.text())
|
||||
.fuzziness(Fuzziness.fromEdits(fuzzyQuery.getMaxEdits())).boost(b));
|
||||
} else if (isSearchField(field)) {
|
||||
context.addFieldLog(field, term.text());
|
||||
return QueryBuilders.fuzzyQuery(field, term.text()).boost(fuzzyQuery.getBoost())
|
||||
|
@ -378,8 +377,8 @@ public class QueryHelper implements Serializable {
|
|||
final String origQuery = fuzzyQuery.toString();
|
||||
context.addFieldLog(Constants.DEFAULT_FIELD, origQuery);
|
||||
context.addHighlightedQuery(origQuery);
|
||||
return buildDefaultQueryBuilder(f -> QueryBuilders.fuzzyQuery(f, origQuery).fuzziness(
|
||||
Fuzziness.fromEdits(fuzzyQuery.getMaxEdits())));
|
||||
return buildDefaultQueryBuilder((f, b) -> QueryBuilders.fuzzyQuery(f, origQuery)
|
||||
.fuzziness(Fuzziness.fromEdits(fuzzyQuery.getMaxEdits())).boost(b));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -410,7 +409,7 @@ public class QueryHelper implements Serializable {
|
|||
final String origQuery = termRangeQuery.toString();
|
||||
context.addFieldLog(Constants.DEFAULT_FIELD, origQuery);
|
||||
context.addHighlightedQuery(origQuery);
|
||||
return buildDefaultQueryBuilder(f -> QueryBuilders.matchPhraseQuery(f, origQuery));
|
||||
return buildDefaultQueryBuilder((f, b) -> QueryBuilders.matchPhraseQuery(f, origQuery).boost(b));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -420,7 +419,7 @@ public class QueryHelper implements Serializable {
|
|||
if (Constants.DEFAULT_FIELD.equals(field)) {
|
||||
context.addFieldLog(field, text);
|
||||
context.addHighlightedQuery(text);
|
||||
return buildDefaultQueryBuilder(f -> QueryBuilders.matchPhraseQuery(f, text));
|
||||
return buildDefaultQueryBuilder((f, b) -> QueryBuilders.matchPhraseQuery(f, text).boost(b));
|
||||
} else if ("sort".equals(field)) {
|
||||
final String[] values = text.split("\\.");
|
||||
if (values.length > 2) {
|
||||
|
@ -454,7 +453,7 @@ public class QueryHelper implements Serializable {
|
|||
final String origQuery = termQuery.toString();
|
||||
context.addFieldLog(Constants.DEFAULT_FIELD, origQuery);
|
||||
context.addHighlightedQuery(origQuery);
|
||||
return buildDefaultQueryBuilder(f -> QueryBuilders.matchPhraseQuery(f, origQuery));
|
||||
return buildDefaultQueryBuilder((f, b) -> QueryBuilders.matchPhraseQuery(f, origQuery).boost(b));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -467,22 +466,32 @@ public class QueryHelper implements Serializable {
|
|||
return false;
|
||||
}
|
||||
|
||||
private QueryBuilder buildDefaultQueryBuilder(final Function<String, QueryBuilder> builder) {
|
||||
// TODO boost
|
||||
private QueryBuilder buildDefaultQueryBuilder(final DefaultQueryBuilderFunction builder) {
|
||||
final BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
|
||||
final QueryBuilder titleQuery = builder.apply(fessConfig.getIndexFieldTitle());
|
||||
final QueryBuilder titleQuery =
|
||||
builder.apply(fessConfig.getIndexFieldTitle(), fessConfig.getQueryBoostTitleAsDecimal().floatValue());
|
||||
boolQuery.should(titleQuery);
|
||||
final QueryBuilder contentQuery = builder.apply(fessConfig.getIndexFieldContent());
|
||||
final QueryBuilder contentQuery =
|
||||
builder.apply(fessConfig.getIndexFieldContent(), fessConfig.getQueryBoostContentAsDecimal().floatValue());
|
||||
boolQuery.should(contentQuery);
|
||||
getQueryLanguage().ifPresent(lang -> {
|
||||
final QueryBuilder titleLangQuery = builder.apply(fessConfig.getIndexFieldTitle() + "_" + lang);
|
||||
boolQuery.should(titleLangQuery);
|
||||
final QueryBuilder contentLangQuery = builder.apply(fessConfig.getIndexFieldContent() + "_" + lang);
|
||||
boolQuery.should(contentLangQuery);
|
||||
});
|
||||
getQueryLanguage().ifPresent(
|
||||
lang -> {
|
||||
final QueryBuilder titleLangQuery =
|
||||
builder.apply(fessConfig.getIndexFieldTitle() + "_" + lang, fessConfig.getQueryBoostTitleLangAsDecimal()
|
||||
.floatValue());
|
||||
boolQuery.should(titleLangQuery);
|
||||
final QueryBuilder contentLangQuery =
|
||||
builder.apply(fessConfig.getIndexFieldContent() + "_" + lang, fessConfig.getQueryBoostContentLangAsDecimal()
|
||||
.floatValue());
|
||||
boolQuery.should(contentLangQuery);
|
||||
});
|
||||
return boolQuery;
|
||||
}
|
||||
|
||||
interface DefaultQueryBuilderFunction {
|
||||
QueryBuilder apply(String field, float boost);
|
||||
}
|
||||
|
||||
protected OptionalThing<String> getQueryLanguage() {
|
||||
if (defaultQueryLanguage != null) {
|
||||
return OptionalEntity.of(defaultQueryLanguage);
|
||||
|
|
|
@ -115,6 +115,18 @@ public interface FessConfig extends FessEnv {
|
|||
/** The key of the configuration. e.g. doc */
|
||||
String INDEX_DOCUMENT_TYPE = "index.document.type";
|
||||
|
||||
/** The key of the configuration. e.g. 1.6 */
|
||||
String QUERY_BOOST_TITLE = "query.boost.title";
|
||||
|
||||
/** The key of the configuration. e.g. 2.0 */
|
||||
String QUERY_BOOST_TITLE_LANG = "query.boost.title.lang";
|
||||
|
||||
/** The key of the configuration. e.g. 1.0 */
|
||||
String QUERY_BOOST_CONTENT = "query.boost.content";
|
||||
|
||||
/** The key of the configuration. e.g. 1.3 */
|
||||
String QUERY_BOOST_CONTENT_LANG = "query.boost.content.lang";
|
||||
|
||||
/** The key of the configuration. e.g. admin */
|
||||
String AUTHENTICATION_ADMIN_ROLES = "authentication.admin.roles";
|
||||
|
||||
|
@ -499,6 +511,68 @@ public interface FessConfig extends FessEnv {
|
|||
*/
|
||||
String getIndexDocumentType();
|
||||
|
||||
/**
|
||||
* Get the value for the key 'query.boost.title'. <br>
|
||||
* The value is, e.g. 1.6 <br>
|
||||
* comment: boost
|
||||
* @return The value of found property. (NotNull: if not found, exception but basically no way)
|
||||
*/
|
||||
String getQueryBoostTitle();
|
||||
|
||||
/**
|
||||
* Get the value for the key 'query.boost.title' as {@link java.math.BigDecimal}. <br>
|
||||
* The value is, e.g. 1.6 <br>
|
||||
* comment: boost
|
||||
* @return The value of found property. (NotNull: if not found, exception but basically no way)
|
||||
* @throws NumberFormatException When the property is not decimal.
|
||||
*/
|
||||
java.math.BigDecimal getQueryBoostTitleAsDecimal();
|
||||
|
||||
/**
|
||||
* Get the value for the key 'query.boost.title.lang'. <br>
|
||||
* The value is, e.g. 2.0 <br>
|
||||
* @return The value of found property. (NotNull: if not found, exception but basically no way)
|
||||
*/
|
||||
String getQueryBoostTitleLang();
|
||||
|
||||
/**
|
||||
* Get the value for the key 'query.boost.title.lang' as {@link java.math.BigDecimal}. <br>
|
||||
* The value is, e.g. 2.0 <br>
|
||||
* @return The value of found property. (NotNull: if not found, exception but basically no way)
|
||||
* @throws NumberFormatException When the property is not decimal.
|
||||
*/
|
||||
java.math.BigDecimal getQueryBoostTitleLangAsDecimal();
|
||||
|
||||
/**
|
||||
* Get the value for the key 'query.boost.content'. <br>
|
||||
* The value is, e.g. 1.0 <br>
|
||||
* @return The value of found property. (NotNull: if not found, exception but basically no way)
|
||||
*/
|
||||
String getQueryBoostContent();
|
||||
|
||||
/**
|
||||
* Get the value for the key 'query.boost.content' as {@link java.math.BigDecimal}. <br>
|
||||
* The value is, e.g. 1.0 <br>
|
||||
* @return The value of found property. (NotNull: if not found, exception but basically no way)
|
||||
* @throws NumberFormatException When the property is not decimal.
|
||||
*/
|
||||
java.math.BigDecimal getQueryBoostContentAsDecimal();
|
||||
|
||||
/**
|
||||
* Get the value for the key 'query.boost.content.lang'. <br>
|
||||
* The value is, e.g. 1.3 <br>
|
||||
* @return The value of found property. (NotNull: if not found, exception but basically no way)
|
||||
*/
|
||||
String getQueryBoostContentLang();
|
||||
|
||||
/**
|
||||
* Get the value for the key 'query.boost.content.lang' as {@link java.math.BigDecimal}. <br>
|
||||
* The value is, e.g. 1.3 <br>
|
||||
* @return The value of found property. (NotNull: if not found, exception but basically no way)
|
||||
* @throws NumberFormatException When the property is not decimal.
|
||||
*/
|
||||
java.math.BigDecimal getQueryBoostContentLangAsDecimal();
|
||||
|
||||
/**
|
||||
* Get the value for the key 'authentication.admin.roles'. <br>
|
||||
* The value is, e.g. admin <br>
|
||||
|
@ -1053,6 +1127,38 @@ public interface FessConfig extends FessEnv {
|
|||
return get(FessConfig.INDEX_DOCUMENT_TYPE);
|
||||
}
|
||||
|
||||
public String getQueryBoostTitle() {
|
||||
return get(FessConfig.QUERY_BOOST_TITLE);
|
||||
}
|
||||
|
||||
public java.math.BigDecimal getQueryBoostTitleAsDecimal() {
|
||||
return getAsDecimal(FessConfig.QUERY_BOOST_TITLE);
|
||||
}
|
||||
|
||||
public String getQueryBoostTitleLang() {
|
||||
return get(FessConfig.QUERY_BOOST_TITLE_LANG);
|
||||
}
|
||||
|
||||
public java.math.BigDecimal getQueryBoostTitleLangAsDecimal() {
|
||||
return getAsDecimal(FessConfig.QUERY_BOOST_TITLE_LANG);
|
||||
}
|
||||
|
||||
public String getQueryBoostContent() {
|
||||
return get(FessConfig.QUERY_BOOST_CONTENT);
|
||||
}
|
||||
|
||||
public java.math.BigDecimal getQueryBoostContentAsDecimal() {
|
||||
return getAsDecimal(FessConfig.QUERY_BOOST_CONTENT);
|
||||
}
|
||||
|
||||
public String getQueryBoostContentLang() {
|
||||
return get(FessConfig.QUERY_BOOST_CONTENT_LANG);
|
||||
}
|
||||
|
||||
public java.math.BigDecimal getQueryBoostContentLangAsDecimal() {
|
||||
return getAsDecimal(FessConfig.QUERY_BOOST_CONTENT_LANG);
|
||||
}
|
||||
|
||||
public String getAuthenticationAdminRoles() {
|
||||
return get(FessConfig.AUTHENTICATION_ADMIN_ROLES);
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ domain.title = Fess
|
|||
|
||||
|
||||
# ========================================================================================
|
||||
# Crawler
|
||||
# Index
|
||||
# ====
|
||||
|
||||
crawler.document.cache.enable=false
|
||||
|
@ -48,6 +48,12 @@ index.field.filetype=filetype
|
|||
index.document.index=fess
|
||||
index.document.type=doc
|
||||
|
||||
# boost
|
||||
query.boost.title=1.6
|
||||
query.boost.title.lang=2.0
|
||||
query.boost.content=1.0
|
||||
query.boost.content.lang=1.3
|
||||
|
||||
# ========================================================================================
|
||||
# Web
|
||||
# =====
|
||||
|
|
Loading…
Add table
Reference in a new issue