Skip to content

Commit

Permalink
feature: add scoring_options into the options popover on the search p…
Browse files Browse the repository at this point in the history
…layground
  • Loading branch information
skeptrunedev authored and cdxker committed Sep 3, 2024
1 parent 7b91c10 commit 0b417e9
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
24 changes: 24 additions & 0 deletions frontends/search/src/components/ResultsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,32 @@ const ResultsPage = (props: ResultsPageProps) => {
group_size: props.search.debounced.group_size ?? 3,
use_quote_negated_terms: props.search.debounced.useQuoteNegatedTerms,
remove_stop_words: props.search.debounced.removeStopWords,
scoring_options: {},
};

if (
props.search.debounced.scoringOptions?.fulltext_boost?.boost_factor &&
props.search.debounced.scoringOptions?.fulltext_boost?.phrase
) {
requestBody.scoring_options.fulltext_boost = {
boost_factor:
props.search.debounced.scoringOptions.fulltext_boost.boost_factor,
phrase: props.search.debounced.scoringOptions.fulltext_boost.phrase,
};
}
if (
props.search.debounced.scoringOptions?.semantic_boost
?.distance_factor &&
props.search.debounced.scoringOptions?.semantic_boost?.phrase
) {
requestBody.scoring_options.semantic_boost = {
distance_factor:
props.search.debounced.scoringOptions.semantic_boost
.distance_factor,
phrase: props.search.debounced.scoringOptions.semantic_boost.phrase,
};
}

let searchRoute = "";
let groupUnique = false;
if (
Expand Down
107 changes: 107 additions & 0 deletions frontends/search/src/components/SearchForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1485,6 +1485,113 @@ const SearchForm = (props: {
/>
</div>
</Show>
<div class="px-1 font-bold">Scoring Options</div>
<div class="items flex justify-between space-x-2 p-1">
<label>Fulltext boost phrase:</label>
<input
class="w-16 rounded border border-neutral-400 p-0.5 text-black"
type="text"
value={
tempSearchValues().scoringOptions?.fulltext_boost
?.phrase ?? ""
}
onInput={(e) => {
setTempSearchValues((prev) => {
return {
...prev,
scoringOptions: {
...prev.scoringOptions,
fulltext_boost: {
...prev.scoringOptions?.fulltext_boost,
phrase: e.currentTarget.value,
},
},
};
});
}}
/>
</div>
<div class="items flex justify-between space-x-2 p-1">
<label>Fulltext boost factor:</label>
<input
class="w-16 rounded border border-neutral-400 p-0.5 text-black"
type="number"
step="any"
value={
tempSearchValues().scoringOptions?.fulltext_boost
?.boost_factor
}
onChange={(e) => {
setTempSearchValues((prev) => {
return {
...prev,
scoringOptions: {
...prev.scoringOptions,
fulltext_boost: {
...prev.scoringOptions?.fulltext_boost,
boost_factor: parseFloat(
e.currentTarget.value,
),
},
},
};
});
}}
/>
</div>
<div class="items flex justify-between space-x-2 p-1">
<label>Semantic boost phrase:</label>
<input
class="w-16 rounded border border-neutral-400 p-0.5 text-black"
type="text"
value={
tempSearchValues().scoringOptions?.semantic_boost
?.phrase ?? ""
}
onInput={(e) => {
setTempSearchValues((prev) => {
return {
...prev,
scoringOptions: {
...prev.scoringOptions,
semantic_boost: {
...prev.scoringOptions?.semantic_boost,
phrase: e.currentTarget.value,
},
},
};
});
}}
/>
</div>
<div class="items flex justify-between space-x-2 p-1">
<label>Semantic boost distance factor:</label>
<input
class="w-16 rounded border border-neutral-400 p-0.5 text-black"
type="number"
step="any"
value={
tempSearchValues().scoringOptions?.semantic_boost
?.distance_factor
}
onChange={(e) => {
setTempSearchValues((prev) => {
return {
...prev,
scoringOptions: {
...prev.scoringOptions,
semantic_boost: {
...prev.scoringOptions?.semantic_boost,
distance_factor: parseFloat(
e.currentTarget.value,
),
},
},
};
});
}}
/>
</div>
</div>
</PopoverPanel>
</Show>
Expand Down
21 changes: 21 additions & 0 deletions frontends/search/src/hooks/useSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ export interface MultiQuery {
weight: number;
}

export interface FulltextBoost {
phrase?: string;
boost_factor?: number;
}

export interface SemanticBoost {
phrase?: string;
distance_factor?: number;
}

export interface ScoringOptions {
fulltext_boost?: FulltextBoost;
semantic_boost?: SemanticBoost;
}

export function isSortByField(
sortBy: SortByField | SortBySearchType,
): sortBy is SortByField {
Expand Down Expand Up @@ -66,6 +81,7 @@ export interface SearchOptions {
removeStopWords: boolean;
filters: Filters | null;
multiQueries: MultiQuery[];
scoringOptions?: ScoringOptions;
}

const initalState: SearchOptions = {
Expand Down Expand Up @@ -104,6 +120,7 @@ const initalState: SearchOptions = {
jsonb_prefilter: true,
} as Filters,
multiQueries: [],
scoringOptions: undefined,
};

const fromStateToParams = (state: SearchOptions): Params => {
Expand Down Expand Up @@ -136,6 +153,7 @@ const fromStateToParams = (state: SearchOptions): Params => {
removeStopWords: state.removeStopWords.toString(),
filters: JSON.stringify(state.filters),
multiQueries: JSON.stringify(state.multiQueries),
scoringOptions: JSON.stringify(state.scoringOptions),
};
};

Expand Down Expand Up @@ -182,6 +200,9 @@ const fromParamsToState = (
removeStopWords: (params.removeStopWords ?? "false") === "true",
filters: JSON.parse(params.filters ?? "null") as Filters | null,
multiQueries: JSON.parse(params.multiQueries ?? "[]") as MultiQuery[],
scoringOptions: JSON.parse(
params.scoringOptions ?? "null",
) as ScoringOptions,
};
};

Expand Down

0 comments on commit 0b417e9

Please sign in to comment.