Skip to content

Commit ccae5ff

Browse files
committed
fix
1 parent b258999 commit ccae5ff

File tree

2 files changed

+78
-58
lines changed

2 files changed

+78
-58
lines changed

src/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,8 @@ interface CourseSearchResult {
394394
* default form action will be prevented.
395395
*/
396396
onSubmit: (e: PreventableEvent) => void
397-
from: number
397+
from: number
398+
searchAfter: Array<number> | null
398399
updateUI: (newUI: string | null) => void
399400
ui: string | null
400401
}
@@ -406,6 +407,7 @@ export const useCourseSearch = (
406407
nextFrom: number,
407408
sort?: SortParam | null,
408409
ui?: string | null
410+
searchAfter?: Array<number> | null
409411
) => Promise<void>,
410412
clearSearch: () => void,
411413
aggregations: Aggregations,
@@ -415,6 +417,7 @@ export const useCourseSearch = (
415417
): CourseSearchResult => {
416418
const [incremental, setIncremental] = useState(false)
417419
const [from, setFrom] = useState(0)
420+
const [searchAfter, setSearchAfter] = useState(null)
418421

419422
const seachUI = useSearchInputs(history)
420423
const {
@@ -454,7 +457,8 @@ export const useCourseSearch = (
454457
if (!incremental) {
455458
clearSearch()
456459
nextFrom = 0
457-
}
460+
}
461+
458462
setFrom(nextFrom)
459463
setIncremental(incremental)
460464

@@ -472,13 +476,15 @@ export const useCourseSearch = (
472476
searchFacets.type = LR_TYPE_ALL
473477
}
474478

475-
await runSearch(text, searchFacets, nextFrom, sort, ui)
479+
await runSearch(text, searchFacets, nextFrom, sort, ui, searchAfter)
476480

477481
setLocation(history, { text, activeFacets, sort, ui })
478482
},
479483
[
480484
from,
481485
setFrom,
486+
searchAfter,
487+
setSearchAfter,
482488
setIncremental,
483489
clearSearch,
484490
runSearch,

src/search.ts

Lines changed: 69 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ export const emptyOrNil = either(isEmpty, isNil)
155155
export interface SearchQueryParams {
156156
text?: string
157157
searchAfter?: number[]
158+
from?: number
158159
size?: number
159160
sort?: SortParam
160161
activeFacets?: Facets
@@ -177,6 +178,7 @@ const getTypes = (activeFacets: Facets | undefined) => {
177178
export const buildSearchQuery = ({
178179
text,
179180
searchAfter,
181+
from,
180182
size,
181183
sort,
182184
activeFacets,
@@ -189,10 +191,12 @@ export const buildSearchQuery = ({
189191
if (!isNil(searchAfter)) {
190192
builder = builder.rawOption("search_after", searchAfter)
191193
}
192-
193194
if (!isNil(size)) {
194195
builder = builder.size(size)
195196
}
197+
if (isNil(searchAfter) && !isNil(from)) {
198+
builder = builder.from(from)
199+
}
196200
if (
197201
sort &&
198202
activeFacets &&
@@ -291,6 +295,8 @@ export const buildLearnQuery = (
291295
facets?: Facets,
292296
aggregations?: Array<string>
293297
): Record<string, any> => {
298+
let orSubqueriesBuilder = bodybuilder()
299+
294300
for (const type of types) {
295301
const queryType = isDoubleQuoted(text) ? "query_string" : "multi_match"
296302
const textQuery = emptyOrNil(text) ?
@@ -350,23 +356,60 @@ export const buildLearnQuery = (
350356
// Add filters for facets if necessary
351357
const facetClauses = buildFacetSubQuery(facets, builder, type, aggregations)
352358
builder = buildOrQuery(builder, type, textQuery, [])
359+
360+
if (!emptyOrNil(text)) {
361+
orSubqueriesBuilder = buildOrQuery(
362+
orSubqueriesBuilder,
363+
type,
364+
textQuery,
365+
[]
366+
)
367+
} else {
368+
builder = buildOrQuery(builder, type, textQuery, [])
369+
}
370+
353371
builder = builder.rawOption("post_filter", {
354372
bool: {
355373
must: [...facetClauses]
356374
}
357375
})
358376

359-
// Include suggest if search test is not null/empty
360-
if (!emptyOrNil(text)) {
361-
builder = builder.rawOption(
362-
"suggest",
363-
// @ts-expect-error
364-
buildSuggestQuery(text, LEARN_SUGGEST_FIELDS)
365-
)
366-
} else if (facetClauses.length === 0 && equals(types, LR_TYPE_ALL)) {
377+
if (
378+
emptyOrNil(text) &&
379+
facetClauses.length === 0 &&
380+
equals(types, LR_TYPE_ALL)
381+
) {
367382
builder = builder.rawOption("sort", buildDefaultSort())
368383
}
369384
}
385+
386+
if (!emptyOrNil(text)) {
387+
builder = builder.rawOption(
388+
"suggest",
389+
// @ts-expect-error
390+
buildSuggestQuery(text, LEARN_SUGGEST_FIELDS)
391+
)
392+
393+
builder = builder.query("function_score", {
394+
boost_mode: "replace",
395+
script_score: {
396+
script: {
397+
source: "Math.round(_score*2)"
398+
}
399+
},
400+
...orSubqueriesBuilder.build()
401+
})
402+
403+
builder = builder.rawOption("sort", [
404+
{
405+
_score: "desc"
406+
},
407+
{
408+
created: "desc"
409+
}
410+
])
411+
}
412+
370413
return builder.build()
371414
}
372415

@@ -513,55 +556,26 @@ export const buildOrQuery = (
513556
textQuery: Record<string, any> | undefined,
514557
extraClauses: any[]
515558
): Bodybuilder => {
516-
if (emptyOrNil(textQuery)) {
517-
builder = builder.orQuery("bool", {
518-
filter: {
519-
bool: {
520-
must: [
521-
{
522-
term: {
523-
object_type: searchType
524-
}
525-
},
526-
...extraClauses
527-
]
528-
}
529-
}
530-
})
531-
} else {
532-
const textFilter = emptyOrNil(textQuery) ? [] : [{ bool: textQuery }]
559+
const textFilter = emptyOrNil(textQuery) ? [] : [{ bool: textQuery }]
533560

534-
builder = builder.query(
535-
"function_score",
536-
{
537-
boost_mode: "replace",
538-
script_score: {
539-
script: {
540-
source: "Math.round(_score*2)"
541-
}
542-
}
543-
},
544-
(nested : Bodybuilder) =>
545-
nested.orQuery("bool", {
546-
filter: {
547-
bool: {
548-
must: [
549-
{
550-
term: {
551-
object_type: searchType
552-
}
553-
},
554-
...extraClauses,
555-
// Add multimatch text query here to filter out non-matching results
556-
...textFilter
557-
]
561+
builder = builder.orQuery("bool", {
562+
filter: {
563+
bool: {
564+
must: [
565+
{
566+
term: {
567+
object_type: searchType
558568
}
559569
},
560-
// Add multimatch text query here again to score results based on match
561-
...textQuery
562-
})
563-
)
564-
}
570+
...extraClauses,
571+
// Add multimatch text query here to filter out non-matching results
572+
...textFilter
573+
]
574+
}
575+
},
576+
// Add multimatch text query here again to score results based on match
577+
...textQuery
578+
})
565579
return builder
566580
}
567581

0 commit comments

Comments
 (0)