From b258999f6c6e6da87b127e1814f04fd5b82112bf Mon Sep 17 00:00:00 2001 From: Anastasia Beglova Date: Mon, 26 Jun 2023 15:13:11 -0400 Subject: [PATCH 1/5] commit --- src/search.ts | 74 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 52 insertions(+), 22 deletions(-) diff --git a/src/search.ts b/src/search.ts index 6d4cf79..e4b7a80 100644 --- a/src/search.ts +++ b/src/search.ts @@ -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, 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 - from?: number + searchAfter?: number[] size?: number sort?: SortParam activeFacets?: Facets @@ -176,7 +176,7 @@ const getTypes = (activeFacets: Facets | undefined) => { */ export const buildSearchQuery = ({ text, - from, + searchAfter, size, sort, activeFacets, @@ -186,9 +186,10 @@ export const buildSearchQuery = ({ }: SearchQueryParams): Record => { 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) } @@ -512,26 +513,55 @@ export const buildOrQuery = ( textQuery: Record | undefined, extraClauses: any[] ): Bodybuilder => { - const textFilter = emptyOrNil(textQuery) ? [] : [{ bool: 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.orQuery("bool", { - filter: { - bool: { - must: [ - { - term: { - object_type: searchType + 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 + ] } }, - ...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 - }) + // Add multimatch text query here again to score results based on match + ...textQuery + }) + ) + } return builder } From 2ceef3f08659d9aa4a926bec13a217b097627dba Mon Sep 17 00:00:00 2001 From: Anastasia Beglova Date: Tue, 27 Jun 2023 09:50:59 -0400 Subject: [PATCH 2/5] fix --- src/index.ts | 17 +++++-- src/search.ts | 124 ++++++++++++++++++++++++++++---------------------- 2 files changed, 81 insertions(+), 60 deletions(-) diff --git a/src/index.ts b/src/index.ts index 3e8842e..4a986dc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -394,7 +394,8 @@ interface CourseSearchResult { * default form action will be prevented. */ onSubmit: (e: PreventableEvent) => void - from: number + from: number + searchAfter: Array | null updateUI: (newUI: string | null) => void ui: string | null } @@ -405,7 +406,8 @@ export const useCourseSearch = ( searchFacets: Facets, nextFrom: number, sort?: SortParam | null, - ui?: string | null + ui?: string | null, + searchAfter?: Array | null ) => Promise, clearSearch: () => void, aggregations: Aggregations, @@ -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 { @@ -454,7 +457,8 @@ export const useCourseSearch = ( if (!incremental) { clearSearch() nextFrom = 0 - } + } + setFrom(nextFrom) setIncremental(incremental) @@ -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, @@ -589,6 +595,7 @@ export const useCourseSearch = ( onSubmit, from, updateUI, - ui + ui, + searchAfter } } diff --git a/src/search.ts b/src/search.ts index e4b7a80..b0f56d0 100644 --- a/src/search.ts +++ b/src/search.ts @@ -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 @@ -177,6 +178,7 @@ const getTypes = (activeFacets: Facets | undefined) => { export const buildSearchQuery = ({ text, searchAfter, + from, size, sort, activeFacets, @@ -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 && @@ -291,6 +295,8 @@ export const buildLearnQuery = ( facets?: Facets, aggregations?: Array ): Record => { + let orSubqueriesBuilder = bodybuilder() + for (const type of types) { const queryType = isDoubleQuoted(text) ? "query_string" : "multi_match" const textQuery = emptyOrNil(text) ? @@ -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() } @@ -513,55 +556,26 @@ export const buildOrQuery = ( textQuery: Record | 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 } From 6d16c88c7726d627685a76480abfc1984a3757a2 Mon Sep 17 00:00:00 2001 From: Anastasia Beglova Date: Fri, 30 Jun 2023 10:45:26 -0400 Subject: [PATCH 3/5] fix --- src/search.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/search.ts b/src/search.ts index b0f56d0..3d46606 100644 --- a/src/search.ts +++ b/src/search.ts @@ -355,7 +355,6 @@ 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( From bad7e797407f820072f369813c4855aa26c9af77 Mon Sep 17 00:00:00 2001 From: Anastasia Beglova Date: Fri, 30 Jun 2023 12:57:49 -0400 Subject: [PATCH 4/5] fix --- src/search.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/search.ts b/src/search.ts index 3d46606..5d898c7 100644 --- a/src/search.ts +++ b/src/search.ts @@ -389,14 +389,16 @@ export const buildLearnQuery = ( 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("query", { + function_score: { + boost_mode: "replace", + script_score: { + script: { + source: "Math.round(_score*2)" + } + }, + query: orSubqueriesBuilder.build() + } }) builder = builder.rawOption("sort", [ From a2677df43a8e81455da63dc65c299fb724771c66 Mon Sep 17 00:00:00 2001 From: Anastasia Beglova Date: Fri, 30 Jun 2023 13:08:41 -0400 Subject: [PATCH 5/5] Revert "fix" This reverts commit bad7e797407f820072f369813c4855aa26c9af77. --- src/search.ts | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/search.ts b/src/search.ts index 5d898c7..3d46606 100644 --- a/src/search.ts +++ b/src/search.ts @@ -389,16 +389,14 @@ export const buildLearnQuery = ( buildSuggestQuery(text, LEARN_SUGGEST_FIELDS) ) - builder = builder.rawOption("query", { - function_score: { - boost_mode: "replace", - script_score: { - script: { - source: "Math.round(_score*2)" - } - }, - query: orSubqueriesBuilder.build() - } + builder = builder.query("function_score", { + boost_mode: "replace", + script_score: { + script: { + source: "Math.round(_score*2)" + } + }, + ...orSubqueriesBuilder.build() }) builder = builder.rawOption("sort", [