feat: add boosting to context based search

This commit is contained in:
Michel Jonathan Schmitz
2019-04-16 15:29:13 +02:00
committed by Rainer Killinger
parent 67c814443c
commit dd4be92f90
4 changed files with 268 additions and 661 deletions

View File

@@ -14,9 +14,11 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {
SCBackendConfigurationSearchBoosting,
SCBackendConfigurationSearchBoostingContext,
SCBackendConfigurationSearchBoostingType,
SCConfigFile,
SCSearchBooleanFilter,
SCSearchContext,
SCSearchFilter,
SCSearchQuery,
SCSearchSort,
@@ -147,15 +149,37 @@ export function buildFilter(filter: SCSearchFilter): ESTermFilter | ESGeoDistanc
/**
* Builds scorings functions from boosting config
* @param boosting
* @returns
* @param boostings Backend boosting configuration for contexts and types
* @param context The context of the app from where the search was initiated
*/
export function buildFunctions(boosting: SCBackendConfigurationSearchBoosting[]): ESFunctionScoreQueryFunction[] {
function buildFunctions(
boostings: SCBackendConfigurationSearchBoostingContext,
context: SCSearchContext | undefined,
): ESFunctionScoreQueryFunction[] {
// default context
let functions: ESFunctionScoreQueryFunction[] =
buildFunctionsForBoostingTypes(boostings['default' as SCSearchContext]);
if (typeof context !== 'undefined' && context !== 'default') {
// specific context provided, extend default context with additional boosts
functions = functions.concat(buildFunctionsForBoostingTypes(boostings[context]));
}
return functions;
}
/**
* Creates boost functions for all type boost configurations
*
* @param boostingTypes Array of type boosting configurations
*/
function buildFunctionsForBoostingTypes(
boostingTypes: SCBackendConfigurationSearchBoostingType[],
): ESFunctionScoreQueryFunction[] {
const functions: ESFunctionScoreQueryFunction[] = [];
// add a good scoring subset from config file
boosting.forEach((boostingForOneSCType) => {
boostingTypes.forEach((boostingForOneSCType) => {
const typeFilter: ESTypeFilter = {
type: {
value: boostingForOneSCType.type,
@@ -305,7 +329,7 @@ export function buildQuery(
const functionScoreQuery: ESFunctionScoreQuery = {
function_score: {
functions: buildFunctions(defaultConfig.internal.boostings),
functions: buildFunctions(defaultConfig.internal.boostings, params.context),
query: {
bool: {
minimum_should_match: 0, // if we have no should, nothing can match