Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ab/duplicate results #57

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 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 @@ -405,7 +406,8 @@ export const useCourseSearch = (
searchFacets: Facets,
nextFrom: number,
sort?: SortParam | null,
ui?: string | 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 Expand Up @@ -589,6 +595,7 @@ export const useCourseSearch = (
onSubmit,
from,
updateUI,
ui
ui,
searchAfter
}
}
67 changes: 55 additions & 12 deletions src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +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, searchAfter, 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
searchAfter?: number[]
from?: number
size?: number
sort?: SortParam
Expand All @@ -176,6 +177,7 @@ const getTypes = (activeFacets: Facets | undefined) => {
*/
export const buildSearchQuery = ({
text,
searchAfter,
from,
size,
sort,
Expand All @@ -186,12 +188,15 @@ export const buildSearchQuery = ({
}: SearchQueryParams): Record<string, any> => {
let builder = bodybuilder()

if (!isNil(from)) {
builder = builder.from(from)
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 @@ -290,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 @@ -348,24 +355,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