Skip to content

Commit

Permalink
Merge pull request #748 from techmatters/gian_CHI-2965
Browse files Browse the repository at this point in the history
chore: Generalized Search - add sort options for empty term and tiebreaker [CHI-2965]
  • Loading branch information
GPaoloni authored Sep 27, 2024
2 parents e748334 + 151404c commit 6d21625
Showing 1 changed file with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const BOOST_FACTORS = {
case: 3,
};
const MIN_SCORE = 0.1;
const MAX_INT = 2147483648 - 1; // 2^31 - 1, the max integer allowed by ElasticSearch integer type

export const FILTER_ALL_CLAUSE: QueryDslQueryContainer[][] = [
[
Expand Down Expand Up @@ -60,7 +61,11 @@ export type DocumentTypeQueryParams = {
[DocumentType.Case]: GenerateTDocQueryParams<DocumentType.Case>;
};

type GenerateTermQueryParams = { type: 'term'; term: string | boolean; boost?: number };
type GenerateTermQueryParams = {
type: 'term';
term: string | boolean | number;
boost?: number;
};
type GenerateRangeQueryParams = {
type: 'range';
ranges: { lt?: string; lte?: string; gt?: string; gte?: string };
Expand Down Expand Up @@ -193,15 +198,22 @@ const generateQueriesFromId = <TDoc extends DocumentType>({
const queries = terms
.map(term => {
// Ignore terms that are not entirely a number, as that breaks term queries against integer fields
if (Number.isNaN(Number(term))) {
if (Number.isNaN(Number(term)) || !Number.isInteger(term)) {
return null;
}

const parsed = Number.parseInt(term, 10);

// Ignore numbers that are greater than maximum supported int
if (parsed > MAX_INT) {
return null;
}

return generateESQuery(
queryWrapper({
documentType,
type: 'term',
term,
term: parsed,
boost: boostFactor * BOOST_FACTORS.id,
field: 'id' as any, // typecast to conform TS, only valid parameters should be accept
parentPath,
Expand Down Expand Up @@ -372,6 +384,10 @@ const generateContactsQuery = ({
highlight: {
fields: { '*': {} },
},
sort:
searchParameters.searchTerm.length === 0
? [{ updatedAt: 'desc' }]
: ['_score', { updatedAt: 'desc' }],
min_score: MIN_SCORE,
from: pagination.start,
size: pagination.limit,
Expand Down Expand Up @@ -483,6 +499,10 @@ const generateCasesQuery = ({
highlight: {
fields: { '*': {} },
},
sort:
searchParameters.searchTerm.length === 0
? [{ updatedAt: 'desc' }]
: ['_score', { updatedAt: 'desc' }],
min_score: MIN_SCORE,
from: pagination.start,
size: pagination.limit,
Expand Down

0 comments on commit 6d21625

Please sign in to comment.