|
1 | | -import {InvertedIndex} from "./inverted_index"; |
2 | | -import {IndexSearcher} from "./index_searcher"; |
3 | | -import {Dict} from "../../common/types"; |
4 | | -import {PLUGINS} from "../../common/plugin"; |
5 | | -import {Query} from "./query_types"; |
6 | | -import {Scorer} from "./scorer"; |
7 | | -import {Analyzer} from "./analyzer/analyzer"; |
8 | | -import {Serialization} from "../../loki/src/serialization/migration"; |
| 1 | +namespace LokiDB.FullTextSearch { |
9 | 2 |
|
10 | | -export class FullTextSearch { |
11 | | - /// The id field of each document. |
12 | | - private _id: string; |
13 | | - /// Set of ids of all indexed documents. |
14 | | - private _docs: Set<InvertedIndex.DocumentIndex>; |
15 | | - private _idxSearcher: IndexSearcher; |
16 | | - private _invIdxs: Dict<InvertedIndex> = {}; |
| 3 | + export class FullTextSearch { |
| 4 | + /// The id field of each document. |
| 5 | + private _id: string; |
| 6 | + /// Set of ids of all indexed documents. |
| 7 | + private _docs: Set<DocumentIndex>; |
| 8 | + private _idxSearcher: IndexSearcher; |
| 9 | + private _invIdxs: Dict<InvertedIndex> = {}; |
17 | 10 |
|
18 | | - /** |
19 | | - * Registers the full-text search as plugin. |
20 | | - */ |
21 | | - public static register(): void { |
22 | | - PLUGINS["FullTextSearch"] = FullTextSearch; |
23 | | - } |
| 11 | + /** |
| 12 | + * Registers the full-text search as plugin. |
| 13 | + */ |
| 14 | + public static register(): void { |
| 15 | + PLUGINS["FullTextSearch"] = FullTextSearch; |
| 16 | + } |
24 | 17 |
|
25 | | - /** |
26 | | - * Initialize the full-text search for the given fields. |
27 | | - * @param {object[]} fieldOptions - the field options |
28 | | - * @param {string} fieldOptions.field - the name of the property field |
29 | | - * @param {boolean=true} fieldOptions.store - flag to indicate if the full-text search should be stored on serialization or |
30 | | - * rebuild on deserialization |
31 | | - * @param {boolean=true} fieldOptions.optimizeChanges - flag to optimize updating and deleting of documents |
32 | | - * (requires more memory but performs faster) |
33 | | - * @param {Analyzer} fieldOptions.analyzer - an analyzer for the field |
34 | | - * @param {string} [id] - the property name of the document index |
35 | | - */ |
36 | | - constructor(fieldOptions: FullTextSearch.FieldOptions[] = [], id?: string) { |
37 | | - // Create an inverted index for each field. |
38 | | - for (let i = 0; i < fieldOptions.length; i++) { |
39 | | - let fieldOption = fieldOptions[i]; |
40 | | - this._invIdxs[fieldOption.field] = new InvertedIndex(fieldOption); |
| 18 | + /** |
| 19 | + * Initialize the full-text search for the given fields. |
| 20 | + * @param {object[]} fieldOptions - the field options |
| 21 | + * @param {string} fieldOptions.field - the name of the property field |
| 22 | + * @param {boolean=true} fieldOptions.store - flag to indicate if the full-text search should be stored on serialization or |
| 23 | + * rebuild on deserialization |
| 24 | + * @param {boolean=true} fieldOptions.optimizeChanges - flag to optimize updating and deleting of documents |
| 25 | + * (requires more memory but performs faster) |
| 26 | + * @param {Analyzer} fieldOptions.analyzer - an analyzer for the field |
| 27 | + * @param {string} [id] - the property name of the document index |
| 28 | + */ |
| 29 | + constructor(fieldOptions: FullTextSearch.FieldOptions[] = [], id?: string) { |
| 30 | + // Create an inverted index for each field. |
| 31 | + for (let i = 0; i < fieldOptions.length; i++) { |
| 32 | + let fieldOption = fieldOptions[i]; |
| 33 | + this._invIdxs[fieldOption.field] = new InvertedIndex(fieldOption); |
| 34 | + } |
| 35 | + this._id = id; |
| 36 | + this._docs = new Set(); |
| 37 | + this._idxSearcher = new IndexSearcher(this._invIdxs, this._docs); |
41 | 38 | } |
42 | | - this._id = id; |
43 | | - this._docs = new Set(); |
44 | | - this._idxSearcher = new IndexSearcher(this._invIdxs, this._docs); |
45 | | - } |
46 | 39 |
|
47 | | - public addDocument(doc: object, id: InvertedIndex.DocumentIndex = doc[this._id]): void { |
48 | | - let fieldNames = Object.keys(this._invIdxs); |
49 | | - for (let i = 0, fieldName; i < fieldNames.length, fieldName = fieldNames[i]; i++) { |
50 | | - if (doc[fieldName] !== undefined) { |
51 | | - this._invIdxs[fieldName].insert(doc[fieldName], id); |
| 40 | + public addDocument(doc: object, id: DocumentIndex = doc[this._id]): void { |
| 41 | + let fieldNames = Object.keys(this._invIdxs); |
| 42 | + for (let i = 0, fieldName; i < fieldNames.length, fieldName = fieldNames[i]; i++) { |
| 43 | + if (doc[fieldName] !== undefined) { |
| 44 | + this._invIdxs[fieldName].insert(doc[fieldName], id); |
| 45 | + } |
52 | 46 | } |
| 47 | + this._docs.add(id); |
| 48 | + this._idxSearcher.setDirty(); |
53 | 49 | } |
54 | | - this._docs.add(id); |
55 | | - this._idxSearcher.setDirty(); |
56 | | - } |
57 | 50 |
|
58 | | - public removeDocument(doc: object, id: InvertedIndex.DocumentIndex = doc[this._id]): void { |
59 | | - let fieldNames = Object.keys(this._invIdxs); |
60 | | - for (let i = 0; i < fieldNames.length; i++) { |
61 | | - this._invIdxs[fieldNames[i]].remove(id); |
| 51 | + public removeDocument(doc: object, id: DocumentIndex = doc[this._id]): void { |
| 52 | + let fieldNames = Object.keys(this._invIdxs); |
| 53 | + for (let i = 0; i < fieldNames.length; i++) { |
| 54 | + this._invIdxs[fieldNames[i]].remove(id); |
| 55 | + } |
| 56 | + this._docs.delete(id); |
| 57 | + this._idxSearcher.setDirty(); |
62 | 58 | } |
63 | | - this._docs.delete(id); |
64 | | - this._idxSearcher.setDirty(); |
65 | | - } |
66 | 59 |
|
67 | | - public updateDocument(doc: object, id: InvertedIndex.DocumentIndex = doc[this._id]): void { |
68 | | - this.removeDocument(doc, id); |
69 | | - this.addDocument(doc, id); |
70 | | - } |
| 60 | + public updateDocument(doc: object, id: DocumentIndex = doc[this._id]): void { |
| 61 | + this.removeDocument(doc, id); |
| 62 | + this.addDocument(doc, id); |
| 63 | + } |
71 | 64 |
|
72 | | - public clear(): void { |
73 | | - for (let id of this._docs) { |
74 | | - this.removeDocument(null, id); |
| 65 | + public clear(): void { |
| 66 | + for (let id of this._docs) { |
| 67 | + this.removeDocument(null, id); |
| 68 | + } |
75 | 69 | } |
76 | | - } |
77 | 70 |
|
78 | | - public search(query: Query): Scorer.ScoreResults { |
79 | | - return this._idxSearcher.search(query); |
80 | | - } |
| 71 | + public search(query: Query): Scorer.ScoreResults { |
| 72 | + return this._idxSearcher.search(query); |
| 73 | + } |
81 | 74 |
|
82 | | - public toJSON(): Serialization.FullTextSearch { |
83 | | - let serialized = {id: this._id, ii: {}}; |
84 | | - let fieldNames = Object.keys(this._invIdxs); |
85 | | - for (let i = 0; i < fieldNames.length; i++) { |
86 | | - const fieldName = fieldNames[i]; |
87 | | - serialized.ii[fieldName] = this._invIdxs[fieldName].toJSON(); |
| 75 | + public toJSON(): Serialization.FullTextSearch { |
| 76 | + let serialized = {id: this._id, ii: {}}; |
| 77 | + let fieldNames = Object.keys(this._invIdxs); |
| 78 | + for (let i = 0; i < fieldNames.length; i++) { |
| 79 | + const fieldName = fieldNames[i]; |
| 80 | + serialized.ii[fieldName] = this._invIdxs[fieldName].toJSON(); |
| 81 | + } |
| 82 | + return serialized; |
88 | 83 | } |
89 | | - return serialized; |
90 | | - } |
91 | 84 |
|
92 | | - public static fromJSONObject(serialized: Serialization.FullTextSearch, analyzers: Dict<Analyzer> = {}): FullTextSearch { |
93 | | - let fts = new FullTextSearch([], serialized.id); |
94 | | - let fieldNames = Object.keys(serialized.ii); |
95 | | - for (let i = 0; i < fieldNames.length; i++) { |
96 | | - const fieldName = fieldNames[i]; |
97 | | - fts._invIdxs[fieldName] = InvertedIndex.fromJSONObject(serialized.ii[fieldName], analyzers[fieldName]); |
| 85 | + public static fromJSONObject(serialized: Serialization.FullTextSearch, analyzers: Dict<Analyzer> = {}): FullTextSearch { |
| 86 | + let fts = new FullTextSearch([], serialized.id); |
| 87 | + let fieldNames = Object.keys(serialized.ii); |
| 88 | + for (let i = 0; i < fieldNames.length; i++) { |
| 89 | + const fieldName = fieldNames[i]; |
| 90 | + fts._invIdxs[fieldName] = InvertedIndex.fromJSONObject(serialized.ii[fieldName], analyzers[fieldName]); |
| 91 | + } |
| 92 | + return fts; |
98 | 93 | } |
99 | | - return fts; |
100 | 94 | } |
101 | | -} |
102 | 95 |
|
103 | | -export namespace FullTextSearch { |
104 | | - export interface FieldOptions extends InvertedIndex.FieldOptions { |
105 | | - field: string; |
| 96 | + export namespace FullTextSearch { |
| 97 | + export interface FieldOptions extends InvertedIndex.FieldOptions { |
| 98 | + field: string; |
| 99 | + } |
106 | 100 | } |
107 | 101 | } |
0 commit comments