Skip to content

Commit 0a5b946

Browse files
authored
feat(loki): make Resultset and DynamicView sortable by full-text-search scoring (#45)
* refactor sort priority of dynamic view as enum
1 parent 2f3b718 commit 0a5b946

File tree

5 files changed

+136
-53
lines changed

5 files changed

+136
-53
lines changed

config/karma.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module.exports = function (config) {
3737
loader: "tslint-loader",
3838
exclude: /node_modules/,
3939
options: {
40-
failOnHint: true,
40+
failOnHint: false,
4141
configFile: path.join("config", "tslint.json"),
4242
}
4343
},

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

+35-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe("full text search", () => {
1414
}
1515

1616
let db: Loki;
17-
let coll: Collection;
17+
let coll: Collection<User>;
1818

1919
beforeEach(() => {
2020
db = new Loki("MyDB");
@@ -95,6 +95,40 @@ describe("full text search", () => {
9595
expect(coll.find({"$fts": query}).length).toBe(3);
9696
});
9797

98+
it("sort", () => {
99+
let query = new QueryBuilder().fuzzy("name", "quak").fuzziness(3).build();
100+
101+
expect(coll.chain().sortByScoring).toThrowAnyError();
102+
103+
let res = coll.chain().find({"$fts": query});
104+
expect(res.data().length).toBe(4);
105+
106+
const unsorted = res.data();
107+
const sorted_desc = res.sortByScoring().data();
108+
const sorted_asc = res.sortByScoring(true).data();
109+
110+
expect(unsorted.length).toBe(sorted_desc.length);
111+
expect(sorted_desc.length).toBe(sorted_asc.length);
112+
expect(unsorted).not.toEqual(sorted_desc);
113+
expect(unsorted).not.toEqual(sorted_asc);
114+
115+
expect(sorted_desc[0].name).toBe("quak");
116+
expect(sorted_desc[3].name).toBe("quarrk");
117+
118+
expect(sorted_asc[0].name).toBe("quarrk");
119+
expect(sorted_asc[3].name).toBe("quak");
120+
121+
// With dynamic view.
122+
const dv = coll.addDynamicView("MyScoringView");
123+
expect(dv.data()).toEqual(unsorted);
124+
125+
expect(dv.applySortByScoring).toThrowAnyError();
126+
dv.applyFind({"$fts": query});
127+
128+
expect(dv.applySortByScoring().data()).toEqual(sorted_desc);
129+
expect(dv.applySortByScoring(true).data()).toEqual(sorted_asc);
130+
});
131+
98132
it("save/load", (done) => {
99133
const adapter = {adapter: new LokiMemoryAdapter()};
100134
db.initializePersistence(adapter)

packages/loki/src/collection.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ export class Collection<E extends object = object> extends LokiEventEmitter {
650650
* @param {string} name - name of dynamic view to add
651651
* @param {object} options - (optional) options to configure dynamic view with
652652
* @param {boolean} [options.persistent=false] - indicates if view is to main internal results array in 'resultdata'
653-
* @param {string} [options.sortPriority='passive'] - 'passive' (sorts performed on call to data) or 'active' (after updates)
653+
* @param {string} [options.sortPriority=SortPriority.PASSIVE] - the sort priority
654654
* @param {number} options.minRebuildInterval - minimum rebuild interval (need clarification to docs here)
655655
* @returns {DynamicView} reference to the dynamic view added
656656
**/

0 commit comments

Comments
 (0)