This repository has been archived by the owner on Jul 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
213 lines (192 loc) · 7.51 KB
/
index.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const Logger = require('js-logger')
const AvailableFields = require('./lib/AvailableFields.js').AvailableFields
const CalculateBuckets = require('./lib/CalculateBuckets.js').CalculateBuckets
const CalculateCategories = require('./lib/CalculateCategories.js').CalculateCategories
const CalculateEntireResultSet = require('./lib/CalculateEntireResultSet.js').CalculateEntireResultSet
const CalculateResultSetPerClause = require('./lib/CalculateResultSetPerClause.js').CalculateResultSetPerClause
const CalculateTopScoringDocs = require('./lib/CalculateTopScoringDocs.js').CalculateTopScoringDocs
const CalculateTotalHits = require('./lib/CalculateTotalHits.js').CalculateTotalHits
const Classify = require('./lib/Classify.js').Classify
const FetchDocsFromDB = require('./lib/FetchDocsFromDB.js').FetchDocsFromDB
const FetchStoredDoc = require('./lib/FetchStoredDoc.js').FetchStoredDoc
const GetIntersectionStream = require('./lib/GetIntersectionStream.js').GetIntersectionStream
const MergeOrConditionsTFIDF = require('./lib/MergeOrConditionsTFIDF.js').MergeOrConditionsTFIDF
const MergeOrConditionsFieldSort = require('./lib/MergeOrConditionsFieldSort.js').MergeOrConditionsFieldSort
const Readable = require('stream').Readable
const ReplaceSynonyms = require('./lib/ReplaceSynonyms.js').ReplaceSynonyms
const ScoreDocsOnField = require('./lib/ScoreDocsOnField.js').ScoreDocsOnField
const ScoreTopScoringDocsTFIDF = require('./lib/ScoreTopScoringDocsTFIDF.js').ScoreTopScoringDocsTFIDF
const levelup = require('levelup')
const matcher = require('./lib/matcher.js')
const siUtil = require('./lib/siUtil.js')
const sw = require('stopword')
const initModule = function (err, Searcher, moduleReady) {
Searcher.bucketStream = function (q) {
q = siUtil.getQueryDefaults(q)
const s = new Readable({ objectMode: true })
q.query.forEach(function (clause) {
s.push(clause)
})
s.push(null)
return s
.pipe(new CalculateResultSetPerClause(Searcher.options, q.filter || {}))
.pipe(new CalculateEntireResultSet(Searcher.options))
.pipe(new CalculateBuckets(Searcher.options, q.filter || {}, q.buckets))
}
Searcher.categoryStream = function (q) {
q = siUtil.getQueryDefaults(q)
const s = new Readable({ objectMode: true })
q.query.forEach(function (clause) {
s.push(clause)
})
s.push(null)
return s
.pipe(new CalculateResultSetPerClause(Searcher.options, q.filter || {}))
.pipe(new CalculateEntireResultSet(Searcher.options))
.pipe(new CalculateCategories(Searcher.options, q))
}
Searcher.classify = function (ops) {
return new Classify(Searcher, ops)
}
Searcher.close = function (callback) {
Searcher.options.indexes.close(function (err) {
while (!Searcher.options.indexes.isClosed()) {
Searcher.options.log.debug('closing...')
}
if (Searcher.options.indexes.isClosed()) {
Searcher.options.log.debug('closed...')
callback(err)
}
})
}
Searcher.availableFields = function () {
const sep = Searcher.options.keySeparator
return Searcher.options.indexes.createReadStream({
gte: 'FIELD' + sep,
lte: 'FIELD' + sep + sep
}).pipe(new AvailableFields(Searcher.options))
}
Searcher.get = function (docIDs) {
var s = new Readable({ objectMode: true })
docIDs.forEach(function (id) {
s.push(id)
})
s.push(null)
return s.pipe(new FetchDocsFromDB(Searcher.options))
}
Searcher.fieldNames = function () {
return Searcher.options.indexes.createReadStream({
gte: 'FIELD',
lte: 'FIELD'
})
}
Searcher.match = function (q) {
return matcher.match(q, Searcher.options)
}
Searcher.dbReadStream = function () {
return Searcher.options.indexes.createReadStream()
}
Searcher.search = function (q) {
q = siUtil.getQueryDefaults(q)
const s = new Readable({ objectMode: true })
q.query.forEach(function (clause) {
s.push(clause)
})
s.push(null)
if (q.sort) {
/*
Can be 2 scenarios:
1) Lots of hits in a smallish index (dense)
2) a few hits in a gigantic index (sparse)
Since 2) can be solved by simply sorting afterwards- solve 1) first
should be
1) Calculate resultset per clause
2) Traverse TF arrays and check for intersections down to "seek limit"
3) Merge/trim pages/sort
4) Fetch stored docs
*/
return s
.pipe(new ReplaceSynonyms(Searcher.options))
.pipe(new CalculateResultSetPerClause(Searcher.options))
.pipe(new ScoreDocsOnField(Searcher.options, (q.offset + q.pageSize), q.sort))
.pipe(new MergeOrConditionsFieldSort(q))
.pipe(new FetchStoredDoc(Searcher.options))
} else {
return s
.pipe(new ReplaceSynonyms(Searcher.options))
.pipe(new CalculateResultSetPerClause(Searcher.options))
.pipe(new CalculateTopScoringDocs(Searcher.options, q.offset, q.pageSize))
.pipe(new ScoreTopScoringDocsTFIDF(Searcher.options))
.pipe(new MergeOrConditionsTFIDF(q.offset, q.pageSize))
.pipe(new FetchStoredDoc(Searcher.options))
}
}
// TODO: seriously needs a rewrite
Searcher.scan = function (q) {
q = siUtil.getQueryDefaults(q)
// just make this work for a simple one clause AND
// TODO: add filtering, NOTting, multi-clause AND
var s = new Readable({ objectMode: true })
s.push('init')
s.push(null)
return s
.pipe(new GetIntersectionStream(Searcher.options,
siUtil.getKeySet(
q.query[0].AND,
Searcher.options.keySeparator
)))
.pipe(new FetchDocsFromDB(Searcher.options))
}
Searcher.totalHits = function (q, callback) {
q = siUtil.getQueryDefaults(q)
const s = new Readable({ objectMode: true })
q.query.forEach(function (clause) {
s.push(clause)
})
s.push(null)
s.pipe(new CalculateResultSetPerClause(Searcher.options, q.filter || {}))
.pipe(new CalculateEntireResultSet(Searcher.options))
.pipe(new CalculateTotalHits(Searcher.options)).on('data', function (totalHits) {
return callback(null, totalHits)
})
}
return moduleReady(err, Searcher)
}
const getOptions = function (options, done) {
var Searcher = {}
Searcher.options = Object.assign({}, {
deletable: true,
fieldedSearch: true,
store: true,
indexPath: 'si',
keySeparator: '○',
logLevel: 'ERROR',
logHandler: Logger.createDefaultHandler(),
nGramLength: 1,
nGramSeparator: ' ',
separator: /[|' .,\-|(\n)]+/,
stopwords: sw.en
}, options)
Searcher.options.log = Logger.get('search-index-searcher')
// We pass the log level as string because the Logger[logLevel] returns
// an object, and Object.assign deosn't make deep assign so it breakes
// We used toUpperCase() for backward compatibility
Searcher.options.log.setLevel(Logger[Searcher.options.logLevel.toUpperCase()])
// Use the global one because the library doesn't support providing handler to named logger
Logger.setHandler(Searcher.options.logHandler)
if (!options.indexes) {
levelup(Searcher.options.indexPath || 'si', {
valueEncoding: 'json'
}, function (err, db) {
Searcher.options.indexes = db
return done(err, Searcher)
})
} else {
return done(null, Searcher)
}
}
module.exports = function (givenOptions, moduleReady) {
getOptions(givenOptions, function (err, Searcher) {
initModule(err, Searcher, moduleReady)
})
}