Skip to content

Commit 7ea17c0

Browse files
authored
feat(full-text-search): return score result as an equivalent array to result set data (#69)
1 parent da06836 commit 7ea17c0

File tree

6 files changed

+19
-15
lines changed

6 files changed

+19
-15
lines changed

packages/full-text-search/spec/generic/full_text_search.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ describe("full-text search", () => {
110110
const sorted_desc = res.sortByScoring().data();
111111
const sorted_asc = res.sortByScoring(true).data();
112112

113-
expect(Object.keys(res.getScoring())).toEqual(Object.keys(unsorted));
113+
expect(res.getScoring()).toBeArrayOfSize(unsorted.length);
114114

115115
expect(unsorted.length).toBe(sorted_desc.length);
116116
expect(sorted_desc.length).toBe(sorted_asc.length);
@@ -130,7 +130,7 @@ describe("full-text search", () => {
130130
expect(dv.applySortByScoring).toThrowAnyError();
131131
dv.applyFind({"$fts": query});
132132

133-
expect(Object.keys(dv.getScoring())).toEqual(Object.keys(unsorted));
133+
expect(dv.getScoring()).toBeArrayOfSize(unsorted.length);
134134

135135
expect(dv.applySortByScoring().data()).toEqual(sorted_desc);
136136
expect(dv.applySortByScoring(true).data()).toEqual(sorted_asc);

packages/full-text-search/src/full_text_search.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class FullTextSearch {
7272
}
7373
}
7474

75-
public search(query: Query): Scorer.ScoreResult {
75+
public search(query: Query): Scorer.ScoreResults {
7676
return this._idxSearcher.search(query);
7777
}
7878

packages/full-text-search/src/index_searcher.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ export class IndexSearcher {
2525
this._scorer = new Scorer(this._invIdxs);
2626
}
2727

28-
public search(query: Query): Scorer.ScoreResult {
28+
public search(query: Query): Scorer.ScoreResults {
2929
let queryResults = this._recursive(query.query, true);
3030

3131
// Do final scoring.
3232
if (query.final_scoring !== undefined ? query.final_scoring : true) {
3333
return this._scorer.finalScore(query, queryResults);
3434
}
3535

36-
const result: Scorer.ScoreResult = {};
36+
const result: Scorer.ScoreResults = {};
3737
for (const key of queryResults.keys()) {
3838
result[key] = {score: 1};
3939
}

packages/full-text-search/src/scorer.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ export class Scorer {
4848
return queryResults;
4949
}
5050

51-
public finalScore(query: Query, queryResults: Scorer.QueryResults): Scorer.ScoreResult {
52-
const result: Scorer.ScoreResult = {};
51+
public finalScore(query: Query, queryResults: Scorer.QueryResults): Scorer.ScoreResults {
52+
const result: Scorer.ScoreResults = {};
5353
const k1 = query.bm25 !== undefined ? query.bm25.k1 : 1.2;
5454
const b = query.bm25 !== undefined ? query.bm25.b : 0.75;
5555
const explain = query.explain !== undefined ? query.explain : false;
@@ -177,6 +177,6 @@ export namespace Scorer {
177177
}
178178

179179
export type ScoreExplanation = BM25Explanation | ConstantExplanation;
180-
181-
export type ScoreResult = Dict<{ score: number, explanation?: ScoreExplanation[] }>;
180+
export type ScoreResult = { score: number, explanation?: ScoreExplanation[] };
181+
export type ScoreResults = Dict<ScoreResult>;
182182
}

packages/loki/src/dynamic_view.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,9 @@ export class DynamicView<TData extends object = object, TNested extends object =
297297

298298
/**
299299
* Returns the scoring of the last full-text-search.
300-
* @returns {ScoreResult}
300+
* @returns {ScoreResult[]}
301301
*/
302-
public getScoring(): Scorer.ScoreResult {
302+
public getScoring(): Scorer.ScoreResult[] {
303303
return this._resultSet.getScoring();
304304
}
305305

packages/loki/src/result_set.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ export class ResultSet<TData extends object = object, TNested extends object = o
293293
public _filteredRows: number[];
294294
public _filterInitialized: boolean;
295295
// Holds the scoring result of the last full-text search.
296-
private _scoring: Scorer.ScoreResult;
296+
private _scoring: Scorer.ScoreResults;
297297

298298
/**
299299
* Constructor.
@@ -635,13 +635,17 @@ export class ResultSet<TData extends object = object, TNested extends object = o
635635

636636
/**
637637
* Returns the scoring of the last full-text-search.
638-
* @returns {ScoreResult}
638+
* @returns {ScoreResult[]}
639639
*/
640-
public getScoring(): Scorer.ScoreResult {
640+
public getScoring(): Scorer.ScoreResult[] {
641641
if (this._scoring === null) {
642642
throw new Error("No scoring available");
643643
}
644-
return this._scoring;
644+
const scoring: Scorer.ScoreResult[] = [];
645+
for (let i = 0; i < this._filteredRows.length; i++) {
646+
scoring.push(this._scoring[this._filteredRows[i]]);
647+
}
648+
return scoring;
645649
}
646650

647651
/**

0 commit comments

Comments
 (0)