use of org.opensearch.index.query.functionscore.ScoreFunctionBuilder in project fess by codelibs.
the class KeyMatchHelper method getBoostedDocumentList.
public List<Map<String, Object>> getBoostedDocumentList(final KeyMatch keyMatch) {
final SearchEngineClient searchEngineClient = ComponentUtil.getSearchEngineClient();
String virtualHost = keyMatch.getVirtualHost();
if (StringUtil.isBlank(virtualHost)) {
virtualHost = StringUtil.EMPTY;
}
final List<Tuple3<String, QueryBuilder, ScoreFunctionBuilder<?>>> boostList = getQueryMap(virtualHost).get(toLowerCase(keyMatch.getTerm()));
if (boostList == null) {
return Collections.emptyList();
}
for (final Tuple3<String, QueryBuilder, ScoreFunctionBuilder<?>> pair : boostList) {
if (!keyMatch.getId().equals(pair.getValue1())) {
continue;
}
final FessConfig fessConfig = ComponentUtil.getFessConfig();
return searchEngineClient.getDocumentList(fessConfig.getIndexDocumentSearchIndex(), searchRequestBuilder -> {
searchRequestBuilder.setPreference(Constants.SEARCH_PREFERENCE_LOCAL).setQuery(pair.getValue2()).setSize(keyMatch.getMaxSize());
return true;
});
}
return Collections.emptyList();
}
use of org.opensearch.index.query.functionscore.ScoreFunctionBuilder in project fess by codelibs.
the class KeyMatchHelper method load.
@Override
public int load() {
final FessConfig fessConfig = ComponentUtil.getFessConfig();
final Map<String, Map<String, List<Tuple3<String, QueryBuilder, ScoreFunctionBuilder<?>>>>> keyMatchQueryMap = new HashMap<>();
getAvailableKeyMatchList().stream().forEach(keyMatch -> {
try {
final BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
if (logger.isDebugEnabled()) {
logger.debug("Loading KeyMatch Query: {}, Size: {}", keyMatch.getQuery(), keyMatch.getMaxSize());
}
getDocumentList(keyMatch).stream().map(doc -> {
if (logger.isDebugEnabled()) {
logger.debug("Loaded KeyMatch doc: {}", doc);
}
return DocumentUtil.getValue(doc, fessConfig.getIndexFieldDocId(), String.class);
}).forEach(docId -> {
boolQuery.should(QueryBuilders.termQuery(fessConfig.getIndexFieldDocId(), docId));
});
if (boolQuery.hasClauses()) {
if (logger.isDebugEnabled()) {
logger.debug("Loaded KeyMatch Boost Query: {}", boolQuery);
}
String virtualHost = keyMatch.getVirtualHost();
if (StringUtil.isBlank(virtualHost)) {
virtualHost = StringUtil.EMPTY;
}
Map<String, List<Tuple3<String, QueryBuilder, ScoreFunctionBuilder<?>>>> queryMap = keyMatchQueryMap.get(virtualHost);
if (queryMap == null) {
queryMap = new HashMap<>();
keyMatchQueryMap.put(virtualHost, queryMap);
}
final String termKey = toLowerCase(keyMatch.getTerm());
List<Tuple3<String, QueryBuilder, ScoreFunctionBuilder<?>>> boostList = queryMap.get(termKey);
if (boostList == null) {
boostList = new ArrayList<>();
queryMap.put(termKey, boostList);
}
boostList.add(new Tuple3<>(keyMatch.getId(), boolQuery, ScoreFunctionBuilders.weightFactorFunction(keyMatch.getBoost())));
} else if (logger.isDebugEnabled()) {
logger.debug("No KeyMatch boost docs");
}
waitForNext();
} catch (final Exception e) {
logger.warn("Cannot load {}", keyMatch, e);
}
});
this.keyMatchQueryMap = keyMatchQueryMap;
return keyMatchQueryMap.size();
}
Aggregations