function typeQueryWithConditions()

in src/QueryBuilder/index.ts [26:97]


function typeQueryWithConditions(
    searchParam: SearchParam,
    compiledSearchParam: CompiledSearchParam,
    searchValue: unknown,
    useKeywordSubFields: boolean,
    baseUrl: string,
    modifier?: string,
): any {
    let typeQuery: any;
    switch (searchParam.type) {
        case 'string':
            typeQuery = stringQuery(compiledSearchParam, searchValue as string, modifier);
            break;
        case 'date':
            typeQuery = dateQuery(compiledSearchParam, searchValue as DateSearchValue, modifier);
            break;
        case 'token':
            typeQuery = tokenQuery(compiledSearchParam, searchValue as TokenSearchValue, useKeywordSubFields, modifier);
            break;
        case 'number':
            typeQuery = numberQuery(compiledSearchParam, searchValue as NumberSearchValue, modifier);
            break;
        case 'quantity':
            typeQuery = quantityQuery(
                compiledSearchParam,
                searchValue as QuantitySearchValue,
                useKeywordSubFields,
                modifier,
            );
            break;
        case 'reference':
            typeQuery = referenceQuery(
                compiledSearchParam,
                searchValue as ReferenceSearchValue,
                useKeywordSubFields,
                baseUrl,
                searchParam.name,
                searchParam.target,
                modifier,
            );
            break;
        case 'uri':
            typeQuery = uriQuery(compiledSearchParam, searchValue as string, useKeywordSubFields, modifier);
            break;
        case 'composite':
        case 'special':
        default:
            typeQuery = stringQuery(compiledSearchParam, searchValue as string, modifier);
    }
    // In most cases conditions are used for fields that are an array of objects
    // Ideally we should be using a nested query, but that'd require to update the index mappings.
    //
    // Simply using an array of bool.must is good enough for most cases. The result will contain the correct documents, however it MAY contain additional documents
    // https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html
    if (compiledSearchParam.condition !== undefined) {
        return {
            bool: {
                must: [
                    typeQuery,
                    {
                        multi_match: {
                            fields: [compiledSearchParam.condition[0], `${compiledSearchParam.condition[0]}.*`],
                            query: compiledSearchParam.condition[2],
                            lenient: true,
                        },
                    },
                ],
            },
        };
    }
    return typeQuery;
}