Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
abeglova committed Jun 26, 2023
1 parent ad58b4c commit 45f8b59
Showing 1 changed file with 55 additions and 25 deletions.
80 changes: 55 additions & 25 deletions src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ export const normalizeDoubleQuotes = (
export const emptyOrNil = either(isEmpty, isNil)

/**
Interface for parameters for generating a search query. Supported fields are text, from, size, sort, channelName
Interface for parameters for generating a search query. Supported fields are text, search_after, size, sort, channelName
and activeFacets. activeFacets supports audience, certification, type, offered_by, topics, department_name, level,
course_feature_tags and resource_type as nested params
*/
export interface SearchQueryParams {
text?: string
from?: number
search_after?: number[]
size?: number
sort?: SortParam
activeFacets?: Facets
Expand All @@ -176,7 +176,7 @@ const getTypes = (activeFacets: Facets | undefined) => {
*/
export const buildSearchQuery = ({
text,
from,
search_after,
size,
sort,
activeFacets,
Expand All @@ -186,9 +186,13 @@ export const buildSearchQuery = ({
}: SearchQueryParams): Record<string, any> => {
let builder = bodybuilder()

if (!isNil(from)) {
builder = builder.from(from)
if (!isNil(search_after)) {
builder = builder.rawOption(
"search_after",
search_after
)
}

if (!isNil(size)) {
builder = builder.size(size)
}
Expand Down Expand Up @@ -512,26 +516,52 @@ export const buildOrQuery = (
textQuery: Record<string, any> | undefined,
extraClauses: any[]
): Bodybuilder => {
const textFilter = emptyOrNil(textQuery) ? [] : [{ bool: textQuery }]

builder = builder.orQuery("bool", {
filter: {
bool: {
must: [
{
term: {
object_type: searchType
}
},
...extraClauses,
// Add multimatch text query here to filter out non-matching results
...textFilter
]
}
},
// Add multimatch text query here again to score results based on match
...textQuery
})
if(emptyOrNil(textQuery)) {
builder = builder.orQuery("bool", {
filter: {
bool: {
must: [
{
term: {
object_type: searchType
}
},
...extraClauses,
]
}
},
})
} else {
const textFilter = emptyOrNil(textQuery) ? [] : [{ bool: textQuery }]

builder = builder.query('function_score',
{
boost_mode: "replace",
script_score : {
script : {
source: "Math.round(_score*2)"
}
}
},
nested => nested.orQuery("bool", {
filter: {
bool: {
must: [
{
term: {
object_type: searchType
}
},
...extraClauses,
// Add multimatch text query here to filter out non-matching results
...textFilter
]
}
},
// Add multimatch text query here again to score results based on match
...textQuery
})
}
return builder
}

Expand Down

0 comments on commit 45f8b59

Please sign in to comment.