-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchEngine.js
105 lines (99 loc) · 2.94 KB
/
SearchEngine.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const sqlite3 = require("sqlite3").verbose();
const path = require("path");
class SearchEngine {
constructor() {
this.databases = {
dbDict: "strong_dict.db",
dbPure: "strong_pure.db",
dbWords: "strong_words.db",
dbAcrostics: "kjv_acrostics.db",
dbBooks: "kjv_books.db",
dbCitations: "kjv_citations.db",
dbChapters: "kjv_chapters.db",
dbKjvPure: "kjv_pure.db",
};
this.dbConnections = {};
for (const key in this.databases) {
this.dbConnections[key] = new sqlite3.Database(
path.join(__dirname, this.databases[key]),
sqlite3.OPEN_READONLY,
(err) => {
if (err) console.error(`Error opening ${key} database:`, err.message);
}
);
}
}
async queryDatabase(db, query, params) {
return new Promise((resolve, reject) => {
this.dbConnections[db].all(query, params, (err, rows) => {
if (err) {
console.error(`Error running query in ${db}:`, err.message);
reject(err);
} else {
resolve(rows);
}
});
});
}
async search(term) {
const searchTerm = `%${term}%`;
try {
const results = await Promise.all([
this.queryDatabase(
"dbDict",
"SELECT * FROM dictionary WHERE key LIKE ? OR transliteration LIKE ? OR definitions LIKE ?",
[searchTerm, searchTerm, searchTerm]
),
this.queryDatabase(
"dbPure",
"SELECT * FROM strong_pure WHERE text_part LIKE ?",
[searchTerm]
),
this.queryDatabase(
"dbWords",
"SELECT * FROM strong_words WHERE strong_id LIKE ?",
[searchTerm]
),
this.queryDatabase(
"dbAcrostics",
"SELECT * FROM acrostics WHERE value LIKE ?",
[searchTerm]
),
this.queryDatabase(
"dbBooks",
"SELECT * FROM books WHERE name LIKE ? OR abbreviation LIKE ?",
[searchTerm, searchTerm]
),
this.queryDatabase(
"dbCitations",
"SELECT * FROM kjv_citations WHERE citation LIKE ?",
[searchTerm]
),
this.queryDatabase(
"dbChapters",
"SELECT * FROM chapters WHERE chapter_name LIKE ? OR chapter_number LIKE ?",
[searchTerm, searchTerm]
),
this.queryDatabase(
"dbKjvPure",
"SELECT * FROM kjv_pure WHERE verse_text LIKE ?",
[searchTerm]
),
]);
return {
dictionary: results[0],
strong_pure: results[1],
strong_words: results[2],
kjv_acrostics: results[3],
kjv_books: results[4],
kjv_citations: results[5],
kjv_chapters: results[6],
kjv_pure: results[7],
};
} catch (error) {
console.error("Search operation failed:", error);
throw error; // Rethrow the error if you want to handle it further up the chain
}
}
}
module.exports = SearchEngine;