Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
abeglova committed Jun 29, 2023
1 parent b258999 commit ccae5ff
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 58 deletions.
12 changes: 9 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,8 @@ interface CourseSearchResult {
* default form action will be prevented.
*/
onSubmit: (e: PreventableEvent) => void
from: number
from: number
searchAfter: Array<number> | null
updateUI: (newUI: string | null) => void
ui: string | null
}
Expand All @@ -406,6 +407,7 @@ export const useCourseSearch = (
nextFrom: number,
sort?: SortParam | null,
ui?: string | null
searchAfter?: Array<number> | null
) => Promise<void>,
clearSearch: () => void,
aggregations: Aggregations,
Expand All @@ -415,6 +417,7 @@ export const useCourseSearch = (
): CourseSearchResult => {
const [incremental, setIncremental] = useState(false)
const [from, setFrom] = useState(0)
const [searchAfter, setSearchAfter] = useState(null)

const seachUI = useSearchInputs(history)
const {
Expand Down Expand Up @@ -454,7 +457,8 @@ export const useCourseSearch = (
if (!incremental) {
clearSearch()
nextFrom = 0
}
}

setFrom(nextFrom)
setIncremental(incremental)

Expand All @@ -472,13 +476,15 @@ export const useCourseSearch = (
searchFacets.type = LR_TYPE_ALL
}

await runSearch(text, searchFacets, nextFrom, sort, ui)
await runSearch(text, searchFacets, nextFrom, sort, ui, searchAfter)

setLocation(history, { text, activeFacets, sort, ui })
},
[
from,
setFrom,
searchAfter,
setSearchAfter,
setIncremental,
clearSearch,
runSearch,
Expand Down
124 changes: 69 additions & 55 deletions src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export const emptyOrNil = either(isEmpty, isNil)
export interface SearchQueryParams {
text?: string
searchAfter?: number[]
from?: number
size?: number
sort?: SortParam
activeFacets?: Facets
Expand All @@ -177,6 +178,7 @@ const getTypes = (activeFacets: Facets | undefined) => {
export const buildSearchQuery = ({
text,
searchAfter,
from,
size,
sort,
activeFacets,
Expand All @@ -189,10 +191,12 @@ export const buildSearchQuery = ({
if (!isNil(searchAfter)) {
builder = builder.rawOption("search_after", searchAfter)
}

if (!isNil(size)) {
builder = builder.size(size)
}
if (isNil(searchAfter) && !isNil(from)) {
builder = builder.from(from)
}
if (
sort &&
activeFacets &&
Expand Down Expand Up @@ -291,6 +295,8 @@ export const buildLearnQuery = (
facets?: Facets,
aggregations?: Array<string>
): Record<string, any> => {
let orSubqueriesBuilder = bodybuilder()

for (const type of types) {
const queryType = isDoubleQuoted(text) ? "query_string" : "multi_match"
const textQuery = emptyOrNil(text) ?
Expand Down Expand Up @@ -350,23 +356,60 @@ export const buildLearnQuery = (
// Add filters for facets if necessary
const facetClauses = buildFacetSubQuery(facets, builder, type, aggregations)
builder = buildOrQuery(builder, type, textQuery, [])

if (!emptyOrNil(text)) {
orSubqueriesBuilder = buildOrQuery(
orSubqueriesBuilder,
type,
textQuery,
[]
)
} else {
builder = buildOrQuery(builder, type, textQuery, [])
}

builder = builder.rawOption("post_filter", {
bool: {
must: [...facetClauses]
}
})

// Include suggest if search test is not null/empty
if (!emptyOrNil(text)) {
builder = builder.rawOption(
"suggest",
// @ts-expect-error
buildSuggestQuery(text, LEARN_SUGGEST_FIELDS)
)
} else if (facetClauses.length === 0 && equals(types, LR_TYPE_ALL)) {
if (
emptyOrNil(text) &&
facetClauses.length === 0 &&
equals(types, LR_TYPE_ALL)
) {
builder = builder.rawOption("sort", buildDefaultSort())
}
}

if (!emptyOrNil(text)) {
builder = builder.rawOption(
"suggest",
// @ts-expect-error
buildSuggestQuery(text, LEARN_SUGGEST_FIELDS)
)

builder = builder.query("function_score", {
boost_mode: "replace",
script_score: {
script: {
source: "Math.round(_score*2)"
}
},
...orSubqueriesBuilder.build()
})

builder = builder.rawOption("sort", [
{
_score: "desc"
},
{
created: "desc"
}
])
}

return builder.build()
}

Expand Down Expand Up @@ -513,55 +556,26 @@ export const buildOrQuery = (
textQuery: Record<string, any> | undefined,
extraClauses: any[]
): Bodybuilder => {
if (emptyOrNil(textQuery)) {
builder = builder.orQuery("bool", {
filter: {
bool: {
must: [
{
term: {
object_type: searchType
}
},
...extraClauses
]
}
}
})
} else {
const textFilter = emptyOrNil(textQuery) ? [] : [{ bool: textQuery }]
const textFilter = emptyOrNil(textQuery) ? [] : [{ bool: textQuery }]

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

Please sign in to comment.