From 25112c2b319ff622ab8d13378d378b2d58fabe1e Mon Sep 17 00:00:00 2001 From: Anastasia Beglova Date: Tue, 27 Jun 2023 09:50:59 -0400 Subject: [PATCH] fix --- src/search.ts | 118 ++++++++++++++++++++++++++------------------------ 1 file changed, 61 insertions(+), 57 deletions(-) diff --git a/src/search.ts b/src/search.ts index e4b7a80..d43afb3 100644 --- a/src/search.ts +++ b/src/search.ts @@ -189,7 +189,6 @@ export const buildSearchQuery = ({ if (!isNil(searchAfter)) { builder = builder.rawOption("search_after", searchAfter) } - if (!isNil(size)) { builder = builder.size(size) } @@ -291,6 +290,10 @@ export const buildLearnQuery = ( facets?: Facets, aggregations?: Array ): Record => { + if (!emptyOrNil(text)) { + const orSubqueriesBuilder = bodybuilder() + } + for (const type of types) { const queryType = isDoubleQuoted(text) ? "query_string" : "multi_match" const textQuery = emptyOrNil(text) ? @@ -350,22 +353,52 @@ 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)) { - 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" + } + ]) + } else if (facetClauses.length === 0 && equals(types, LR_TYPE_ALL)) { + builder = builder.rawOption("sort", buildDefaultSort()) } return builder.build() } @@ -513,55 +546,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 }