Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
abeglova committed Jun 27, 2023
1 parent b258999 commit 25112c2
Showing 1 changed file with 61 additions and 57 deletions.
118 changes: 61 additions & 57 deletions src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ export const buildSearchQuery = ({
if (!isNil(searchAfter)) {
builder = builder.rawOption("search_after", searchAfter)
}

if (!isNil(size)) {
builder = builder.size(size)
}
Expand Down Expand Up @@ -291,6 +290,10 @@ export const buildLearnQuery = (
facets?: Facets,
aggregations?: Array<string>
): Record<string, any> => {
if (!emptyOrNil(text)) {
const orSubqueriesBuilder = bodybuilder()

Check warning on line 294 in src/search.ts

View workflow job for this annotation

GitHub Actions / javascript-tests

'orSubqueriesBuilder' is assigned a value but never used
}

for (const type of types) {
const queryType = isDoubleQuoted(text) ? "query_string" : "multi_match"
const textQuery = emptyOrNil(text) ?
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -513,55 +546,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 25112c2

Please sign in to comment.