-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate full-text-search to loki (insert/update/remove + search)
* make full text search available in Collection/Resultset * add an unit test for full text search with Loki * implement insert/remove/update documents and chained search unit test with full text search * make common module with a small plugin system and common types * support elasticsearch 5 and 6 inside the unit test
- Loading branch information
Showing
22 changed files
with
518 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
function getGlobal(): any { | ||
let glob; | ||
(function (global) { | ||
glob = global; | ||
})(typeof global !== "undefined" && global || this); | ||
return glob; | ||
} | ||
|
||
|
||
function create(): void { | ||
const global = getGlobal(); | ||
const sym = Symbol.for("LOKI") as any; | ||
if (global[sym] === undefined) { | ||
global[sym] = { | ||
}; | ||
} | ||
return global[sym]; | ||
} | ||
|
||
export const PLUGINS = create(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import {Loki} from "./loki"; | ||
import {Loki} from "../loki/src/loki"; | ||
|
||
export type ANY = any; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
packages/full-text-search/spec/generic/full_text_search.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* global describe, ddescribe, it, expect */ | ||
import {Loki} from "../../../loki/src/loki"; | ||
import {QueryBuilder} from "../../src/query_builder"; | ||
import {LokiMemoryAdapter} from "../../../loki/src/memory_adapter"; | ||
import {Collection} from "../../../loki/src/collection"; | ||
import {FullTextSearch} from "../../src/full_text_search"; | ||
|
||
describe("full text search", () => { | ||
FullTextSearch.register(); | ||
|
||
interface User { | ||
name: string; | ||
id: number; | ||
} | ||
|
||
let db: Loki; | ||
let coll: Collection; | ||
|
||
beforeEach(() => { | ||
db = new Loki("MyDB"); | ||
coll = db.addCollection<User>("User", {fullTextSearch: [{name: "name"}]}); | ||
|
||
coll.insert([ | ||
{name: "quark", id: 1}, | ||
{name: "quarrk", id: 2}, | ||
{name: "quak", id: 3}, | ||
{name: "quask", id: 4}, | ||
]); | ||
}); | ||
|
||
it("usage", () => { | ||
let query = new QueryBuilder().fuzzy("name", "quak").fuzziness(1).build(); | ||
expect(coll.find({"$fts": query}).length).toBe(3); | ||
}); | ||
|
||
it("chained", () => { | ||
let query = new QueryBuilder().fuzzy("name", "quak").fuzziness(1).build(); | ||
|
||
expect( | ||
coll.find({"$fts": query})).not.toEqual( | ||
coll.find({"id": {"$in": [1, 2, 3]}})); | ||
|
||
expect( | ||
coll | ||
.chain() | ||
.find({"id": {"$in": [1, 2, 3]}}) | ||
.find({"$fts": query}) | ||
.data().length) | ||
.toBe(2); | ||
|
||
expect( | ||
coll | ||
.chain() | ||
.find({"$fts": query}) | ||
.find({"id": {"$in": [1, 2, 3]}}) | ||
.data().length) | ||
.toBe(2); | ||
}); | ||
|
||
it("update", () => { | ||
coll.updateWhere((user: User) => { | ||
return user.name == "quak"; | ||
}, (user: User) => { | ||
user.name = "quaaak"; | ||
return user; | ||
}); | ||
|
||
let query = new QueryBuilder().fuzzy("name", "quak").fuzziness(1).build(); | ||
expect(coll.find({"$fts": query}).length).toBe(2); | ||
}); | ||
|
||
it("remove", () => { | ||
coll.removeWhere((user: User) => { | ||
return user.name == "quak"; | ||
}); | ||
|
||
let query = new QueryBuilder().fuzzy("name", "quak").fuzziness(1).build(); | ||
expect(coll.find({"$fts": query}).length).toBe(2); | ||
}); | ||
|
||
it("clear", () => { | ||
coll.clear(); | ||
|
||
coll.insert([ | ||
{name: "abcd", id: 1}, | ||
{name: "abcde", id: 2}, | ||
{name: "abcdef", id: 3}, | ||
{name: "abcdefg", id: 4}, | ||
]); | ||
|
||
let query = new QueryBuilder().fuzzy("name", "quak").fuzziness(1).build(); | ||
expect(coll.find({"$fts": query}).length).toBe(0); | ||
|
||
query = new QueryBuilder().fuzzy("name", "abcde").fuzziness(1).build(); | ||
expect(coll.find({"$fts": query}).length).toBe(3); | ||
}); | ||
|
||
it("save/load", (done) => { | ||
const adapter = {adapter: new LokiMemoryAdapter()}; | ||
db.initializePersistence(adapter) | ||
.then(() => { | ||
return db.saveDatabase(); | ||
}) | ||
.then(() => { | ||
const db2 = new Loki("MyDB"); | ||
return db2.initializePersistence(adapter) | ||
.then(() => { | ||
return db2.loadDatabase(); | ||
}).then(() => { | ||
const coll2 = db2.getCollection<User>("User"); | ||
let query = new QueryBuilder().fuzzy("name", "quak").fuzziness(1).build(); | ||
expect(coll2.find({"$fts": query}).length).toBe(3); | ||
done(); | ||
}); | ||
}) | ||
.catch(() => { | ||
expect(true).toBe(false); | ||
done(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.