machinery-tool 1.23.1 → 1.24.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.git_revision +1 -1
- data/NEWS +11 -0
- data/export_helpers/kiwi_export_readme.md +3 -3
- data/export_helpers/merge_users_and_groups.pl.erb +1 -1
- data/lib/build_task.rb +5 -3
- data/lib/deploy_task.rb +0 -1
- data/lib/exceptions.rb +13 -0
- data/lib/kiwi_config.rb +3 -9
- data/lib/machinery_helper.rb +15 -1
- data/lib/remote_system.rb +4 -2
- data/lib/system_description.rb +0 -9
- data/lib/version.rb +1 -1
- data/machinery-helper/version.go +1 -1
- data/man/generated/machinery.1.gz +0 -0
- data/manual/docs/index.html +2 -8
- data/manual/docs/machinery.ymp +10 -10
- data/manual/mkdocs.yml +3 -1
- data/manual/site/404.html +145 -0
- data/manual/site/css/base.css +324 -0
- data/manual/site/css/bootstrap-custom.min.css +1 -0
- data/manual/site/css/fixed-positioning.css +0 -0
- data/manual/site/css/font-awesome.min.css +4 -0
- data/manual/site/docs/index.html +25 -70
- data/manual/site/fonts/fontawesome-webfont.eot +0 -0
- data/manual/site/fonts/fontawesome-webfont.svg +2671 -0
- data/manual/site/fonts/fontawesome-webfont.ttf +0 -0
- data/manual/site/fonts/fontawesome-webfont.woff +0 -0
- data/manual/site/fonts/fontawesome-webfont.woff2 +0 -0
- data/manual/site/fonts/glyphicons-halflings-regular.eot +0 -0
- data/manual/site/fonts/glyphicons-halflings-regular.svg +288 -0
- data/manual/site/fonts/glyphicons-halflings-regular.ttf +0 -0
- data/manual/site/fonts/glyphicons-halflings-regular.woff +0 -0
- data/manual/site/fonts/glyphicons-halflings-regular.woff2 +0 -0
- data/manual/site/img/favicon.ico +0 -0
- data/manual/site/img/grid.png +0 -0
- data/manual/site/index.html +135 -396
- data/manual/site/js/base.js +216 -0
- data/manual/site/js/bootstrap-3.0.3.min.js +7 -0
- data/manual/site/js/jquery-1.10.2.min.js +6 -0
- data/manual/site/js/jquery.nicescroll.min.js +0 -0
- data/manual/site/js/jquery.pageslide.min.js +0 -0
- data/manual/site/js/parallaxImg.js +0 -0
- data/manual/site/machinery-analyze.1/index.html +25 -54
- data/manual/site/machinery-build.1/index.html +25 -85
- data/manual/site/machinery-compare.1/index.html +25 -87
- data/manual/site/machinery-config.1/index.html +25 -62
- data/manual/site/machinery-copy.1/index.html +25 -50
- data/manual/site/machinery-deploy.1/index.html +25 -87
- data/manual/site/machinery-export-autoyast.1/index.html +25 -74
- data/manual/site/machinery-export-html.1/index.html +25 -55
- data/manual/site/machinery-export-kiwi.1/index.html +25 -56
- data/manual/site/machinery-inspect-container.1/index.html +25 -129
- data/manual/site/machinery-inspect.1/index.html +25 -172
- data/manual/site/machinery-list.1/index.html +25 -67
- data/manual/site/machinery-man.1/index.html +25 -34
- data/manual/site/machinery-move.1/index.html +25 -49
- data/manual/site/machinery-remove.1/index.html +25 -62
- data/manual/site/machinery-serve.1/index.html +25 -61
- data/manual/site/machinery-show.1/index.html +25 -86
- data/manual/site/machinery-upgrade-format.1/index.html +25 -60
- data/manual/site/machinery-validate.1/index.html +25 -48
- data/manual/site/machinery.ymp +10 -10
- data/manual/site/machinery_main_general.1/index.html +25 -133
- data/manual/site/machinery_main_scopes.1/index.html +25 -129
- data/manual/site/machinery_main_security_implications.1/index.html +25 -108
- data/manual/site/machinery_main_usecases.1/index.html +25 -44
- data/manual/site/search/lunr.js +2986 -0
- data/manual/site/search/main.js +96 -0
- data/manual/site/search/search_index.json +1 -0
- data/manual/site/search/worker.js +128 -0
- data/manual/site/sitemap.xml +48 -87
- data/manual/site/sitemap.xml.gz +0 -0
- data/plugins/os/os_model.rb +0 -29
- data/tools/go.rb +14 -20
- metadata +32 -17
- data/manual/site/mkdocs/js/lunr.min.js +0 -7
- data/manual/site/mkdocs/js/mustache.min.js +0 -1
- data/manual/site/mkdocs/js/require.js +0 -36
- data/manual/site/mkdocs/js/search-results-template.mustache +0 -4
- data/manual/site/mkdocs/js/search.js +0 -88
- data/manual/site/mkdocs/js/text.js +0 -390
- data/manual/site/mkdocs/search_index.json +0 -869
@@ -17,35 +17,35 @@
|
|
17
17
|
|
18
18
|
|
19
19
|
<li class="">
|
20
|
-
<a href="
|
20
|
+
<a href="docs/">Welcome</a>
|
21
21
|
</li>
|
22
22
|
|
23
23
|
|
24
24
|
|
25
25
|
|
26
26
|
<li class="">
|
27
|
-
<a href="
|
27
|
+
<a href="machinery_main_general.1/">General Overview</a>
|
28
28
|
</li>
|
29
29
|
|
30
30
|
|
31
31
|
|
32
32
|
|
33
33
|
<li class="">
|
34
|
-
<a href="
|
34
|
+
<a href="machinery_main_scopes.1/">Scopes</a>
|
35
35
|
</li>
|
36
36
|
|
37
37
|
|
38
38
|
|
39
39
|
|
40
40
|
<li class="current">
|
41
|
-
<a href="
|
41
|
+
<a href="machinery_main_usecases.1/">Use cases</a>
|
42
42
|
</li>
|
43
43
|
|
44
44
|
|
45
45
|
|
46
46
|
|
47
47
|
<li class="">
|
48
|
-
<a href="
|
48
|
+
<a href="machinery_main_security_implications.1/">Security Implications</a>
|
49
49
|
</li>
|
50
50
|
|
51
51
|
|
@@ -55,79 +55,79 @@
|
|
55
55
|
<ul>
|
56
56
|
|
57
57
|
<li class="">
|
58
|
-
<a href="
|
58
|
+
<a href="machinery-analyze.1/">Analyze</a>
|
59
59
|
</li>
|
60
60
|
|
61
61
|
<li class="">
|
62
|
-
<a href="
|
62
|
+
<a href="machinery-build.1/">Build</a>
|
63
63
|
</li>
|
64
64
|
|
65
65
|
<li class="">
|
66
|
-
<a href="
|
66
|
+
<a href="machinery-compare.1/">Compare</a>
|
67
67
|
</li>
|
68
68
|
|
69
69
|
<li class="">
|
70
|
-
<a href="
|
70
|
+
<a href="machinery-config.1/">Config</a>
|
71
71
|
</li>
|
72
72
|
|
73
73
|
<li class="">
|
74
|
-
<a href="
|
74
|
+
<a href="machinery-copy.1/">Copy</a>
|
75
75
|
</li>
|
76
76
|
|
77
77
|
<li class="">
|
78
|
-
<a href="
|
78
|
+
<a href="machinery-deploy.1/">Deploy</a>
|
79
79
|
</li>
|
80
80
|
|
81
81
|
<li class="">
|
82
|
-
<a href="
|
82
|
+
<a href="machinery-export-autoyast.1/">Export AutoYaST</a>
|
83
83
|
</li>
|
84
84
|
|
85
85
|
<li class="">
|
86
|
-
<a href="
|
86
|
+
<a href="machinery-export-kiwi.1/">Export Kiwi</a>
|
87
87
|
</li>
|
88
88
|
|
89
89
|
<li class="">
|
90
|
-
<a href="
|
90
|
+
<a href="machinery-export-html.1/">Export HTML</a>
|
91
91
|
</li>
|
92
92
|
|
93
93
|
<li class="">
|
94
|
-
<a href="
|
94
|
+
<a href="machinery-inspect.1/">Inspect</a>
|
95
95
|
</li>
|
96
96
|
|
97
97
|
<li class="">
|
98
|
-
<a href="
|
98
|
+
<a href="machinery-inspect-container.1/">Inspect Container</a>
|
99
99
|
</li>
|
100
100
|
|
101
101
|
<li class="">
|
102
|
-
<a href="
|
102
|
+
<a href="machinery-list.1/">List</a>
|
103
103
|
</li>
|
104
104
|
|
105
105
|
<li class="">
|
106
|
-
<a href="
|
106
|
+
<a href="machinery-man.1/">Man</a>
|
107
107
|
</li>
|
108
108
|
|
109
109
|
<li class="">
|
110
|
-
<a href="
|
110
|
+
<a href="machinery-move.1/">Move</a>
|
111
111
|
</li>
|
112
112
|
|
113
113
|
<li class="">
|
114
|
-
<a href="
|
114
|
+
<a href="machinery-remove.1/">Remove</a>
|
115
115
|
</li>
|
116
116
|
|
117
117
|
<li class="">
|
118
|
-
<a href="
|
118
|
+
<a href="machinery-serve.1/">Serve</a>
|
119
119
|
</li>
|
120
120
|
|
121
121
|
<li class="">
|
122
|
-
<a href="
|
122
|
+
<a href="machinery-show.1/">Show</a>
|
123
123
|
</li>
|
124
124
|
|
125
125
|
<li class="">
|
126
|
-
<a href="
|
126
|
+
<a href="machinery-upgrade-format.1/">Upgrade Format</a>
|
127
127
|
</li>
|
128
128
|
|
129
129
|
<li class="">
|
130
|
-
<a href="
|
130
|
+
<a href="machinery-validate.1/">Validate</a>
|
131
131
|
</li>
|
132
132
|
|
133
133
|
</ul>
|
@@ -138,26 +138,7 @@
|
|
138
138
|
</ul>
|
139
139
|
</div>
|
140
140
|
<div class="content">
|
141
|
-
|
142
|
-
<p>Some of the important use cases of Machinery are:</p>
|
143
|
-
<ul>
|
144
|
-
<li>Inspecting a System and Collecting Information</li>
|
145
|
-
</ul>
|
146
|
-
<p>Collecting a variety of information. Limit the gathered
|
147
|
-
information with scopes (see section about scopes). Each inspection step
|
148
|
-
updates the system description.</p>
|
149
|
-
<ul>
|
150
|
-
<li>Reviewing System Description</li>
|
151
|
-
</ul>
|
152
|
-
<p>After a successful inspection, the system description can be displayed on
|
153
|
-
the console or the output can be fed into other tools.</p>
|
154
|
-
<ul>
|
155
|
-
<li>Cloning a System</li>
|
156
|
-
</ul>
|
157
|
-
<p>An inspected system can be cloned. The inspection step returns a system
|
158
|
-
description which is used as the basis for cloning physical or virtual
|
159
|
-
instances. Machinery can build a system image from the description, which
|
160
|
-
can then for example be deployed to a cloud environment.</p>
|
141
|
+
|
161
142
|
</div>
|
162
143
|
</div>
|
163
144
|
</body>
|
@@ -0,0 +1,2986 @@
|
|
1
|
+
/**
|
2
|
+
* lunr - http://lunrjs.com - A bit like Solr, but much smaller and not as bright - 2.1.6
|
3
|
+
* Copyright (C) 2018 Oliver Nightingale
|
4
|
+
* @license MIT
|
5
|
+
*/
|
6
|
+
|
7
|
+
;(function(){
|
8
|
+
|
9
|
+
/**
|
10
|
+
* A convenience function for configuring and constructing
|
11
|
+
* a new lunr Index.
|
12
|
+
*
|
13
|
+
* A lunr.Builder instance is created and the pipeline setup
|
14
|
+
* with a trimmer, stop word filter and stemmer.
|
15
|
+
*
|
16
|
+
* This builder object is yielded to the configuration function
|
17
|
+
* that is passed as a parameter, allowing the list of fields
|
18
|
+
* and other builder parameters to be customised.
|
19
|
+
*
|
20
|
+
* All documents _must_ be added within the passed config function.
|
21
|
+
*
|
22
|
+
* @example
|
23
|
+
* var idx = lunr(function () {
|
24
|
+
* this.field('title')
|
25
|
+
* this.field('body')
|
26
|
+
* this.ref('id')
|
27
|
+
*
|
28
|
+
* documents.forEach(function (doc) {
|
29
|
+
* this.add(doc)
|
30
|
+
* }, this)
|
31
|
+
* })
|
32
|
+
*
|
33
|
+
* @see {@link lunr.Builder}
|
34
|
+
* @see {@link lunr.Pipeline}
|
35
|
+
* @see {@link lunr.trimmer}
|
36
|
+
* @see {@link lunr.stopWordFilter}
|
37
|
+
* @see {@link lunr.stemmer}
|
38
|
+
* @namespace {function} lunr
|
39
|
+
*/
|
40
|
+
var lunr = function (config) {
|
41
|
+
var builder = new lunr.Builder
|
42
|
+
|
43
|
+
builder.pipeline.add(
|
44
|
+
lunr.trimmer,
|
45
|
+
lunr.stopWordFilter,
|
46
|
+
lunr.stemmer
|
47
|
+
)
|
48
|
+
|
49
|
+
builder.searchPipeline.add(
|
50
|
+
lunr.stemmer
|
51
|
+
)
|
52
|
+
|
53
|
+
config.call(builder, builder)
|
54
|
+
return builder.build()
|
55
|
+
}
|
56
|
+
|
57
|
+
lunr.version = "2.1.6"
|
58
|
+
/*!
|
59
|
+
* lunr.utils
|
60
|
+
* Copyright (C) 2018 Oliver Nightingale
|
61
|
+
*/
|
62
|
+
|
63
|
+
/**
|
64
|
+
* A namespace containing utils for the rest of the lunr library
|
65
|
+
*/
|
66
|
+
lunr.utils = {}
|
67
|
+
|
68
|
+
/**
|
69
|
+
* Print a warning message to the console.
|
70
|
+
*
|
71
|
+
* @param {String} message The message to be printed.
|
72
|
+
* @memberOf Utils
|
73
|
+
*/
|
74
|
+
lunr.utils.warn = (function (global) {
|
75
|
+
/* eslint-disable no-console */
|
76
|
+
return function (message) {
|
77
|
+
if (global.console && console.warn) {
|
78
|
+
console.warn(message)
|
79
|
+
}
|
80
|
+
}
|
81
|
+
/* eslint-enable no-console */
|
82
|
+
})(this)
|
83
|
+
|
84
|
+
/**
|
85
|
+
* Convert an object to a string.
|
86
|
+
*
|
87
|
+
* In the case of `null` and `undefined` the function returns
|
88
|
+
* the empty string, in all other cases the result of calling
|
89
|
+
* `toString` on the passed object is returned.
|
90
|
+
*
|
91
|
+
* @param {Any} obj The object to convert to a string.
|
92
|
+
* @return {String} string representation of the passed object.
|
93
|
+
* @memberOf Utils
|
94
|
+
*/
|
95
|
+
lunr.utils.asString = function (obj) {
|
96
|
+
if (obj === void 0 || obj === null) {
|
97
|
+
return ""
|
98
|
+
} else {
|
99
|
+
return obj.toString()
|
100
|
+
}
|
101
|
+
}
|
102
|
+
lunr.FieldRef = function (docRef, fieldName, stringValue) {
|
103
|
+
this.docRef = docRef
|
104
|
+
this.fieldName = fieldName
|
105
|
+
this._stringValue = stringValue
|
106
|
+
}
|
107
|
+
|
108
|
+
lunr.FieldRef.joiner = "/"
|
109
|
+
|
110
|
+
lunr.FieldRef.fromString = function (s) {
|
111
|
+
var n = s.indexOf(lunr.FieldRef.joiner)
|
112
|
+
|
113
|
+
if (n === -1) {
|
114
|
+
throw "malformed field ref string"
|
115
|
+
}
|
116
|
+
|
117
|
+
var fieldRef = s.slice(0, n),
|
118
|
+
docRef = s.slice(n + 1)
|
119
|
+
|
120
|
+
return new lunr.FieldRef (docRef, fieldRef, s)
|
121
|
+
}
|
122
|
+
|
123
|
+
lunr.FieldRef.prototype.toString = function () {
|
124
|
+
if (this._stringValue == undefined) {
|
125
|
+
this._stringValue = this.fieldName + lunr.FieldRef.joiner + this.docRef
|
126
|
+
}
|
127
|
+
|
128
|
+
return this._stringValue
|
129
|
+
}
|
130
|
+
/**
|
131
|
+
* A function to calculate the inverse document frequency for
|
132
|
+
* a posting. This is shared between the builder and the index
|
133
|
+
*
|
134
|
+
* @private
|
135
|
+
* @param {object} posting - The posting for a given term
|
136
|
+
* @param {number} documentCount - The total number of documents.
|
137
|
+
*/
|
138
|
+
lunr.idf = function (posting, documentCount) {
|
139
|
+
var documentsWithTerm = 0
|
140
|
+
|
141
|
+
for (var fieldName in posting) {
|
142
|
+
if (fieldName == '_index') continue // Ignore the term index, its not a field
|
143
|
+
documentsWithTerm += Object.keys(posting[fieldName]).length
|
144
|
+
}
|
145
|
+
|
146
|
+
var x = (documentCount - documentsWithTerm + 0.5) / (documentsWithTerm + 0.5)
|
147
|
+
|
148
|
+
return Math.log(1 + Math.abs(x))
|
149
|
+
}
|
150
|
+
|
151
|
+
/**
|
152
|
+
* A token wraps a string representation of a token
|
153
|
+
* as it is passed through the text processing pipeline.
|
154
|
+
*
|
155
|
+
* @constructor
|
156
|
+
* @param {string} [str=''] - The string token being wrapped.
|
157
|
+
* @param {object} [metadata={}] - Metadata associated with this token.
|
158
|
+
*/
|
159
|
+
lunr.Token = function (str, metadata) {
|
160
|
+
this.str = str || ""
|
161
|
+
this.metadata = metadata || {}
|
162
|
+
}
|
163
|
+
|
164
|
+
/**
|
165
|
+
* Returns the token string that is being wrapped by this object.
|
166
|
+
*
|
167
|
+
* @returns {string}
|
168
|
+
*/
|
169
|
+
lunr.Token.prototype.toString = function () {
|
170
|
+
return this.str
|
171
|
+
}
|
172
|
+
|
173
|
+
/**
|
174
|
+
* A token update function is used when updating or optionally
|
175
|
+
* when cloning a token.
|
176
|
+
*
|
177
|
+
* @callback lunr.Token~updateFunction
|
178
|
+
* @param {string} str - The string representation of the token.
|
179
|
+
* @param {Object} metadata - All metadata associated with this token.
|
180
|
+
*/
|
181
|
+
|
182
|
+
/**
|
183
|
+
* Applies the given function to the wrapped string token.
|
184
|
+
*
|
185
|
+
* @example
|
186
|
+
* token.update(function (str, metadata) {
|
187
|
+
* return str.toUpperCase()
|
188
|
+
* })
|
189
|
+
*
|
190
|
+
* @param {lunr.Token~updateFunction} fn - A function to apply to the token string.
|
191
|
+
* @returns {lunr.Token}
|
192
|
+
*/
|
193
|
+
lunr.Token.prototype.update = function (fn) {
|
194
|
+
this.str = fn(this.str, this.metadata)
|
195
|
+
return this
|
196
|
+
}
|
197
|
+
|
198
|
+
/**
|
199
|
+
* Creates a clone of this token. Optionally a function can be
|
200
|
+
* applied to the cloned token.
|
201
|
+
*
|
202
|
+
* @param {lunr.Token~updateFunction} [fn] - An optional function to apply to the cloned token.
|
203
|
+
* @returns {lunr.Token}
|
204
|
+
*/
|
205
|
+
lunr.Token.prototype.clone = function (fn) {
|
206
|
+
fn = fn || function (s) { return s }
|
207
|
+
return new lunr.Token (fn(this.str, this.metadata), this.metadata)
|
208
|
+
}
|
209
|
+
/*!
|
210
|
+
* lunr.tokenizer
|
211
|
+
* Copyright (C) 2018 Oliver Nightingale
|
212
|
+
*/
|
213
|
+
|
214
|
+
/**
|
215
|
+
* A function for splitting a string into tokens ready to be inserted into
|
216
|
+
* the search index. Uses `lunr.tokenizer.separator` to split strings, change
|
217
|
+
* the value of this property to change how strings are split into tokens.
|
218
|
+
*
|
219
|
+
* This tokenizer will convert its parameter to a string by calling `toString` and
|
220
|
+
* then will split this string on the character in `lunr.tokenizer.separator`.
|
221
|
+
* Arrays will have their elements converted to strings and wrapped in a lunr.Token.
|
222
|
+
*
|
223
|
+
* @static
|
224
|
+
* @param {?(string|object|object[])} obj - The object to convert into tokens
|
225
|
+
* @returns {lunr.Token[]}
|
226
|
+
*/
|
227
|
+
lunr.tokenizer = function (obj) {
|
228
|
+
if (obj == null || obj == undefined) {
|
229
|
+
return []
|
230
|
+
}
|
231
|
+
|
232
|
+
if (Array.isArray(obj)) {
|
233
|
+
return obj.map(function (t) {
|
234
|
+
return new lunr.Token(lunr.utils.asString(t).toLowerCase())
|
235
|
+
})
|
236
|
+
}
|
237
|
+
|
238
|
+
var str = obj.toString().trim().toLowerCase(),
|
239
|
+
len = str.length,
|
240
|
+
tokens = []
|
241
|
+
|
242
|
+
for (var sliceEnd = 0, sliceStart = 0; sliceEnd <= len; sliceEnd++) {
|
243
|
+
var char = str.charAt(sliceEnd),
|
244
|
+
sliceLength = sliceEnd - sliceStart
|
245
|
+
|
246
|
+
if ((char.match(lunr.tokenizer.separator) || sliceEnd == len)) {
|
247
|
+
|
248
|
+
if (sliceLength > 0) {
|
249
|
+
tokens.push(
|
250
|
+
new lunr.Token (str.slice(sliceStart, sliceEnd), {
|
251
|
+
position: [sliceStart, sliceLength],
|
252
|
+
index: tokens.length
|
253
|
+
})
|
254
|
+
)
|
255
|
+
}
|
256
|
+
|
257
|
+
sliceStart = sliceEnd + 1
|
258
|
+
}
|
259
|
+
|
260
|
+
}
|
261
|
+
|
262
|
+
return tokens
|
263
|
+
}
|
264
|
+
|
265
|
+
/**
|
266
|
+
* The separator used to split a string into tokens. Override this property to change the behaviour of
|
267
|
+
* `lunr.tokenizer` behaviour when tokenizing strings. By default this splits on whitespace and hyphens.
|
268
|
+
*
|
269
|
+
* @static
|
270
|
+
* @see lunr.tokenizer
|
271
|
+
*/
|
272
|
+
lunr.tokenizer.separator = /[\s\-]+/
|
273
|
+
/*!
|
274
|
+
* lunr.Pipeline
|
275
|
+
* Copyright (C) 2018 Oliver Nightingale
|
276
|
+
*/
|
277
|
+
|
278
|
+
/**
|
279
|
+
* lunr.Pipelines maintain an ordered list of functions to be applied to all
|
280
|
+
* tokens in documents entering the search index and queries being ran against
|
281
|
+
* the index.
|
282
|
+
*
|
283
|
+
* An instance of lunr.Index created with the lunr shortcut will contain a
|
284
|
+
* pipeline with a stop word filter and an English language stemmer. Extra
|
285
|
+
* functions can be added before or after either of these functions or these
|
286
|
+
* default functions can be removed.
|
287
|
+
*
|
288
|
+
* When run the pipeline will call each function in turn, passing a token, the
|
289
|
+
* index of that token in the original list of all tokens and finally a list of
|
290
|
+
* all the original tokens.
|
291
|
+
*
|
292
|
+
* The output of functions in the pipeline will be passed to the next function
|
293
|
+
* in the pipeline. To exclude a token from entering the index the function
|
294
|
+
* should return undefined, the rest of the pipeline will not be called with
|
295
|
+
* this token.
|
296
|
+
*
|
297
|
+
* For serialisation of pipelines to work, all functions used in an instance of
|
298
|
+
* a pipeline should be registered with lunr.Pipeline. Registered functions can
|
299
|
+
* then be loaded. If trying to load a serialised pipeline that uses functions
|
300
|
+
* that are not registered an error will be thrown.
|
301
|
+
*
|
302
|
+
* If not planning on serialising the pipeline then registering pipeline functions
|
303
|
+
* is not necessary.
|
304
|
+
*
|
305
|
+
* @constructor
|
306
|
+
*/
|
307
|
+
lunr.Pipeline = function () {
|
308
|
+
this._stack = []
|
309
|
+
}
|
310
|
+
|
311
|
+
lunr.Pipeline.registeredFunctions = Object.create(null)
|
312
|
+
|
313
|
+
/**
|
314
|
+
* A pipeline function maps lunr.Token to lunr.Token. A lunr.Token contains the token
|
315
|
+
* string as well as all known metadata. A pipeline function can mutate the token string
|
316
|
+
* or mutate (or add) metadata for a given token.
|
317
|
+
*
|
318
|
+
* A pipeline function can indicate that the passed token should be discarded by returning
|
319
|
+
* null. This token will not be passed to any downstream pipeline functions and will not be
|
320
|
+
* added to the index.
|
321
|
+
*
|
322
|
+
* Multiple tokens can be returned by returning an array of tokens. Each token will be passed
|
323
|
+
* to any downstream pipeline functions and all will returned tokens will be added to the index.
|
324
|
+
*
|
325
|
+
* Any number of pipeline functions may be chained together using a lunr.Pipeline.
|
326
|
+
*
|
327
|
+
* @interface lunr.PipelineFunction
|
328
|
+
* @param {lunr.Token} token - A token from the document being processed.
|
329
|
+
* @param {number} i - The index of this token in the complete list of tokens for this document/field.
|
330
|
+
* @param {lunr.Token[]} tokens - All tokens for this document/field.
|
331
|
+
* @returns {(?lunr.Token|lunr.Token[])}
|
332
|
+
*/
|
333
|
+
|
334
|
+
/**
|
335
|
+
* Register a function with the pipeline.
|
336
|
+
*
|
337
|
+
* Functions that are used in the pipeline should be registered if the pipeline
|
338
|
+
* needs to be serialised, or a serialised pipeline needs to be loaded.
|
339
|
+
*
|
340
|
+
* Registering a function does not add it to a pipeline, functions must still be
|
341
|
+
* added to instances of the pipeline for them to be used when running a pipeline.
|
342
|
+
*
|
343
|
+
* @param {lunr.PipelineFunction} fn - The function to check for.
|
344
|
+
* @param {String} label - The label to register this function with
|
345
|
+
*/
|
346
|
+
lunr.Pipeline.registerFunction = function (fn, label) {
|
347
|
+
if (label in this.registeredFunctions) {
|
348
|
+
lunr.utils.warn('Overwriting existing registered function: ' + label)
|
349
|
+
}
|
350
|
+
|
351
|
+
fn.label = label
|
352
|
+
lunr.Pipeline.registeredFunctions[fn.label] = fn
|
353
|
+
}
|
354
|
+
|
355
|
+
/**
|
356
|
+
* Warns if the function is not registered as a Pipeline function.
|
357
|
+
*
|
358
|
+
* @param {lunr.PipelineFunction} fn - The function to check for.
|
359
|
+
* @private
|
360
|
+
*/
|
361
|
+
lunr.Pipeline.warnIfFunctionNotRegistered = function (fn) {
|
362
|
+
var isRegistered = fn.label && (fn.label in this.registeredFunctions)
|
363
|
+
|
364
|
+
if (!isRegistered) {
|
365
|
+
lunr.utils.warn('Function is not registered with pipeline. This may cause problems when serialising the index.\n', fn)
|
366
|
+
}
|
367
|
+
}
|
368
|
+
|
369
|
+
/**
|
370
|
+
* Loads a previously serialised pipeline.
|
371
|
+
*
|
372
|
+
* All functions to be loaded must already be registered with lunr.Pipeline.
|
373
|
+
* If any function from the serialised data has not been registered then an
|
374
|
+
* error will be thrown.
|
375
|
+
*
|
376
|
+
* @param {Object} serialised - The serialised pipeline to load.
|
377
|
+
* @returns {lunr.Pipeline}
|
378
|
+
*/
|
379
|
+
lunr.Pipeline.load = function (serialised) {
|
380
|
+
var pipeline = new lunr.Pipeline
|
381
|
+
|
382
|
+
serialised.forEach(function (fnName) {
|
383
|
+
var fn = lunr.Pipeline.registeredFunctions[fnName]
|
384
|
+
|
385
|
+
if (fn) {
|
386
|
+
pipeline.add(fn)
|
387
|
+
} else {
|
388
|
+
throw new Error('Cannot load unregistered function: ' + fnName)
|
389
|
+
}
|
390
|
+
})
|
391
|
+
|
392
|
+
return pipeline
|
393
|
+
}
|
394
|
+
|
395
|
+
/**
|
396
|
+
* Adds new functions to the end of the pipeline.
|
397
|
+
*
|
398
|
+
* Logs a warning if the function has not been registered.
|
399
|
+
*
|
400
|
+
* @param {lunr.PipelineFunction[]} functions - Any number of functions to add to the pipeline.
|
401
|
+
*/
|
402
|
+
lunr.Pipeline.prototype.add = function () {
|
403
|
+
var fns = Array.prototype.slice.call(arguments)
|
404
|
+
|
405
|
+
fns.forEach(function (fn) {
|
406
|
+
lunr.Pipeline.warnIfFunctionNotRegistered(fn)
|
407
|
+
this._stack.push(fn)
|
408
|
+
}, this)
|
409
|
+
}
|
410
|
+
|
411
|
+
/**
|
412
|
+
* Adds a single function after a function that already exists in the
|
413
|
+
* pipeline.
|
414
|
+
*
|
415
|
+
* Logs a warning if the function has not been registered.
|
416
|
+
*
|
417
|
+
* @param {lunr.PipelineFunction} existingFn - A function that already exists in the pipeline.
|
418
|
+
* @param {lunr.PipelineFunction} newFn - The new function to add to the pipeline.
|
419
|
+
*/
|
420
|
+
lunr.Pipeline.prototype.after = function (existingFn, newFn) {
|
421
|
+
lunr.Pipeline.warnIfFunctionNotRegistered(newFn)
|
422
|
+
|
423
|
+
var pos = this._stack.indexOf(existingFn)
|
424
|
+
if (pos == -1) {
|
425
|
+
throw new Error('Cannot find existingFn')
|
426
|
+
}
|
427
|
+
|
428
|
+
pos = pos + 1
|
429
|
+
this._stack.splice(pos, 0, newFn)
|
430
|
+
}
|
431
|
+
|
432
|
+
/**
|
433
|
+
* Adds a single function before a function that already exists in the
|
434
|
+
* pipeline.
|
435
|
+
*
|
436
|
+
* Logs a warning if the function has not been registered.
|
437
|
+
*
|
438
|
+
* @param {lunr.PipelineFunction} existingFn - A function that already exists in the pipeline.
|
439
|
+
* @param {lunr.PipelineFunction} newFn - The new function to add to the pipeline.
|
440
|
+
*/
|
441
|
+
lunr.Pipeline.prototype.before = function (existingFn, newFn) {
|
442
|
+
lunr.Pipeline.warnIfFunctionNotRegistered(newFn)
|
443
|
+
|
444
|
+
var pos = this._stack.indexOf(existingFn)
|
445
|
+
if (pos == -1) {
|
446
|
+
throw new Error('Cannot find existingFn')
|
447
|
+
}
|
448
|
+
|
449
|
+
this._stack.splice(pos, 0, newFn)
|
450
|
+
}
|
451
|
+
|
452
|
+
/**
|
453
|
+
* Removes a function from the pipeline.
|
454
|
+
*
|
455
|
+
* @param {lunr.PipelineFunction} fn The function to remove from the pipeline.
|
456
|
+
*/
|
457
|
+
lunr.Pipeline.prototype.remove = function (fn) {
|
458
|
+
var pos = this._stack.indexOf(fn)
|
459
|
+
if (pos == -1) {
|
460
|
+
return
|
461
|
+
}
|
462
|
+
|
463
|
+
this._stack.splice(pos, 1)
|
464
|
+
}
|
465
|
+
|
466
|
+
/**
|
467
|
+
* Runs the current list of functions that make up the pipeline against the
|
468
|
+
* passed tokens.
|
469
|
+
*
|
470
|
+
* @param {Array} tokens The tokens to run through the pipeline.
|
471
|
+
* @returns {Array}
|
472
|
+
*/
|
473
|
+
lunr.Pipeline.prototype.run = function (tokens) {
|
474
|
+
var stackLength = this._stack.length
|
475
|
+
|
476
|
+
for (var i = 0; i < stackLength; i++) {
|
477
|
+
var fn = this._stack[i]
|
478
|
+
var memo = []
|
479
|
+
|
480
|
+
for (var j = 0; j < tokens.length; j++) {
|
481
|
+
var result = fn(tokens[j], j, tokens)
|
482
|
+
|
483
|
+
if (result === void 0 || result === '') continue
|
484
|
+
|
485
|
+
if (result instanceof Array) {
|
486
|
+
for (var k = 0; k < result.length; k++) {
|
487
|
+
memo.push(result[k])
|
488
|
+
}
|
489
|
+
} else {
|
490
|
+
memo.push(result)
|
491
|
+
}
|
492
|
+
}
|
493
|
+
|
494
|
+
tokens = memo
|
495
|
+
}
|
496
|
+
|
497
|
+
return tokens
|
498
|
+
}
|
499
|
+
|
500
|
+
/**
|
501
|
+
* Convenience method for passing a string through a pipeline and getting
|
502
|
+
* strings out. This method takes care of wrapping the passed string in a
|
503
|
+
* token and mapping the resulting tokens back to strings.
|
504
|
+
*
|
505
|
+
* @param {string} str - The string to pass through the pipeline.
|
506
|
+
* @returns {string[]}
|
507
|
+
*/
|
508
|
+
lunr.Pipeline.prototype.runString = function (str) {
|
509
|
+
var token = new lunr.Token (str)
|
510
|
+
|
511
|
+
return this.run([token]).map(function (t) {
|
512
|
+
return t.toString()
|
513
|
+
})
|
514
|
+
}
|
515
|
+
|
516
|
+
/**
|
517
|
+
* Resets the pipeline by removing any existing processors.
|
518
|
+
*
|
519
|
+
*/
|
520
|
+
lunr.Pipeline.prototype.reset = function () {
|
521
|
+
this._stack = []
|
522
|
+
}
|
523
|
+
|
524
|
+
/**
|
525
|
+
* Returns a representation of the pipeline ready for serialisation.
|
526
|
+
*
|
527
|
+
* Logs a warning if the function has not been registered.
|
528
|
+
*
|
529
|
+
* @returns {Array}
|
530
|
+
*/
|
531
|
+
lunr.Pipeline.prototype.toJSON = function () {
|
532
|
+
return this._stack.map(function (fn) {
|
533
|
+
lunr.Pipeline.warnIfFunctionNotRegistered(fn)
|
534
|
+
|
535
|
+
return fn.label
|
536
|
+
})
|
537
|
+
}
|
538
|
+
/*!
|
539
|
+
* lunr.Vector
|
540
|
+
* Copyright (C) 2018 Oliver Nightingale
|
541
|
+
*/
|
542
|
+
|
543
|
+
/**
|
544
|
+
* A vector is used to construct the vector space of documents and queries. These
|
545
|
+
* vectors support operations to determine the similarity between two documents or
|
546
|
+
* a document and a query.
|
547
|
+
*
|
548
|
+
* Normally no parameters are required for initializing a vector, but in the case of
|
549
|
+
* loading a previously dumped vector the raw elements can be provided to the constructor.
|
550
|
+
*
|
551
|
+
* For performance reasons vectors are implemented with a flat array, where an elements
|
552
|
+
* index is immediately followed by its value. E.g. [index, value, index, value]. This
|
553
|
+
* allows the underlying array to be as sparse as possible and still offer decent
|
554
|
+
* performance when being used for vector calculations.
|
555
|
+
*
|
556
|
+
* @constructor
|
557
|
+
* @param {Number[]} [elements] - The flat list of element index and element value pairs.
|
558
|
+
*/
|
559
|
+
lunr.Vector = function (elements) {
|
560
|
+
this._magnitude = 0
|
561
|
+
this.elements = elements || []
|
562
|
+
}
|
563
|
+
|
564
|
+
|
565
|
+
/**
|
566
|
+
* Calculates the position within the vector to insert a given index.
|
567
|
+
*
|
568
|
+
* This is used internally by insert and upsert. If there are duplicate indexes then
|
569
|
+
* the position is returned as if the value for that index were to be updated, but it
|
570
|
+
* is the callers responsibility to check whether there is a duplicate at that index
|
571
|
+
*
|
572
|
+
* @param {Number} insertIdx - The index at which the element should be inserted.
|
573
|
+
* @returns {Number}
|
574
|
+
*/
|
575
|
+
lunr.Vector.prototype.positionForIndex = function (index) {
|
576
|
+
// For an empty vector the tuple can be inserted at the beginning
|
577
|
+
if (this.elements.length == 0) {
|
578
|
+
return 0
|
579
|
+
}
|
580
|
+
|
581
|
+
var start = 0,
|
582
|
+
end = this.elements.length / 2,
|
583
|
+
sliceLength = end - start,
|
584
|
+
pivotPoint = Math.floor(sliceLength / 2),
|
585
|
+
pivotIndex = this.elements[pivotPoint * 2]
|
586
|
+
|
587
|
+
while (sliceLength > 1) {
|
588
|
+
if (pivotIndex < index) {
|
589
|
+
start = pivotPoint
|
590
|
+
}
|
591
|
+
|
592
|
+
if (pivotIndex > index) {
|
593
|
+
end = pivotPoint
|
594
|
+
}
|
595
|
+
|
596
|
+
if (pivotIndex == index) {
|
597
|
+
break
|
598
|
+
}
|
599
|
+
|
600
|
+
sliceLength = end - start
|
601
|
+
pivotPoint = start + Math.floor(sliceLength / 2)
|
602
|
+
pivotIndex = this.elements[pivotPoint * 2]
|
603
|
+
}
|
604
|
+
|
605
|
+
if (pivotIndex == index) {
|
606
|
+
return pivotPoint * 2
|
607
|
+
}
|
608
|
+
|
609
|
+
if (pivotIndex > index) {
|
610
|
+
return pivotPoint * 2
|
611
|
+
}
|
612
|
+
|
613
|
+
if (pivotIndex < index) {
|
614
|
+
return (pivotPoint + 1) * 2
|
615
|
+
}
|
616
|
+
}
|
617
|
+
|
618
|
+
/**
|
619
|
+
* Inserts an element at an index within the vector.
|
620
|
+
*
|
621
|
+
* Does not allow duplicates, will throw an error if there is already an entry
|
622
|
+
* for this index.
|
623
|
+
*
|
624
|
+
* @param {Number} insertIdx - The index at which the element should be inserted.
|
625
|
+
* @param {Number} val - The value to be inserted into the vector.
|
626
|
+
*/
|
627
|
+
lunr.Vector.prototype.insert = function (insertIdx, val) {
|
628
|
+
this.upsert(insertIdx, val, function () {
|
629
|
+
throw "duplicate index"
|
630
|
+
})
|
631
|
+
}
|
632
|
+
|
633
|
+
/**
|
634
|
+
* Inserts or updates an existing index within the vector.
|
635
|
+
*
|
636
|
+
* @param {Number} insertIdx - The index at which the element should be inserted.
|
637
|
+
* @param {Number} val - The value to be inserted into the vector.
|
638
|
+
* @param {function} fn - A function that is called for updates, the existing value and the
|
639
|
+
* requested value are passed as arguments
|
640
|
+
*/
|
641
|
+
lunr.Vector.prototype.upsert = function (insertIdx, val, fn) {
|
642
|
+
this._magnitude = 0
|
643
|
+
var position = this.positionForIndex(insertIdx)
|
644
|
+
|
645
|
+
if (this.elements[position] == insertIdx) {
|
646
|
+
this.elements[position + 1] = fn(this.elements[position + 1], val)
|
647
|
+
} else {
|
648
|
+
this.elements.splice(position, 0, insertIdx, val)
|
649
|
+
}
|
650
|
+
}
|
651
|
+
|
652
|
+
/**
|
653
|
+
* Calculates the magnitude of this vector.
|
654
|
+
*
|
655
|
+
* @returns {Number}
|
656
|
+
*/
|
657
|
+
lunr.Vector.prototype.magnitude = function () {
|
658
|
+
if (this._magnitude) return this._magnitude
|
659
|
+
|
660
|
+
var sumOfSquares = 0,
|
661
|
+
elementsLength = this.elements.length
|
662
|
+
|
663
|
+
for (var i = 1; i < elementsLength; i += 2) {
|
664
|
+
var val = this.elements[i]
|
665
|
+
sumOfSquares += val * val
|
666
|
+
}
|
667
|
+
|
668
|
+
return this._magnitude = Math.sqrt(sumOfSquares)
|
669
|
+
}
|
670
|
+
|
671
|
+
/**
|
672
|
+
* Calculates the dot product of this vector and another vector.
|
673
|
+
*
|
674
|
+
* @param {lunr.Vector} otherVector - The vector to compute the dot product with.
|
675
|
+
* @returns {Number}
|
676
|
+
*/
|
677
|
+
lunr.Vector.prototype.dot = function (otherVector) {
|
678
|
+
var dotProduct = 0,
|
679
|
+
a = this.elements, b = otherVector.elements,
|
680
|
+
aLen = a.length, bLen = b.length,
|
681
|
+
aVal = 0, bVal = 0,
|
682
|
+
i = 0, j = 0
|
683
|
+
|
684
|
+
while (i < aLen && j < bLen) {
|
685
|
+
aVal = a[i], bVal = b[j]
|
686
|
+
if (aVal < bVal) {
|
687
|
+
i += 2
|
688
|
+
} else if (aVal > bVal) {
|
689
|
+
j += 2
|
690
|
+
} else if (aVal == bVal) {
|
691
|
+
dotProduct += a[i + 1] * b[j + 1]
|
692
|
+
i += 2
|
693
|
+
j += 2
|
694
|
+
}
|
695
|
+
}
|
696
|
+
|
697
|
+
return dotProduct
|
698
|
+
}
|
699
|
+
|
700
|
+
/**
|
701
|
+
* Calculates the cosine similarity between this vector and another
|
702
|
+
* vector.
|
703
|
+
*
|
704
|
+
* @param {lunr.Vector} otherVector - The other vector to calculate the
|
705
|
+
* similarity with.
|
706
|
+
* @returns {Number}
|
707
|
+
*/
|
708
|
+
lunr.Vector.prototype.similarity = function (otherVector) {
|
709
|
+
return this.dot(otherVector) / (this.magnitude() * otherVector.magnitude())
|
710
|
+
}
|
711
|
+
|
712
|
+
/**
|
713
|
+
* Converts the vector to an array of the elements within the vector.
|
714
|
+
*
|
715
|
+
* @returns {Number[]}
|
716
|
+
*/
|
717
|
+
lunr.Vector.prototype.toArray = function () {
|
718
|
+
var output = new Array (this.elements.length / 2)
|
719
|
+
|
720
|
+
for (var i = 1, j = 0; i < this.elements.length; i += 2, j++) {
|
721
|
+
output[j] = this.elements[i]
|
722
|
+
}
|
723
|
+
|
724
|
+
return output
|
725
|
+
}
|
726
|
+
|
727
|
+
/**
|
728
|
+
* A JSON serializable representation of the vector.
|
729
|
+
*
|
730
|
+
* @returns {Number[]}
|
731
|
+
*/
|
732
|
+
lunr.Vector.prototype.toJSON = function () {
|
733
|
+
return this.elements
|
734
|
+
}
|
735
|
+
/* eslint-disable */
|
736
|
+
/*!
|
737
|
+
* lunr.stemmer
|
738
|
+
* Copyright (C) 2018 Oliver Nightingale
|
739
|
+
* Includes code from - http://tartarus.org/~martin/PorterStemmer/js.txt
|
740
|
+
*/
|
741
|
+
|
742
|
+
/**
|
743
|
+
* lunr.stemmer is an english language stemmer, this is a JavaScript
|
744
|
+
* implementation of the PorterStemmer taken from http://tartarus.org/~martin
|
745
|
+
*
|
746
|
+
* @static
|
747
|
+
* @implements {lunr.PipelineFunction}
|
748
|
+
* @param {lunr.Token} token - The string to stem
|
749
|
+
* @returns {lunr.Token}
|
750
|
+
* @see {@link lunr.Pipeline}
|
751
|
+
*/
|
752
|
+
lunr.stemmer = (function(){
|
753
|
+
var step2list = {
|
754
|
+
"ational" : "ate",
|
755
|
+
"tional" : "tion",
|
756
|
+
"enci" : "ence",
|
757
|
+
"anci" : "ance",
|
758
|
+
"izer" : "ize",
|
759
|
+
"bli" : "ble",
|
760
|
+
"alli" : "al",
|
761
|
+
"entli" : "ent",
|
762
|
+
"eli" : "e",
|
763
|
+
"ousli" : "ous",
|
764
|
+
"ization" : "ize",
|
765
|
+
"ation" : "ate",
|
766
|
+
"ator" : "ate",
|
767
|
+
"alism" : "al",
|
768
|
+
"iveness" : "ive",
|
769
|
+
"fulness" : "ful",
|
770
|
+
"ousness" : "ous",
|
771
|
+
"aliti" : "al",
|
772
|
+
"iviti" : "ive",
|
773
|
+
"biliti" : "ble",
|
774
|
+
"logi" : "log"
|
775
|
+
},
|
776
|
+
|
777
|
+
step3list = {
|
778
|
+
"icate" : "ic",
|
779
|
+
"ative" : "",
|
780
|
+
"alize" : "al",
|
781
|
+
"iciti" : "ic",
|
782
|
+
"ical" : "ic",
|
783
|
+
"ful" : "",
|
784
|
+
"ness" : ""
|
785
|
+
},
|
786
|
+
|
787
|
+
c = "[^aeiou]", // consonant
|
788
|
+
v = "[aeiouy]", // vowel
|
789
|
+
C = c + "[^aeiouy]*", // consonant sequence
|
790
|
+
V = v + "[aeiou]*", // vowel sequence
|
791
|
+
|
792
|
+
mgr0 = "^(" + C + ")?" + V + C, // [C]VC... is m>0
|
793
|
+
meq1 = "^(" + C + ")?" + V + C + "(" + V + ")?$", // [C]VC[V] is m=1
|
794
|
+
mgr1 = "^(" + C + ")?" + V + C + V + C, // [C]VCVC... is m>1
|
795
|
+
s_v = "^(" + C + ")?" + v; // vowel in stem
|
796
|
+
|
797
|
+
var re_mgr0 = new RegExp(mgr0);
|
798
|
+
var re_mgr1 = new RegExp(mgr1);
|
799
|
+
var re_meq1 = new RegExp(meq1);
|
800
|
+
var re_s_v = new RegExp(s_v);
|
801
|
+
|
802
|
+
var re_1a = /^(.+?)(ss|i)es$/;
|
803
|
+
var re2_1a = /^(.+?)([^s])s$/;
|
804
|
+
var re_1b = /^(.+?)eed$/;
|
805
|
+
var re2_1b = /^(.+?)(ed|ing)$/;
|
806
|
+
var re_1b_2 = /.$/;
|
807
|
+
var re2_1b_2 = /(at|bl|iz)$/;
|
808
|
+
var re3_1b_2 = new RegExp("([^aeiouylsz])\\1$");
|
809
|
+
var re4_1b_2 = new RegExp("^" + C + v + "[^aeiouwxy]$");
|
810
|
+
|
811
|
+
var re_1c = /^(.+?[^aeiou])y$/;
|
812
|
+
var re_2 = /^(.+?)(ational|tional|enci|anci|izer|bli|alli|entli|eli|ousli|ization|ation|ator|alism|iveness|fulness|ousness|aliti|iviti|biliti|logi)$/;
|
813
|
+
|
814
|
+
var re_3 = /^(.+?)(icate|ative|alize|iciti|ical|ful|ness)$/;
|
815
|
+
|
816
|
+
var re_4 = /^(.+?)(al|ance|ence|er|ic|able|ible|ant|ement|ment|ent|ou|ism|ate|iti|ous|ive|ize)$/;
|
817
|
+
var re2_4 = /^(.+?)(s|t)(ion)$/;
|
818
|
+
|
819
|
+
var re_5 = /^(.+?)e$/;
|
820
|
+
var re_5_1 = /ll$/;
|
821
|
+
var re3_5 = new RegExp("^" + C + v + "[^aeiouwxy]$");
|
822
|
+
|
823
|
+
var porterStemmer = function porterStemmer(w) {
|
824
|
+
var stem,
|
825
|
+
suffix,
|
826
|
+
firstch,
|
827
|
+
re,
|
828
|
+
re2,
|
829
|
+
re3,
|
830
|
+
re4;
|
831
|
+
|
832
|
+
if (w.length < 3) { return w; }
|
833
|
+
|
834
|
+
firstch = w.substr(0,1);
|
835
|
+
if (firstch == "y") {
|
836
|
+
w = firstch.toUpperCase() + w.substr(1);
|
837
|
+
}
|
838
|
+
|
839
|
+
// Step 1a
|
840
|
+
re = re_1a
|
841
|
+
re2 = re2_1a;
|
842
|
+
|
843
|
+
if (re.test(w)) { w = w.replace(re,"$1$2"); }
|
844
|
+
else if (re2.test(w)) { w = w.replace(re2,"$1$2"); }
|
845
|
+
|
846
|
+
// Step 1b
|
847
|
+
re = re_1b;
|
848
|
+
re2 = re2_1b;
|
849
|
+
if (re.test(w)) {
|
850
|
+
var fp = re.exec(w);
|
851
|
+
re = re_mgr0;
|
852
|
+
if (re.test(fp[1])) {
|
853
|
+
re = re_1b_2;
|
854
|
+
w = w.replace(re,"");
|
855
|
+
}
|
856
|
+
} else if (re2.test(w)) {
|
857
|
+
var fp = re2.exec(w);
|
858
|
+
stem = fp[1];
|
859
|
+
re2 = re_s_v;
|
860
|
+
if (re2.test(stem)) {
|
861
|
+
w = stem;
|
862
|
+
re2 = re2_1b_2;
|
863
|
+
re3 = re3_1b_2;
|
864
|
+
re4 = re4_1b_2;
|
865
|
+
if (re2.test(w)) { w = w + "e"; }
|
866
|
+
else if (re3.test(w)) { re = re_1b_2; w = w.replace(re,""); }
|
867
|
+
else if (re4.test(w)) { w = w + "e"; }
|
868
|
+
}
|
869
|
+
}
|
870
|
+
|
871
|
+
// Step 1c - replace suffix y or Y by i if preceded by a non-vowel which is not the first letter of the word (so cry -> cri, by -> by, say -> say)
|
872
|
+
re = re_1c;
|
873
|
+
if (re.test(w)) {
|
874
|
+
var fp = re.exec(w);
|
875
|
+
stem = fp[1];
|
876
|
+
w = stem + "i";
|
877
|
+
}
|
878
|
+
|
879
|
+
// Step 2
|
880
|
+
re = re_2;
|
881
|
+
if (re.test(w)) {
|
882
|
+
var fp = re.exec(w);
|
883
|
+
stem = fp[1];
|
884
|
+
suffix = fp[2];
|
885
|
+
re = re_mgr0;
|
886
|
+
if (re.test(stem)) {
|
887
|
+
w = stem + step2list[suffix];
|
888
|
+
}
|
889
|
+
}
|
890
|
+
|
891
|
+
// Step 3
|
892
|
+
re = re_3;
|
893
|
+
if (re.test(w)) {
|
894
|
+
var fp = re.exec(w);
|
895
|
+
stem = fp[1];
|
896
|
+
suffix = fp[2];
|
897
|
+
re = re_mgr0;
|
898
|
+
if (re.test(stem)) {
|
899
|
+
w = stem + step3list[suffix];
|
900
|
+
}
|
901
|
+
}
|
902
|
+
|
903
|
+
// Step 4
|
904
|
+
re = re_4;
|
905
|
+
re2 = re2_4;
|
906
|
+
if (re.test(w)) {
|
907
|
+
var fp = re.exec(w);
|
908
|
+
stem = fp[1];
|
909
|
+
re = re_mgr1;
|
910
|
+
if (re.test(stem)) {
|
911
|
+
w = stem;
|
912
|
+
}
|
913
|
+
} else if (re2.test(w)) {
|
914
|
+
var fp = re2.exec(w);
|
915
|
+
stem = fp[1] + fp[2];
|
916
|
+
re2 = re_mgr1;
|
917
|
+
if (re2.test(stem)) {
|
918
|
+
w = stem;
|
919
|
+
}
|
920
|
+
}
|
921
|
+
|
922
|
+
// Step 5
|
923
|
+
re = re_5;
|
924
|
+
if (re.test(w)) {
|
925
|
+
var fp = re.exec(w);
|
926
|
+
stem = fp[1];
|
927
|
+
re = re_mgr1;
|
928
|
+
re2 = re_meq1;
|
929
|
+
re3 = re3_5;
|
930
|
+
if (re.test(stem) || (re2.test(stem) && !(re3.test(stem)))) {
|
931
|
+
w = stem;
|
932
|
+
}
|
933
|
+
}
|
934
|
+
|
935
|
+
re = re_5_1;
|
936
|
+
re2 = re_mgr1;
|
937
|
+
if (re.test(w) && re2.test(w)) {
|
938
|
+
re = re_1b_2;
|
939
|
+
w = w.replace(re,"");
|
940
|
+
}
|
941
|
+
|
942
|
+
// and turn initial Y back to y
|
943
|
+
|
944
|
+
if (firstch == "y") {
|
945
|
+
w = firstch.toLowerCase() + w.substr(1);
|
946
|
+
}
|
947
|
+
|
948
|
+
return w;
|
949
|
+
};
|
950
|
+
|
951
|
+
return function (token) {
|
952
|
+
return token.update(porterStemmer);
|
953
|
+
}
|
954
|
+
})();
|
955
|
+
|
956
|
+
lunr.Pipeline.registerFunction(lunr.stemmer, 'stemmer')
|
957
|
+
/*!
|
958
|
+
* lunr.stopWordFilter
|
959
|
+
* Copyright (C) 2018 Oliver Nightingale
|
960
|
+
*/
|
961
|
+
|
962
|
+
/**
|
963
|
+
* lunr.generateStopWordFilter builds a stopWordFilter function from the provided
|
964
|
+
* list of stop words.
|
965
|
+
*
|
966
|
+
* The built in lunr.stopWordFilter is built using this generator and can be used
|
967
|
+
* to generate custom stopWordFilters for applications or non English languages.
|
968
|
+
*
|
969
|
+
* @param {Array} token The token to pass through the filter
|
970
|
+
* @returns {lunr.PipelineFunction}
|
971
|
+
* @see lunr.Pipeline
|
972
|
+
* @see lunr.stopWordFilter
|
973
|
+
*/
|
974
|
+
lunr.generateStopWordFilter = function (stopWords) {
|
975
|
+
var words = stopWords.reduce(function (memo, stopWord) {
|
976
|
+
memo[stopWord] = stopWord
|
977
|
+
return memo
|
978
|
+
}, {})
|
979
|
+
|
980
|
+
return function (token) {
|
981
|
+
if (token && words[token.toString()] !== token.toString()) return token
|
982
|
+
}
|
983
|
+
}
|
984
|
+
|
985
|
+
/**
|
986
|
+
* lunr.stopWordFilter is an English language stop word list filter, any words
|
987
|
+
* contained in the list will not be passed through the filter.
|
988
|
+
*
|
989
|
+
* This is intended to be used in the Pipeline. If the token does not pass the
|
990
|
+
* filter then undefined will be returned.
|
991
|
+
*
|
992
|
+
* @implements {lunr.PipelineFunction}
|
993
|
+
* @params {lunr.Token} token - A token to check for being a stop word.
|
994
|
+
* @returns {lunr.Token}
|
995
|
+
* @see {@link lunr.Pipeline}
|
996
|
+
*/
|
997
|
+
lunr.stopWordFilter = lunr.generateStopWordFilter([
|
998
|
+
'a',
|
999
|
+
'able',
|
1000
|
+
'about',
|
1001
|
+
'across',
|
1002
|
+
'after',
|
1003
|
+
'all',
|
1004
|
+
'almost',
|
1005
|
+
'also',
|
1006
|
+
'am',
|
1007
|
+
'among',
|
1008
|
+
'an',
|
1009
|
+
'and',
|
1010
|
+
'any',
|
1011
|
+
'are',
|
1012
|
+
'as',
|
1013
|
+
'at',
|
1014
|
+
'be',
|
1015
|
+
'because',
|
1016
|
+
'been',
|
1017
|
+
'but',
|
1018
|
+
'by',
|
1019
|
+
'can',
|
1020
|
+
'cannot',
|
1021
|
+
'could',
|
1022
|
+
'dear',
|
1023
|
+
'did',
|
1024
|
+
'do',
|
1025
|
+
'does',
|
1026
|
+
'either',
|
1027
|
+
'else',
|
1028
|
+
'ever',
|
1029
|
+
'every',
|
1030
|
+
'for',
|
1031
|
+
'from',
|
1032
|
+
'get',
|
1033
|
+
'got',
|
1034
|
+
'had',
|
1035
|
+
'has',
|
1036
|
+
'have',
|
1037
|
+
'he',
|
1038
|
+
'her',
|
1039
|
+
'hers',
|
1040
|
+
'him',
|
1041
|
+
'his',
|
1042
|
+
'how',
|
1043
|
+
'however',
|
1044
|
+
'i',
|
1045
|
+
'if',
|
1046
|
+
'in',
|
1047
|
+
'into',
|
1048
|
+
'is',
|
1049
|
+
'it',
|
1050
|
+
'its',
|
1051
|
+
'just',
|
1052
|
+
'least',
|
1053
|
+
'let',
|
1054
|
+
'like',
|
1055
|
+
'likely',
|
1056
|
+
'may',
|
1057
|
+
'me',
|
1058
|
+
'might',
|
1059
|
+
'most',
|
1060
|
+
'must',
|
1061
|
+
'my',
|
1062
|
+
'neither',
|
1063
|
+
'no',
|
1064
|
+
'nor',
|
1065
|
+
'not',
|
1066
|
+
'of',
|
1067
|
+
'off',
|
1068
|
+
'often',
|
1069
|
+
'on',
|
1070
|
+
'only',
|
1071
|
+
'or',
|
1072
|
+
'other',
|
1073
|
+
'our',
|
1074
|
+
'own',
|
1075
|
+
'rather',
|
1076
|
+
'said',
|
1077
|
+
'say',
|
1078
|
+
'says',
|
1079
|
+
'she',
|
1080
|
+
'should',
|
1081
|
+
'since',
|
1082
|
+
'so',
|
1083
|
+
'some',
|
1084
|
+
'than',
|
1085
|
+
'that',
|
1086
|
+
'the',
|
1087
|
+
'their',
|
1088
|
+
'them',
|
1089
|
+
'then',
|
1090
|
+
'there',
|
1091
|
+
'these',
|
1092
|
+
'they',
|
1093
|
+
'this',
|
1094
|
+
'tis',
|
1095
|
+
'to',
|
1096
|
+
'too',
|
1097
|
+
'twas',
|
1098
|
+
'us',
|
1099
|
+
'wants',
|
1100
|
+
'was',
|
1101
|
+
'we',
|
1102
|
+
'were',
|
1103
|
+
'what',
|
1104
|
+
'when',
|
1105
|
+
'where',
|
1106
|
+
'which',
|
1107
|
+
'while',
|
1108
|
+
'who',
|
1109
|
+
'whom',
|
1110
|
+
'why',
|
1111
|
+
'will',
|
1112
|
+
'with',
|
1113
|
+
'would',
|
1114
|
+
'yet',
|
1115
|
+
'you',
|
1116
|
+
'your'
|
1117
|
+
])
|
1118
|
+
|
1119
|
+
lunr.Pipeline.registerFunction(lunr.stopWordFilter, 'stopWordFilter')
|
1120
|
+
/*!
|
1121
|
+
* lunr.trimmer
|
1122
|
+
* Copyright (C) 2018 Oliver Nightingale
|
1123
|
+
*/
|
1124
|
+
|
1125
|
+
/**
|
1126
|
+
* lunr.trimmer is a pipeline function for trimming non word
|
1127
|
+
* characters from the beginning and end of tokens before they
|
1128
|
+
* enter the index.
|
1129
|
+
*
|
1130
|
+
* This implementation may not work correctly for non latin
|
1131
|
+
* characters and should either be removed or adapted for use
|
1132
|
+
* with languages with non-latin characters.
|
1133
|
+
*
|
1134
|
+
* @static
|
1135
|
+
* @implements {lunr.PipelineFunction}
|
1136
|
+
* @param {lunr.Token} token The token to pass through the filter
|
1137
|
+
* @returns {lunr.Token}
|
1138
|
+
* @see lunr.Pipeline
|
1139
|
+
*/
|
1140
|
+
lunr.trimmer = function (token) {
|
1141
|
+
return token.update(function (s) {
|
1142
|
+
return s.replace(/^\W+/, '').replace(/\W+$/, '')
|
1143
|
+
})
|
1144
|
+
}
|
1145
|
+
|
1146
|
+
lunr.Pipeline.registerFunction(lunr.trimmer, 'trimmer')
|
1147
|
+
/*!
|
1148
|
+
* lunr.TokenSet
|
1149
|
+
* Copyright (C) 2018 Oliver Nightingale
|
1150
|
+
*/
|
1151
|
+
|
1152
|
+
/**
|
1153
|
+
* A token set is used to store the unique list of all tokens
|
1154
|
+
* within an index. Token sets are also used to represent an
|
1155
|
+
* incoming query to the index, this query token set and index
|
1156
|
+
* token set are then intersected to find which tokens to look
|
1157
|
+
* up in the inverted index.
|
1158
|
+
*
|
1159
|
+
* A token set can hold multiple tokens, as in the case of the
|
1160
|
+
* index token set, or it can hold a single token as in the
|
1161
|
+
* case of a simple query token set.
|
1162
|
+
*
|
1163
|
+
* Additionally token sets are used to perform wildcard matching.
|
1164
|
+
* Leading, contained and trailing wildcards are supported, and
|
1165
|
+
* from this edit distance matching can also be provided.
|
1166
|
+
*
|
1167
|
+
* Token sets are implemented as a minimal finite state automata,
|
1168
|
+
* where both common prefixes and suffixes are shared between tokens.
|
1169
|
+
* This helps to reduce the space used for storing the token set.
|
1170
|
+
*
|
1171
|
+
* @constructor
|
1172
|
+
*/
|
1173
|
+
lunr.TokenSet = function () {
|
1174
|
+
this.final = false
|
1175
|
+
this.edges = {}
|
1176
|
+
this.id = lunr.TokenSet._nextId
|
1177
|
+
lunr.TokenSet._nextId += 1
|
1178
|
+
}
|
1179
|
+
|
1180
|
+
/**
|
1181
|
+
* Keeps track of the next, auto increment, identifier to assign
|
1182
|
+
* to a new tokenSet.
|
1183
|
+
*
|
1184
|
+
* TokenSets require a unique identifier to be correctly minimised.
|
1185
|
+
*
|
1186
|
+
* @private
|
1187
|
+
*/
|
1188
|
+
lunr.TokenSet._nextId = 1
|
1189
|
+
|
1190
|
+
/**
|
1191
|
+
* Creates a TokenSet instance from the given sorted array of words.
|
1192
|
+
*
|
1193
|
+
* @param {String[]} arr - A sorted array of strings to create the set from.
|
1194
|
+
* @returns {lunr.TokenSet}
|
1195
|
+
* @throws Will throw an error if the input array is not sorted.
|
1196
|
+
*/
|
1197
|
+
lunr.TokenSet.fromArray = function (arr) {
|
1198
|
+
var builder = new lunr.TokenSet.Builder
|
1199
|
+
|
1200
|
+
for (var i = 0, len = arr.length; i < len; i++) {
|
1201
|
+
builder.insert(arr[i])
|
1202
|
+
}
|
1203
|
+
|
1204
|
+
builder.finish()
|
1205
|
+
return builder.root
|
1206
|
+
}
|
1207
|
+
|
1208
|
+
/**
|
1209
|
+
* Creates a token set from a query clause.
|
1210
|
+
*
|
1211
|
+
* @private
|
1212
|
+
* @param {Object} clause - A single clause from lunr.Query.
|
1213
|
+
* @param {string} clause.term - The query clause term.
|
1214
|
+
* @param {number} [clause.editDistance] - The optional edit distance for the term.
|
1215
|
+
* @returns {lunr.TokenSet}
|
1216
|
+
*/
|
1217
|
+
lunr.TokenSet.fromClause = function (clause) {
|
1218
|
+
if ('editDistance' in clause) {
|
1219
|
+
return lunr.TokenSet.fromFuzzyString(clause.term, clause.editDistance)
|
1220
|
+
} else {
|
1221
|
+
return lunr.TokenSet.fromString(clause.term)
|
1222
|
+
}
|
1223
|
+
}
|
1224
|
+
|
1225
|
+
/**
|
1226
|
+
* Creates a token set representing a single string with a specified
|
1227
|
+
* edit distance.
|
1228
|
+
*
|
1229
|
+
* Insertions, deletions, substitutions and transpositions are each
|
1230
|
+
* treated as an edit distance of 1.
|
1231
|
+
*
|
1232
|
+
* Increasing the allowed edit distance will have a dramatic impact
|
1233
|
+
* on the performance of both creating and intersecting these TokenSets.
|
1234
|
+
* It is advised to keep the edit distance less than 3.
|
1235
|
+
*
|
1236
|
+
* @param {string} str - The string to create the token set from.
|
1237
|
+
* @param {number} editDistance - The allowed edit distance to match.
|
1238
|
+
* @returns {lunr.Vector}
|
1239
|
+
*/
|
1240
|
+
lunr.TokenSet.fromFuzzyString = function (str, editDistance) {
|
1241
|
+
var root = new lunr.TokenSet
|
1242
|
+
|
1243
|
+
var stack = [{
|
1244
|
+
node: root,
|
1245
|
+
editsRemaining: editDistance,
|
1246
|
+
str: str
|
1247
|
+
}]
|
1248
|
+
|
1249
|
+
while (stack.length) {
|
1250
|
+
var frame = stack.pop()
|
1251
|
+
|
1252
|
+
// no edit
|
1253
|
+
if (frame.str.length > 0) {
|
1254
|
+
var char = frame.str.charAt(0),
|
1255
|
+
noEditNode
|
1256
|
+
|
1257
|
+
if (char in frame.node.edges) {
|
1258
|
+
noEditNode = frame.node.edges[char]
|
1259
|
+
} else {
|
1260
|
+
noEditNode = new lunr.TokenSet
|
1261
|
+
frame.node.edges[char] = noEditNode
|
1262
|
+
}
|
1263
|
+
|
1264
|
+
if (frame.str.length == 1) {
|
1265
|
+
noEditNode.final = true
|
1266
|
+
} else {
|
1267
|
+
stack.push({
|
1268
|
+
node: noEditNode,
|
1269
|
+
editsRemaining: frame.editsRemaining,
|
1270
|
+
str: frame.str.slice(1)
|
1271
|
+
})
|
1272
|
+
}
|
1273
|
+
}
|
1274
|
+
|
1275
|
+
// deletion
|
1276
|
+
// can only do a deletion if we have enough edits remaining
|
1277
|
+
// and if there are characters left to delete in the string
|
1278
|
+
if (frame.editsRemaining > 0 && frame.str.length > 1) {
|
1279
|
+
var char = frame.str.charAt(1),
|
1280
|
+
deletionNode
|
1281
|
+
|
1282
|
+
if (char in frame.node.edges) {
|
1283
|
+
deletionNode = frame.node.edges[char]
|
1284
|
+
} else {
|
1285
|
+
deletionNode = new lunr.TokenSet
|
1286
|
+
frame.node.edges[char] = deletionNode
|
1287
|
+
}
|
1288
|
+
|
1289
|
+
if (frame.str.length <= 2) {
|
1290
|
+
deletionNode.final = true
|
1291
|
+
} else {
|
1292
|
+
stack.push({
|
1293
|
+
node: deletionNode,
|
1294
|
+
editsRemaining: frame.editsRemaining - 1,
|
1295
|
+
str: frame.str.slice(2)
|
1296
|
+
})
|
1297
|
+
}
|
1298
|
+
}
|
1299
|
+
|
1300
|
+
// deletion
|
1301
|
+
// just removing the last character from the str
|
1302
|
+
if (frame.editsRemaining > 0 && frame.str.length == 1) {
|
1303
|
+
frame.node.final = true
|
1304
|
+
}
|
1305
|
+
|
1306
|
+
// substitution
|
1307
|
+
// can only do a substitution if we have enough edits remaining
|
1308
|
+
// and if there are characters left to substitute
|
1309
|
+
if (frame.editsRemaining > 0 && frame.str.length >= 1) {
|
1310
|
+
if ("*" in frame.node.edges) {
|
1311
|
+
var substitutionNode = frame.node.edges["*"]
|
1312
|
+
} else {
|
1313
|
+
var substitutionNode = new lunr.TokenSet
|
1314
|
+
frame.node.edges["*"] = substitutionNode
|
1315
|
+
}
|
1316
|
+
|
1317
|
+
if (frame.str.length == 1) {
|
1318
|
+
substitutionNode.final = true
|
1319
|
+
} else {
|
1320
|
+
stack.push({
|
1321
|
+
node: substitutionNode,
|
1322
|
+
editsRemaining: frame.editsRemaining - 1,
|
1323
|
+
str: frame.str.slice(1)
|
1324
|
+
})
|
1325
|
+
}
|
1326
|
+
}
|
1327
|
+
|
1328
|
+
// insertion
|
1329
|
+
// can only do insertion if there are edits remaining
|
1330
|
+
if (frame.editsRemaining > 0) {
|
1331
|
+
if ("*" in frame.node.edges) {
|
1332
|
+
var insertionNode = frame.node.edges["*"]
|
1333
|
+
} else {
|
1334
|
+
var insertionNode = new lunr.TokenSet
|
1335
|
+
frame.node.edges["*"] = insertionNode
|
1336
|
+
}
|
1337
|
+
|
1338
|
+
if (frame.str.length == 0) {
|
1339
|
+
insertionNode.final = true
|
1340
|
+
} else {
|
1341
|
+
stack.push({
|
1342
|
+
node: insertionNode,
|
1343
|
+
editsRemaining: frame.editsRemaining - 1,
|
1344
|
+
str: frame.str
|
1345
|
+
})
|
1346
|
+
}
|
1347
|
+
}
|
1348
|
+
|
1349
|
+
// transposition
|
1350
|
+
// can only do a transposition if there are edits remaining
|
1351
|
+
// and there are enough characters to transpose
|
1352
|
+
if (frame.editsRemaining > 0 && frame.str.length > 1) {
|
1353
|
+
var charA = frame.str.charAt(0),
|
1354
|
+
charB = frame.str.charAt(1),
|
1355
|
+
transposeNode
|
1356
|
+
|
1357
|
+
if (charB in frame.node.edges) {
|
1358
|
+
transposeNode = frame.node.edges[charB]
|
1359
|
+
} else {
|
1360
|
+
transposeNode = new lunr.TokenSet
|
1361
|
+
frame.node.edges[charB] = transposeNode
|
1362
|
+
}
|
1363
|
+
|
1364
|
+
if (frame.str.length == 1) {
|
1365
|
+
transposeNode.final = true
|
1366
|
+
} else {
|
1367
|
+
stack.push({
|
1368
|
+
node: transposeNode,
|
1369
|
+
editsRemaining: frame.editsRemaining - 1,
|
1370
|
+
str: charA + frame.str.slice(2)
|
1371
|
+
})
|
1372
|
+
}
|
1373
|
+
}
|
1374
|
+
}
|
1375
|
+
|
1376
|
+
return root
|
1377
|
+
}
|
1378
|
+
|
1379
|
+
/**
|
1380
|
+
* Creates a TokenSet from a string.
|
1381
|
+
*
|
1382
|
+
* The string may contain one or more wildcard characters (*)
|
1383
|
+
* that will allow wildcard matching when intersecting with
|
1384
|
+
* another TokenSet.
|
1385
|
+
*
|
1386
|
+
* @param {string} str - The string to create a TokenSet from.
|
1387
|
+
* @returns {lunr.TokenSet}
|
1388
|
+
*/
|
1389
|
+
lunr.TokenSet.fromString = function (str) {
|
1390
|
+
var node = new lunr.TokenSet,
|
1391
|
+
root = node,
|
1392
|
+
wildcardFound = false
|
1393
|
+
|
1394
|
+
/*
|
1395
|
+
* Iterates through all characters within the passed string
|
1396
|
+
* appending a node for each character.
|
1397
|
+
*
|
1398
|
+
* As soon as a wildcard character is found then a self
|
1399
|
+
* referencing edge is introduced to continually match
|
1400
|
+
* any number of any characters.
|
1401
|
+
*/
|
1402
|
+
for (var i = 0, len = str.length; i < len; i++) {
|
1403
|
+
var char = str[i],
|
1404
|
+
final = (i == len - 1)
|
1405
|
+
|
1406
|
+
if (char == "*") {
|
1407
|
+
wildcardFound = true
|
1408
|
+
node.edges[char] = node
|
1409
|
+
node.final = final
|
1410
|
+
|
1411
|
+
} else {
|
1412
|
+
var next = new lunr.TokenSet
|
1413
|
+
next.final = final
|
1414
|
+
|
1415
|
+
node.edges[char] = next
|
1416
|
+
node = next
|
1417
|
+
|
1418
|
+
// TODO: is this needed anymore?
|
1419
|
+
if (wildcardFound) {
|
1420
|
+
node.edges["*"] = root
|
1421
|
+
}
|
1422
|
+
}
|
1423
|
+
}
|
1424
|
+
|
1425
|
+
return root
|
1426
|
+
}
|
1427
|
+
|
1428
|
+
/**
|
1429
|
+
* Converts this TokenSet into an array of strings
|
1430
|
+
* contained within the TokenSet.
|
1431
|
+
*
|
1432
|
+
* @returns {string[]}
|
1433
|
+
*/
|
1434
|
+
lunr.TokenSet.prototype.toArray = function () {
|
1435
|
+
var words = []
|
1436
|
+
|
1437
|
+
var stack = [{
|
1438
|
+
prefix: "",
|
1439
|
+
node: this
|
1440
|
+
}]
|
1441
|
+
|
1442
|
+
while (stack.length) {
|
1443
|
+
var frame = stack.pop(),
|
1444
|
+
edges = Object.keys(frame.node.edges),
|
1445
|
+
len = edges.length
|
1446
|
+
|
1447
|
+
if (frame.node.final) {
|
1448
|
+
words.push(frame.prefix)
|
1449
|
+
}
|
1450
|
+
|
1451
|
+
for (var i = 0; i < len; i++) {
|
1452
|
+
var edge = edges[i]
|
1453
|
+
|
1454
|
+
stack.push({
|
1455
|
+
prefix: frame.prefix.concat(edge),
|
1456
|
+
node: frame.node.edges[edge]
|
1457
|
+
})
|
1458
|
+
}
|
1459
|
+
}
|
1460
|
+
|
1461
|
+
return words
|
1462
|
+
}
|
1463
|
+
|
1464
|
+
/**
|
1465
|
+
* Generates a string representation of a TokenSet.
|
1466
|
+
*
|
1467
|
+
* This is intended to allow TokenSets to be used as keys
|
1468
|
+
* in objects, largely to aid the construction and minimisation
|
1469
|
+
* of a TokenSet. As such it is not designed to be a human
|
1470
|
+
* friendly representation of the TokenSet.
|
1471
|
+
*
|
1472
|
+
* @returns {string}
|
1473
|
+
*/
|
1474
|
+
lunr.TokenSet.prototype.toString = function () {
|
1475
|
+
// NOTE: Using Object.keys here as this.edges is very likely
|
1476
|
+
// to enter 'hash-mode' with many keys being added
|
1477
|
+
//
|
1478
|
+
// avoiding a for-in loop here as it leads to the function
|
1479
|
+
// being de-optimised (at least in V8). From some simple
|
1480
|
+
// benchmarks the performance is comparable, but allowing
|
1481
|
+
// V8 to optimize may mean easy performance wins in the future.
|
1482
|
+
|
1483
|
+
if (this._str) {
|
1484
|
+
return this._str
|
1485
|
+
}
|
1486
|
+
|
1487
|
+
var str = this.final ? '1' : '0',
|
1488
|
+
labels = Object.keys(this.edges).sort(),
|
1489
|
+
len = labels.length
|
1490
|
+
|
1491
|
+
for (var i = 0; i < len; i++) {
|
1492
|
+
var label = labels[i],
|
1493
|
+
node = this.edges[label]
|
1494
|
+
|
1495
|
+
str = str + label + node.id
|
1496
|
+
}
|
1497
|
+
|
1498
|
+
return str
|
1499
|
+
}
|
1500
|
+
|
1501
|
+
/**
|
1502
|
+
* Returns a new TokenSet that is the intersection of
|
1503
|
+
* this TokenSet and the passed TokenSet.
|
1504
|
+
*
|
1505
|
+
* This intersection will take into account any wildcards
|
1506
|
+
* contained within the TokenSet.
|
1507
|
+
*
|
1508
|
+
* @param {lunr.TokenSet} b - An other TokenSet to intersect with.
|
1509
|
+
* @returns {lunr.TokenSet}
|
1510
|
+
*/
|
1511
|
+
lunr.TokenSet.prototype.intersect = function (b) {
|
1512
|
+
var output = new lunr.TokenSet,
|
1513
|
+
frame = undefined
|
1514
|
+
|
1515
|
+
var stack = [{
|
1516
|
+
qNode: b,
|
1517
|
+
output: output,
|
1518
|
+
node: this
|
1519
|
+
}]
|
1520
|
+
|
1521
|
+
while (stack.length) {
|
1522
|
+
frame = stack.pop()
|
1523
|
+
|
1524
|
+
// NOTE: As with the #toString method, we are using
|
1525
|
+
// Object.keys and a for loop instead of a for-in loop
|
1526
|
+
// as both of these objects enter 'hash' mode, causing
|
1527
|
+
// the function to be de-optimised in V8
|
1528
|
+
var qEdges = Object.keys(frame.qNode.edges),
|
1529
|
+
qLen = qEdges.length,
|
1530
|
+
nEdges = Object.keys(frame.node.edges),
|
1531
|
+
nLen = nEdges.length
|
1532
|
+
|
1533
|
+
for (var q = 0; q < qLen; q++) {
|
1534
|
+
var qEdge = qEdges[q]
|
1535
|
+
|
1536
|
+
for (var n = 0; n < nLen; n++) {
|
1537
|
+
var nEdge = nEdges[n]
|
1538
|
+
|
1539
|
+
if (nEdge == qEdge || qEdge == '*') {
|
1540
|
+
var node = frame.node.edges[nEdge],
|
1541
|
+
qNode = frame.qNode.edges[qEdge],
|
1542
|
+
final = node.final && qNode.final,
|
1543
|
+
next = undefined
|
1544
|
+
|
1545
|
+
if (nEdge in frame.output.edges) {
|
1546
|
+
// an edge already exists for this character
|
1547
|
+
// no need to create a new node, just set the finality
|
1548
|
+
// bit unless this node is already final
|
1549
|
+
next = frame.output.edges[nEdge]
|
1550
|
+
next.final = next.final || final
|
1551
|
+
|
1552
|
+
} else {
|
1553
|
+
// no edge exists yet, must create one
|
1554
|
+
// set the finality bit and insert it
|
1555
|
+
// into the output
|
1556
|
+
next = new lunr.TokenSet
|
1557
|
+
next.final = final
|
1558
|
+
frame.output.edges[nEdge] = next
|
1559
|
+
}
|
1560
|
+
|
1561
|
+
stack.push({
|
1562
|
+
qNode: qNode,
|
1563
|
+
output: next,
|
1564
|
+
node: node
|
1565
|
+
})
|
1566
|
+
}
|
1567
|
+
}
|
1568
|
+
}
|
1569
|
+
}
|
1570
|
+
|
1571
|
+
return output
|
1572
|
+
}
|
1573
|
+
lunr.TokenSet.Builder = function () {
|
1574
|
+
this.previousWord = ""
|
1575
|
+
this.root = new lunr.TokenSet
|
1576
|
+
this.uncheckedNodes = []
|
1577
|
+
this.minimizedNodes = {}
|
1578
|
+
}
|
1579
|
+
|
1580
|
+
lunr.TokenSet.Builder.prototype.insert = function (word) {
|
1581
|
+
var node,
|
1582
|
+
commonPrefix = 0
|
1583
|
+
|
1584
|
+
if (word < this.previousWord) {
|
1585
|
+
throw new Error ("Out of order word insertion")
|
1586
|
+
}
|
1587
|
+
|
1588
|
+
for (var i = 0; i < word.length && i < this.previousWord.length; i++) {
|
1589
|
+
if (word[i] != this.previousWord[i]) break
|
1590
|
+
commonPrefix++
|
1591
|
+
}
|
1592
|
+
|
1593
|
+
this.minimize(commonPrefix)
|
1594
|
+
|
1595
|
+
if (this.uncheckedNodes.length == 0) {
|
1596
|
+
node = this.root
|
1597
|
+
} else {
|
1598
|
+
node = this.uncheckedNodes[this.uncheckedNodes.length - 1].child
|
1599
|
+
}
|
1600
|
+
|
1601
|
+
for (var i = commonPrefix; i < word.length; i++) {
|
1602
|
+
var nextNode = new lunr.TokenSet,
|
1603
|
+
char = word[i]
|
1604
|
+
|
1605
|
+
node.edges[char] = nextNode
|
1606
|
+
|
1607
|
+
this.uncheckedNodes.push({
|
1608
|
+
parent: node,
|
1609
|
+
char: char,
|
1610
|
+
child: nextNode
|
1611
|
+
})
|
1612
|
+
|
1613
|
+
node = nextNode
|
1614
|
+
}
|
1615
|
+
|
1616
|
+
node.final = true
|
1617
|
+
this.previousWord = word
|
1618
|
+
}
|
1619
|
+
|
1620
|
+
lunr.TokenSet.Builder.prototype.finish = function () {
|
1621
|
+
this.minimize(0)
|
1622
|
+
}
|
1623
|
+
|
1624
|
+
lunr.TokenSet.Builder.prototype.minimize = function (downTo) {
|
1625
|
+
for (var i = this.uncheckedNodes.length - 1; i >= downTo; i--) {
|
1626
|
+
var node = this.uncheckedNodes[i],
|
1627
|
+
childKey = node.child.toString()
|
1628
|
+
|
1629
|
+
if (childKey in this.minimizedNodes) {
|
1630
|
+
node.parent.edges[node.char] = this.minimizedNodes[childKey]
|
1631
|
+
} else {
|
1632
|
+
// Cache the key for this node since
|
1633
|
+
// we know it can't change anymore
|
1634
|
+
node.child._str = childKey
|
1635
|
+
|
1636
|
+
this.minimizedNodes[childKey] = node.child
|
1637
|
+
}
|
1638
|
+
|
1639
|
+
this.uncheckedNodes.pop()
|
1640
|
+
}
|
1641
|
+
}
|
1642
|
+
/*!
|
1643
|
+
* lunr.Index
|
1644
|
+
* Copyright (C) 2018 Oliver Nightingale
|
1645
|
+
*/
|
1646
|
+
|
1647
|
+
/**
|
1648
|
+
* An index contains the built index of all documents and provides a query interface
|
1649
|
+
* to the index.
|
1650
|
+
*
|
1651
|
+
* Usually instances of lunr.Index will not be created using this constructor, instead
|
1652
|
+
* lunr.Builder should be used to construct new indexes, or lunr.Index.load should be
|
1653
|
+
* used to load previously built and serialized indexes.
|
1654
|
+
*
|
1655
|
+
* @constructor
|
1656
|
+
* @param {Object} attrs - The attributes of the built search index.
|
1657
|
+
* @param {Object} attrs.invertedIndex - An index of term/field to document reference.
|
1658
|
+
* @param {Object<string, lunr.Vector>} attrs.documentVectors - Document vectors keyed by document reference.
|
1659
|
+
* @param {lunr.TokenSet} attrs.tokenSet - An set of all corpus tokens.
|
1660
|
+
* @param {string[]} attrs.fields - The names of indexed document fields.
|
1661
|
+
* @param {lunr.Pipeline} attrs.pipeline - The pipeline to use for search terms.
|
1662
|
+
*/
|
1663
|
+
lunr.Index = function (attrs) {
|
1664
|
+
this.invertedIndex = attrs.invertedIndex
|
1665
|
+
this.fieldVectors = attrs.fieldVectors
|
1666
|
+
this.tokenSet = attrs.tokenSet
|
1667
|
+
this.fields = attrs.fields
|
1668
|
+
this.pipeline = attrs.pipeline
|
1669
|
+
}
|
1670
|
+
|
1671
|
+
/**
|
1672
|
+
* A result contains details of a document matching a search query.
|
1673
|
+
* @typedef {Object} lunr.Index~Result
|
1674
|
+
* @property {string} ref - The reference of the document this result represents.
|
1675
|
+
* @property {number} score - A number between 0 and 1 representing how similar this document is to the query.
|
1676
|
+
* @property {lunr.MatchData} matchData - Contains metadata about this match including which term(s) caused the match.
|
1677
|
+
*/
|
1678
|
+
|
1679
|
+
/**
|
1680
|
+
* Although lunr provides the ability to create queries using lunr.Query, it also provides a simple
|
1681
|
+
* query language which itself is parsed into an instance of lunr.Query.
|
1682
|
+
*
|
1683
|
+
* For programmatically building queries it is advised to directly use lunr.Query, the query language
|
1684
|
+
* is best used for human entered text rather than program generated text.
|
1685
|
+
*
|
1686
|
+
* At its simplest queries can just be a single term, e.g. `hello`, multiple terms are also supported
|
1687
|
+
* and will be combined with OR, e.g `hello world` will match documents that contain either 'hello'
|
1688
|
+
* or 'world', though those that contain both will rank higher in the results.
|
1689
|
+
*
|
1690
|
+
* Wildcards can be included in terms to match one or more unspecified characters, these wildcards can
|
1691
|
+
* be inserted anywhere within the term, and more than one wildcard can exist in a single term. Adding
|
1692
|
+
* wildcards will increase the number of documents that will be found but can also have a negative
|
1693
|
+
* impact on query performance, especially with wildcards at the beginning of a term.
|
1694
|
+
*
|
1695
|
+
* Terms can be restricted to specific fields, e.g. `title:hello`, only documents with the term
|
1696
|
+
* hello in the title field will match this query. Using a field not present in the index will lead
|
1697
|
+
* to an error being thrown.
|
1698
|
+
*
|
1699
|
+
* Modifiers can also be added to terms, lunr supports edit distance and boost modifiers on terms. A term
|
1700
|
+
* boost will make documents matching that term score higher, e.g. `foo^5`. Edit distance is also supported
|
1701
|
+
* to provide fuzzy matching, e.g. 'hello~2' will match documents with hello with an edit distance of 2.
|
1702
|
+
* Avoid large values for edit distance to improve query performance.
|
1703
|
+
*
|
1704
|
+
* To escape special characters the backslash character '\' can be used, this allows searches to include
|
1705
|
+
* characters that would normally be considered modifiers, e.g. `foo\~2` will search for a term "foo~2" instead
|
1706
|
+
* of attempting to apply a boost of 2 to the search term "foo".
|
1707
|
+
*
|
1708
|
+
* @typedef {string} lunr.Index~QueryString
|
1709
|
+
* @example <caption>Simple single term query</caption>
|
1710
|
+
* hello
|
1711
|
+
* @example <caption>Multiple term query</caption>
|
1712
|
+
* hello world
|
1713
|
+
* @example <caption>term scoped to a field</caption>
|
1714
|
+
* title:hello
|
1715
|
+
* @example <caption>term with a boost of 10</caption>
|
1716
|
+
* hello^10
|
1717
|
+
* @example <caption>term with an edit distance of 2</caption>
|
1718
|
+
* hello~2
|
1719
|
+
*/
|
1720
|
+
|
1721
|
+
/**
|
1722
|
+
* Performs a search against the index using lunr query syntax.
|
1723
|
+
*
|
1724
|
+
* Results will be returned sorted by their score, the most relevant results
|
1725
|
+
* will be returned first.
|
1726
|
+
*
|
1727
|
+
* For more programmatic querying use lunr.Index#query.
|
1728
|
+
*
|
1729
|
+
* @param {lunr.Index~QueryString} queryString - A string containing a lunr query.
|
1730
|
+
* @throws {lunr.QueryParseError} If the passed query string cannot be parsed.
|
1731
|
+
* @returns {lunr.Index~Result[]}
|
1732
|
+
*/
|
1733
|
+
lunr.Index.prototype.search = function (queryString) {
|
1734
|
+
return this.query(function (query) {
|
1735
|
+
var parser = new lunr.QueryParser(queryString, query)
|
1736
|
+
parser.parse()
|
1737
|
+
})
|
1738
|
+
}
|
1739
|
+
|
1740
|
+
/**
|
1741
|
+
* A query builder callback provides a query object to be used to express
|
1742
|
+
* the query to perform on the index.
|
1743
|
+
*
|
1744
|
+
* @callback lunr.Index~queryBuilder
|
1745
|
+
* @param {lunr.Query} query - The query object to build up.
|
1746
|
+
* @this lunr.Query
|
1747
|
+
*/
|
1748
|
+
|
1749
|
+
/**
|
1750
|
+
* Performs a query against the index using the yielded lunr.Query object.
|
1751
|
+
*
|
1752
|
+
* If performing programmatic queries against the index, this method is preferred
|
1753
|
+
* over lunr.Index#search so as to avoid the additional query parsing overhead.
|
1754
|
+
*
|
1755
|
+
* A query object is yielded to the supplied function which should be used to
|
1756
|
+
* express the query to be run against the index.
|
1757
|
+
*
|
1758
|
+
* Note that although this function takes a callback parameter it is _not_ an
|
1759
|
+
* asynchronous operation, the callback is just yielded a query object to be
|
1760
|
+
* customized.
|
1761
|
+
*
|
1762
|
+
* @param {lunr.Index~queryBuilder} fn - A function that is used to build the query.
|
1763
|
+
* @returns {lunr.Index~Result[]}
|
1764
|
+
*/
|
1765
|
+
lunr.Index.prototype.query = function (fn) {
|
1766
|
+
// for each query clause
|
1767
|
+
// * process terms
|
1768
|
+
// * expand terms from token set
|
1769
|
+
// * find matching documents and metadata
|
1770
|
+
// * get document vectors
|
1771
|
+
// * score documents
|
1772
|
+
|
1773
|
+
var query = new lunr.Query(this.fields),
|
1774
|
+
matchingFields = Object.create(null),
|
1775
|
+
queryVectors = Object.create(null),
|
1776
|
+
termFieldCache = Object.create(null)
|
1777
|
+
|
1778
|
+
fn.call(query, query)
|
1779
|
+
|
1780
|
+
for (var i = 0; i < query.clauses.length; i++) {
|
1781
|
+
/*
|
1782
|
+
* Unless the pipeline has been disabled for this term, which is
|
1783
|
+
* the case for terms with wildcards, we need to pass the clause
|
1784
|
+
* term through the search pipeline. A pipeline returns an array
|
1785
|
+
* of processed terms. Pipeline functions may expand the passed
|
1786
|
+
* term, which means we may end up performing multiple index lookups
|
1787
|
+
* for a single query term.
|
1788
|
+
*/
|
1789
|
+
var clause = query.clauses[i],
|
1790
|
+
terms = null
|
1791
|
+
|
1792
|
+
if (clause.usePipeline) {
|
1793
|
+
terms = this.pipeline.runString(clause.term)
|
1794
|
+
} else {
|
1795
|
+
terms = [clause.term]
|
1796
|
+
}
|
1797
|
+
|
1798
|
+
for (var m = 0; m < terms.length; m++) {
|
1799
|
+
var term = terms[m]
|
1800
|
+
|
1801
|
+
/*
|
1802
|
+
* Each term returned from the pipeline needs to use the same query
|
1803
|
+
* clause object, e.g. the same boost and or edit distance. The
|
1804
|
+
* simplest way to do this is to re-use the clause object but mutate
|
1805
|
+
* its term property.
|
1806
|
+
*/
|
1807
|
+
clause.term = term
|
1808
|
+
|
1809
|
+
/*
|
1810
|
+
* From the term in the clause we create a token set which will then
|
1811
|
+
* be used to intersect the indexes token set to get a list of terms
|
1812
|
+
* to lookup in the inverted index
|
1813
|
+
*/
|
1814
|
+
var termTokenSet = lunr.TokenSet.fromClause(clause),
|
1815
|
+
expandedTerms = this.tokenSet.intersect(termTokenSet).toArray()
|
1816
|
+
|
1817
|
+
for (var j = 0; j < expandedTerms.length; j++) {
|
1818
|
+
/*
|
1819
|
+
* For each term get the posting and termIndex, this is required for
|
1820
|
+
* building the query vector.
|
1821
|
+
*/
|
1822
|
+
var expandedTerm = expandedTerms[j],
|
1823
|
+
posting = this.invertedIndex[expandedTerm],
|
1824
|
+
termIndex = posting._index
|
1825
|
+
|
1826
|
+
for (var k = 0; k < clause.fields.length; k++) {
|
1827
|
+
/*
|
1828
|
+
* For each field that this query term is scoped by (by default
|
1829
|
+
* all fields are in scope) we need to get all the document refs
|
1830
|
+
* that have this term in that field.
|
1831
|
+
*
|
1832
|
+
* The posting is the entry in the invertedIndex for the matching
|
1833
|
+
* term from above.
|
1834
|
+
*/
|
1835
|
+
var field = clause.fields[k],
|
1836
|
+
fieldPosting = posting[field],
|
1837
|
+
matchingDocumentRefs = Object.keys(fieldPosting),
|
1838
|
+
termField = expandedTerm + "/" + field
|
1839
|
+
|
1840
|
+
/*
|
1841
|
+
* To support field level boosts a query vector is created per
|
1842
|
+
* field. This vector is populated using the termIndex found for
|
1843
|
+
* the term and a unit value with the appropriate boost applied.
|
1844
|
+
*
|
1845
|
+
* If the query vector for this field does not exist yet it needs
|
1846
|
+
* to be created.
|
1847
|
+
*/
|
1848
|
+
if (queryVectors[field] === undefined) {
|
1849
|
+
queryVectors[field] = new lunr.Vector
|
1850
|
+
}
|
1851
|
+
|
1852
|
+
/*
|
1853
|
+
* Using upsert because there could already be an entry in the vector
|
1854
|
+
* for the term we are working with. In that case we just add the scores
|
1855
|
+
* together.
|
1856
|
+
*/
|
1857
|
+
queryVectors[field].upsert(termIndex, 1 * clause.boost, function (a, b) { return a + b })
|
1858
|
+
|
1859
|
+
/**
|
1860
|
+
* If we've already seen this term, field combo then we've already collected
|
1861
|
+
* the matching documents and metadata, no need to go through all that again
|
1862
|
+
*/
|
1863
|
+
if (termFieldCache[termField]) {
|
1864
|
+
continue
|
1865
|
+
}
|
1866
|
+
|
1867
|
+
for (var l = 0; l < matchingDocumentRefs.length; l++) {
|
1868
|
+
/*
|
1869
|
+
* All metadata for this term/field/document triple
|
1870
|
+
* are then extracted and collected into an instance
|
1871
|
+
* of lunr.MatchData ready to be returned in the query
|
1872
|
+
* results
|
1873
|
+
*/
|
1874
|
+
var matchingDocumentRef = matchingDocumentRefs[l],
|
1875
|
+
matchingFieldRef = new lunr.FieldRef (matchingDocumentRef, field),
|
1876
|
+
metadata = fieldPosting[matchingDocumentRef],
|
1877
|
+
fieldMatch
|
1878
|
+
|
1879
|
+
if ((fieldMatch = matchingFields[matchingFieldRef]) === undefined) {
|
1880
|
+
matchingFields[matchingFieldRef] = new lunr.MatchData (expandedTerm, field, metadata)
|
1881
|
+
} else {
|
1882
|
+
fieldMatch.add(expandedTerm, field, metadata)
|
1883
|
+
}
|
1884
|
+
|
1885
|
+
}
|
1886
|
+
|
1887
|
+
termFieldCache[termField] = true
|
1888
|
+
}
|
1889
|
+
}
|
1890
|
+
}
|
1891
|
+
}
|
1892
|
+
|
1893
|
+
var matchingFieldRefs = Object.keys(matchingFields),
|
1894
|
+
results = [],
|
1895
|
+
matches = Object.create(null)
|
1896
|
+
|
1897
|
+
for (var i = 0; i < matchingFieldRefs.length; i++) {
|
1898
|
+
/*
|
1899
|
+
* Currently we have document fields that match the query, but we
|
1900
|
+
* need to return documents. The matchData and scores are combined
|
1901
|
+
* from multiple fields belonging to the same document.
|
1902
|
+
*
|
1903
|
+
* Scores are calculated by field, using the query vectors created
|
1904
|
+
* above, and combined into a final document score using addition.
|
1905
|
+
*/
|
1906
|
+
var fieldRef = lunr.FieldRef.fromString(matchingFieldRefs[i]),
|
1907
|
+
docRef = fieldRef.docRef,
|
1908
|
+
fieldVector = this.fieldVectors[fieldRef],
|
1909
|
+
score = queryVectors[fieldRef.fieldName].similarity(fieldVector),
|
1910
|
+
docMatch
|
1911
|
+
|
1912
|
+
if ((docMatch = matches[docRef]) !== undefined) {
|
1913
|
+
docMatch.score += score
|
1914
|
+
docMatch.matchData.combine(matchingFields[fieldRef])
|
1915
|
+
} else {
|
1916
|
+
var match = {
|
1917
|
+
ref: docRef,
|
1918
|
+
score: score,
|
1919
|
+
matchData: matchingFields[fieldRef]
|
1920
|
+
}
|
1921
|
+
matches[docRef] = match
|
1922
|
+
results.push(match)
|
1923
|
+
}
|
1924
|
+
}
|
1925
|
+
|
1926
|
+
/*
|
1927
|
+
* Sort the results objects by score, highest first.
|
1928
|
+
*/
|
1929
|
+
return results.sort(function (a, b) {
|
1930
|
+
return b.score - a.score
|
1931
|
+
})
|
1932
|
+
}
|
1933
|
+
|
1934
|
+
/**
|
1935
|
+
* Prepares the index for JSON serialization.
|
1936
|
+
*
|
1937
|
+
* The schema for this JSON blob will be described in a
|
1938
|
+
* separate JSON schema file.
|
1939
|
+
*
|
1940
|
+
* @returns {Object}
|
1941
|
+
*/
|
1942
|
+
lunr.Index.prototype.toJSON = function () {
|
1943
|
+
var invertedIndex = Object.keys(this.invertedIndex)
|
1944
|
+
.sort()
|
1945
|
+
.map(function (term) {
|
1946
|
+
return [term, this.invertedIndex[term]]
|
1947
|
+
}, this)
|
1948
|
+
|
1949
|
+
var fieldVectors = Object.keys(this.fieldVectors)
|
1950
|
+
.map(function (ref) {
|
1951
|
+
return [ref, this.fieldVectors[ref].toJSON()]
|
1952
|
+
}, this)
|
1953
|
+
|
1954
|
+
return {
|
1955
|
+
version: lunr.version,
|
1956
|
+
fields: this.fields,
|
1957
|
+
fieldVectors: fieldVectors,
|
1958
|
+
invertedIndex: invertedIndex,
|
1959
|
+
pipeline: this.pipeline.toJSON()
|
1960
|
+
}
|
1961
|
+
}
|
1962
|
+
|
1963
|
+
/**
|
1964
|
+
* Loads a previously serialized lunr.Index
|
1965
|
+
*
|
1966
|
+
* @param {Object} serializedIndex - A previously serialized lunr.Index
|
1967
|
+
* @returns {lunr.Index}
|
1968
|
+
*/
|
1969
|
+
lunr.Index.load = function (serializedIndex) {
|
1970
|
+
var attrs = {},
|
1971
|
+
fieldVectors = {},
|
1972
|
+
serializedVectors = serializedIndex.fieldVectors,
|
1973
|
+
invertedIndex = {},
|
1974
|
+
serializedInvertedIndex = serializedIndex.invertedIndex,
|
1975
|
+
tokenSetBuilder = new lunr.TokenSet.Builder,
|
1976
|
+
pipeline = lunr.Pipeline.load(serializedIndex.pipeline)
|
1977
|
+
|
1978
|
+
if (serializedIndex.version != lunr.version) {
|
1979
|
+
lunr.utils.warn("Version mismatch when loading serialised index. Current version of lunr '" + lunr.version + "' does not match serialized index '" + serializedIndex.version + "'")
|
1980
|
+
}
|
1981
|
+
|
1982
|
+
for (var i = 0; i < serializedVectors.length; i++) {
|
1983
|
+
var tuple = serializedVectors[i],
|
1984
|
+
ref = tuple[0],
|
1985
|
+
elements = tuple[1]
|
1986
|
+
|
1987
|
+
fieldVectors[ref] = new lunr.Vector(elements)
|
1988
|
+
}
|
1989
|
+
|
1990
|
+
for (var i = 0; i < serializedInvertedIndex.length; i++) {
|
1991
|
+
var tuple = serializedInvertedIndex[i],
|
1992
|
+
term = tuple[0],
|
1993
|
+
posting = tuple[1]
|
1994
|
+
|
1995
|
+
tokenSetBuilder.insert(term)
|
1996
|
+
invertedIndex[term] = posting
|
1997
|
+
}
|
1998
|
+
|
1999
|
+
tokenSetBuilder.finish()
|
2000
|
+
|
2001
|
+
attrs.fields = serializedIndex.fields
|
2002
|
+
|
2003
|
+
attrs.fieldVectors = fieldVectors
|
2004
|
+
attrs.invertedIndex = invertedIndex
|
2005
|
+
attrs.tokenSet = tokenSetBuilder.root
|
2006
|
+
attrs.pipeline = pipeline
|
2007
|
+
|
2008
|
+
return new lunr.Index(attrs)
|
2009
|
+
}
|
2010
|
+
/*!
|
2011
|
+
* lunr.Builder
|
2012
|
+
* Copyright (C) 2018 Oliver Nightingale
|
2013
|
+
*/
|
2014
|
+
|
2015
|
+
/**
|
2016
|
+
* lunr.Builder performs indexing on a set of documents and
|
2017
|
+
* returns instances of lunr.Index ready for querying.
|
2018
|
+
*
|
2019
|
+
* All configuration of the index is done via the builder, the
|
2020
|
+
* fields to index, the document reference, the text processing
|
2021
|
+
* pipeline and document scoring parameters are all set on the
|
2022
|
+
* builder before indexing.
|
2023
|
+
*
|
2024
|
+
* @constructor
|
2025
|
+
* @property {string} _ref - Internal reference to the document reference field.
|
2026
|
+
* @property {string[]} _fields - Internal reference to the document fields to index.
|
2027
|
+
* @property {object} invertedIndex - The inverted index maps terms to document fields.
|
2028
|
+
* @property {object} documentTermFrequencies - Keeps track of document term frequencies.
|
2029
|
+
* @property {object} documentLengths - Keeps track of the length of documents added to the index.
|
2030
|
+
* @property {lunr.tokenizer} tokenizer - Function for splitting strings into tokens for indexing.
|
2031
|
+
* @property {lunr.Pipeline} pipeline - The pipeline performs text processing on tokens before indexing.
|
2032
|
+
* @property {lunr.Pipeline} searchPipeline - A pipeline for processing search terms before querying the index.
|
2033
|
+
* @property {number} documentCount - Keeps track of the total number of documents indexed.
|
2034
|
+
* @property {number} _b - A parameter to control field length normalization, setting this to 0 disabled normalization, 1 fully normalizes field lengths, the default value is 0.75.
|
2035
|
+
* @property {number} _k1 - A parameter to control how quickly an increase in term frequency results in term frequency saturation, the default value is 1.2.
|
2036
|
+
* @property {number} termIndex - A counter incremented for each unique term, used to identify a terms position in the vector space.
|
2037
|
+
* @property {array} metadataWhitelist - A list of metadata keys that have been whitelisted for entry in the index.
|
2038
|
+
*/
|
2039
|
+
lunr.Builder = function () {
|
2040
|
+
this._ref = "id"
|
2041
|
+
this._fields = []
|
2042
|
+
this.invertedIndex = Object.create(null)
|
2043
|
+
this.fieldTermFrequencies = {}
|
2044
|
+
this.fieldLengths = {}
|
2045
|
+
this.tokenizer = lunr.tokenizer
|
2046
|
+
this.pipeline = new lunr.Pipeline
|
2047
|
+
this.searchPipeline = new lunr.Pipeline
|
2048
|
+
this.documentCount = 0
|
2049
|
+
this._b = 0.75
|
2050
|
+
this._k1 = 1.2
|
2051
|
+
this.termIndex = 0
|
2052
|
+
this.metadataWhitelist = []
|
2053
|
+
}
|
2054
|
+
|
2055
|
+
/**
|
2056
|
+
* Sets the document field used as the document reference. Every document must have this field.
|
2057
|
+
* The type of this field in the document should be a string, if it is not a string it will be
|
2058
|
+
* coerced into a string by calling toString.
|
2059
|
+
*
|
2060
|
+
* The default ref is 'id'.
|
2061
|
+
*
|
2062
|
+
* The ref should _not_ be changed during indexing, it should be set before any documents are
|
2063
|
+
* added to the index. Changing it during indexing can lead to inconsistent results.
|
2064
|
+
*
|
2065
|
+
* @param {string} ref - The name of the reference field in the document.
|
2066
|
+
*/
|
2067
|
+
lunr.Builder.prototype.ref = function (ref) {
|
2068
|
+
this._ref = ref
|
2069
|
+
}
|
2070
|
+
|
2071
|
+
/**
|
2072
|
+
* Adds a field to the list of document fields that will be indexed. Every document being
|
2073
|
+
* indexed should have this field. Null values for this field in indexed documents will
|
2074
|
+
* not cause errors but will limit the chance of that document being retrieved by searches.
|
2075
|
+
*
|
2076
|
+
* All fields should be added before adding documents to the index. Adding fields after
|
2077
|
+
* a document has been indexed will have no effect on already indexed documents.
|
2078
|
+
*
|
2079
|
+
* @param {string} field - The name of a field to index in all documents.
|
2080
|
+
*/
|
2081
|
+
lunr.Builder.prototype.field = function (field) {
|
2082
|
+
this._fields.push(field)
|
2083
|
+
}
|
2084
|
+
|
2085
|
+
/**
|
2086
|
+
* A parameter to tune the amount of field length normalisation that is applied when
|
2087
|
+
* calculating relevance scores. A value of 0 will completely disable any normalisation
|
2088
|
+
* and a value of 1 will fully normalise field lengths. The default is 0.75. Values of b
|
2089
|
+
* will be clamped to the range 0 - 1.
|
2090
|
+
*
|
2091
|
+
* @param {number} number - The value to set for this tuning parameter.
|
2092
|
+
*/
|
2093
|
+
lunr.Builder.prototype.b = function (number) {
|
2094
|
+
if (number < 0) {
|
2095
|
+
this._b = 0
|
2096
|
+
} else if (number > 1) {
|
2097
|
+
this._b = 1
|
2098
|
+
} else {
|
2099
|
+
this._b = number
|
2100
|
+
}
|
2101
|
+
}
|
2102
|
+
|
2103
|
+
/**
|
2104
|
+
* A parameter that controls the speed at which a rise in term frequency results in term
|
2105
|
+
* frequency saturation. The default value is 1.2. Setting this to a higher value will give
|
2106
|
+
* slower saturation levels, a lower value will result in quicker saturation.
|
2107
|
+
*
|
2108
|
+
* @param {number} number - The value to set for this tuning parameter.
|
2109
|
+
*/
|
2110
|
+
lunr.Builder.prototype.k1 = function (number) {
|
2111
|
+
this._k1 = number
|
2112
|
+
}
|
2113
|
+
|
2114
|
+
/**
|
2115
|
+
* Adds a document to the index.
|
2116
|
+
*
|
2117
|
+
* Before adding fields to the index the index should have been fully setup, with the document
|
2118
|
+
* ref and all fields to index already having been specified.
|
2119
|
+
*
|
2120
|
+
* The document must have a field name as specified by the ref (by default this is 'id') and
|
2121
|
+
* it should have all fields defined for indexing, though null or undefined values will not
|
2122
|
+
* cause errors.
|
2123
|
+
*
|
2124
|
+
* @param {object} doc - The document to add to the index.
|
2125
|
+
*/
|
2126
|
+
lunr.Builder.prototype.add = function (doc) {
|
2127
|
+
var docRef = doc[this._ref]
|
2128
|
+
|
2129
|
+
this.documentCount += 1
|
2130
|
+
|
2131
|
+
for (var i = 0; i < this._fields.length; i++) {
|
2132
|
+
var fieldName = this._fields[i],
|
2133
|
+
field = doc[fieldName],
|
2134
|
+
tokens = this.tokenizer(field),
|
2135
|
+
terms = this.pipeline.run(tokens),
|
2136
|
+
fieldRef = new lunr.FieldRef (docRef, fieldName),
|
2137
|
+
fieldTerms = Object.create(null)
|
2138
|
+
|
2139
|
+
this.fieldTermFrequencies[fieldRef] = fieldTerms
|
2140
|
+
this.fieldLengths[fieldRef] = 0
|
2141
|
+
|
2142
|
+
// store the length of this field for this document
|
2143
|
+
this.fieldLengths[fieldRef] += terms.length
|
2144
|
+
|
2145
|
+
// calculate term frequencies for this field
|
2146
|
+
for (var j = 0; j < terms.length; j++) {
|
2147
|
+
var term = terms[j]
|
2148
|
+
|
2149
|
+
if (fieldTerms[term] == undefined) {
|
2150
|
+
fieldTerms[term] = 0
|
2151
|
+
}
|
2152
|
+
|
2153
|
+
fieldTerms[term] += 1
|
2154
|
+
|
2155
|
+
// add to inverted index
|
2156
|
+
// create an initial posting if one doesn't exist
|
2157
|
+
if (this.invertedIndex[term] == undefined) {
|
2158
|
+
var posting = Object.create(null)
|
2159
|
+
posting["_index"] = this.termIndex
|
2160
|
+
this.termIndex += 1
|
2161
|
+
|
2162
|
+
for (var k = 0; k < this._fields.length; k++) {
|
2163
|
+
posting[this._fields[k]] = Object.create(null)
|
2164
|
+
}
|
2165
|
+
|
2166
|
+
this.invertedIndex[term] = posting
|
2167
|
+
}
|
2168
|
+
|
2169
|
+
// add an entry for this term/fieldName/docRef to the invertedIndex
|
2170
|
+
if (this.invertedIndex[term][fieldName][docRef] == undefined) {
|
2171
|
+
this.invertedIndex[term][fieldName][docRef] = Object.create(null)
|
2172
|
+
}
|
2173
|
+
|
2174
|
+
// store all whitelisted metadata about this token in the
|
2175
|
+
// inverted index
|
2176
|
+
for (var l = 0; l < this.metadataWhitelist.length; l++) {
|
2177
|
+
var metadataKey = this.metadataWhitelist[l],
|
2178
|
+
metadata = term.metadata[metadataKey]
|
2179
|
+
|
2180
|
+
if (this.invertedIndex[term][fieldName][docRef][metadataKey] == undefined) {
|
2181
|
+
this.invertedIndex[term][fieldName][docRef][metadataKey] = []
|
2182
|
+
}
|
2183
|
+
|
2184
|
+
this.invertedIndex[term][fieldName][docRef][metadataKey].push(metadata)
|
2185
|
+
}
|
2186
|
+
}
|
2187
|
+
|
2188
|
+
}
|
2189
|
+
}
|
2190
|
+
|
2191
|
+
/**
|
2192
|
+
* Calculates the average document length for this index
|
2193
|
+
*
|
2194
|
+
* @private
|
2195
|
+
*/
|
2196
|
+
lunr.Builder.prototype.calculateAverageFieldLengths = function () {
|
2197
|
+
|
2198
|
+
var fieldRefs = Object.keys(this.fieldLengths),
|
2199
|
+
numberOfFields = fieldRefs.length,
|
2200
|
+
accumulator = {},
|
2201
|
+
documentsWithField = {}
|
2202
|
+
|
2203
|
+
for (var i = 0; i < numberOfFields; i++) {
|
2204
|
+
var fieldRef = lunr.FieldRef.fromString(fieldRefs[i]),
|
2205
|
+
field = fieldRef.fieldName
|
2206
|
+
|
2207
|
+
documentsWithField[field] || (documentsWithField[field] = 0)
|
2208
|
+
documentsWithField[field] += 1
|
2209
|
+
|
2210
|
+
accumulator[field] || (accumulator[field] = 0)
|
2211
|
+
accumulator[field] += this.fieldLengths[fieldRef]
|
2212
|
+
}
|
2213
|
+
|
2214
|
+
for (var i = 0; i < this._fields.length; i++) {
|
2215
|
+
var field = this._fields[i]
|
2216
|
+
accumulator[field] = accumulator[field] / documentsWithField[field]
|
2217
|
+
}
|
2218
|
+
|
2219
|
+
this.averageFieldLength = accumulator
|
2220
|
+
}
|
2221
|
+
|
2222
|
+
/**
|
2223
|
+
* Builds a vector space model of every document using lunr.Vector
|
2224
|
+
*
|
2225
|
+
* @private
|
2226
|
+
*/
|
2227
|
+
lunr.Builder.prototype.createFieldVectors = function () {
|
2228
|
+
var fieldVectors = {},
|
2229
|
+
fieldRefs = Object.keys(this.fieldTermFrequencies),
|
2230
|
+
fieldRefsLength = fieldRefs.length,
|
2231
|
+
termIdfCache = Object.create(null)
|
2232
|
+
|
2233
|
+
for (var i = 0; i < fieldRefsLength; i++) {
|
2234
|
+
var fieldRef = lunr.FieldRef.fromString(fieldRefs[i]),
|
2235
|
+
field = fieldRef.fieldName,
|
2236
|
+
fieldLength = this.fieldLengths[fieldRef],
|
2237
|
+
fieldVector = new lunr.Vector,
|
2238
|
+
termFrequencies = this.fieldTermFrequencies[fieldRef],
|
2239
|
+
terms = Object.keys(termFrequencies),
|
2240
|
+
termsLength = terms.length
|
2241
|
+
|
2242
|
+
for (var j = 0; j < termsLength; j++) {
|
2243
|
+
var term = terms[j],
|
2244
|
+
tf = termFrequencies[term],
|
2245
|
+
termIndex = this.invertedIndex[term]._index,
|
2246
|
+
idf, score, scoreWithPrecision
|
2247
|
+
|
2248
|
+
if (termIdfCache[term] === undefined) {
|
2249
|
+
idf = lunr.idf(this.invertedIndex[term], this.documentCount)
|
2250
|
+
termIdfCache[term] = idf
|
2251
|
+
} else {
|
2252
|
+
idf = termIdfCache[term]
|
2253
|
+
}
|
2254
|
+
|
2255
|
+
score = idf * ((this._k1 + 1) * tf) / (this._k1 * (1 - this._b + this._b * (fieldLength / this.averageFieldLength[field])) + tf)
|
2256
|
+
scoreWithPrecision = Math.round(score * 1000) / 1000
|
2257
|
+
// Converts 1.23456789 to 1.234.
|
2258
|
+
// Reducing the precision so that the vectors take up less
|
2259
|
+
// space when serialised. Doing it now so that they behave
|
2260
|
+
// the same before and after serialisation. Also, this is
|
2261
|
+
// the fastest approach to reducing a number's precision in
|
2262
|
+
// JavaScript.
|
2263
|
+
|
2264
|
+
fieldVector.insert(termIndex, scoreWithPrecision)
|
2265
|
+
}
|
2266
|
+
|
2267
|
+
fieldVectors[fieldRef] = fieldVector
|
2268
|
+
}
|
2269
|
+
|
2270
|
+
this.fieldVectors = fieldVectors
|
2271
|
+
}
|
2272
|
+
|
2273
|
+
/**
|
2274
|
+
* Creates a token set of all tokens in the index using lunr.TokenSet
|
2275
|
+
*
|
2276
|
+
* @private
|
2277
|
+
*/
|
2278
|
+
lunr.Builder.prototype.createTokenSet = function () {
|
2279
|
+
this.tokenSet = lunr.TokenSet.fromArray(
|
2280
|
+
Object.keys(this.invertedIndex).sort()
|
2281
|
+
)
|
2282
|
+
}
|
2283
|
+
|
2284
|
+
/**
|
2285
|
+
* Builds the index, creating an instance of lunr.Index.
|
2286
|
+
*
|
2287
|
+
* This completes the indexing process and should only be called
|
2288
|
+
* once all documents have been added to the index.
|
2289
|
+
*
|
2290
|
+
* @returns {lunr.Index}
|
2291
|
+
*/
|
2292
|
+
lunr.Builder.prototype.build = function () {
|
2293
|
+
this.calculateAverageFieldLengths()
|
2294
|
+
this.createFieldVectors()
|
2295
|
+
this.createTokenSet()
|
2296
|
+
|
2297
|
+
return new lunr.Index({
|
2298
|
+
invertedIndex: this.invertedIndex,
|
2299
|
+
fieldVectors: this.fieldVectors,
|
2300
|
+
tokenSet: this.tokenSet,
|
2301
|
+
fields: this._fields,
|
2302
|
+
pipeline: this.searchPipeline
|
2303
|
+
})
|
2304
|
+
}
|
2305
|
+
|
2306
|
+
/**
|
2307
|
+
* Applies a plugin to the index builder.
|
2308
|
+
*
|
2309
|
+
* A plugin is a function that is called with the index builder as its context.
|
2310
|
+
* Plugins can be used to customise or extend the behaviour of the index
|
2311
|
+
* in some way. A plugin is just a function, that encapsulated the custom
|
2312
|
+
* behaviour that should be applied when building the index.
|
2313
|
+
*
|
2314
|
+
* The plugin function will be called with the index builder as its argument, additional
|
2315
|
+
* arguments can also be passed when calling use. The function will be called
|
2316
|
+
* with the index builder as its context.
|
2317
|
+
*
|
2318
|
+
* @param {Function} plugin The plugin to apply.
|
2319
|
+
*/
|
2320
|
+
lunr.Builder.prototype.use = function (fn) {
|
2321
|
+
var args = Array.prototype.slice.call(arguments, 1)
|
2322
|
+
args.unshift(this)
|
2323
|
+
fn.apply(this, args)
|
2324
|
+
}
|
2325
|
+
/**
|
2326
|
+
* Contains and collects metadata about a matching document.
|
2327
|
+
* A single instance of lunr.MatchData is returned as part of every
|
2328
|
+
* lunr.Index~Result.
|
2329
|
+
*
|
2330
|
+
* @constructor
|
2331
|
+
* @param {string} term - The term this match data is associated with
|
2332
|
+
* @param {string} field - The field in which the term was found
|
2333
|
+
* @param {object} metadata - The metadata recorded about this term in this field
|
2334
|
+
* @property {object} metadata - A cloned collection of metadata associated with this document.
|
2335
|
+
* @see {@link lunr.Index~Result}
|
2336
|
+
*/
|
2337
|
+
lunr.MatchData = function (term, field, metadata) {
|
2338
|
+
var clonedMetadata = Object.create(null),
|
2339
|
+
metadataKeys = Object.keys(metadata)
|
2340
|
+
|
2341
|
+
// Cloning the metadata to prevent the original
|
2342
|
+
// being mutated during match data combination.
|
2343
|
+
// Metadata is kept in an array within the inverted
|
2344
|
+
// index so cloning the data can be done with
|
2345
|
+
// Array#slice
|
2346
|
+
for (var i = 0; i < metadataKeys.length; i++) {
|
2347
|
+
var key = metadataKeys[i]
|
2348
|
+
clonedMetadata[key] = metadata[key].slice()
|
2349
|
+
}
|
2350
|
+
|
2351
|
+
this.metadata = Object.create(null)
|
2352
|
+
this.metadata[term] = Object.create(null)
|
2353
|
+
this.metadata[term][field] = clonedMetadata
|
2354
|
+
}
|
2355
|
+
|
2356
|
+
/**
|
2357
|
+
* An instance of lunr.MatchData will be created for every term that matches a
|
2358
|
+
* document. However only one instance is required in a lunr.Index~Result. This
|
2359
|
+
* method combines metadata from another instance of lunr.MatchData with this
|
2360
|
+
* objects metadata.
|
2361
|
+
*
|
2362
|
+
* @param {lunr.MatchData} otherMatchData - Another instance of match data to merge with this one.
|
2363
|
+
* @see {@link lunr.Index~Result}
|
2364
|
+
*/
|
2365
|
+
lunr.MatchData.prototype.combine = function (otherMatchData) {
|
2366
|
+
var terms = Object.keys(otherMatchData.metadata)
|
2367
|
+
|
2368
|
+
for (var i = 0; i < terms.length; i++) {
|
2369
|
+
var term = terms[i],
|
2370
|
+
fields = Object.keys(otherMatchData.metadata[term])
|
2371
|
+
|
2372
|
+
if (this.metadata[term] == undefined) {
|
2373
|
+
this.metadata[term] = Object.create(null)
|
2374
|
+
}
|
2375
|
+
|
2376
|
+
for (var j = 0; j < fields.length; j++) {
|
2377
|
+
var field = fields[j],
|
2378
|
+
keys = Object.keys(otherMatchData.metadata[term][field])
|
2379
|
+
|
2380
|
+
if (this.metadata[term][field] == undefined) {
|
2381
|
+
this.metadata[term][field] = Object.create(null)
|
2382
|
+
}
|
2383
|
+
|
2384
|
+
for (var k = 0; k < keys.length; k++) {
|
2385
|
+
var key = keys[k]
|
2386
|
+
|
2387
|
+
if (this.metadata[term][field][key] == undefined) {
|
2388
|
+
this.metadata[term][field][key] = otherMatchData.metadata[term][field][key]
|
2389
|
+
} else {
|
2390
|
+
this.metadata[term][field][key] = this.metadata[term][field][key].concat(otherMatchData.metadata[term][field][key])
|
2391
|
+
}
|
2392
|
+
|
2393
|
+
}
|
2394
|
+
}
|
2395
|
+
}
|
2396
|
+
}
|
2397
|
+
|
2398
|
+
/**
|
2399
|
+
* Add metadata for a term/field pair to this instance of match data.
|
2400
|
+
*
|
2401
|
+
* @param {string} term - The term this match data is associated with
|
2402
|
+
* @param {string} field - The field in which the term was found
|
2403
|
+
* @param {object} metadata - The metadata recorded about this term in this field
|
2404
|
+
*/
|
2405
|
+
lunr.MatchData.prototype.add = function (term, field, metadata) {
|
2406
|
+
if (!(term in this.metadata)) {
|
2407
|
+
this.metadata[term] = Object.create(null)
|
2408
|
+
this.metadata[term][field] = metadata
|
2409
|
+
return
|
2410
|
+
}
|
2411
|
+
|
2412
|
+
if (!(field in this.metadata[term])) {
|
2413
|
+
this.metadata[term][field] = metadata
|
2414
|
+
return
|
2415
|
+
}
|
2416
|
+
|
2417
|
+
var metadataKeys = Object.keys(metadata)
|
2418
|
+
|
2419
|
+
for (var i = 0; i < metadataKeys.length; i++) {
|
2420
|
+
var key = metadataKeys[i]
|
2421
|
+
|
2422
|
+
if (key in this.metadata[term][field]) {
|
2423
|
+
this.metadata[term][field][key] = this.metadata[term][field][key].concat(metadata[key])
|
2424
|
+
} else {
|
2425
|
+
this.metadata[term][field][key] = metadata[key]
|
2426
|
+
}
|
2427
|
+
}
|
2428
|
+
}
|
2429
|
+
/**
|
2430
|
+
* A lunr.Query provides a programmatic way of defining queries to be performed
|
2431
|
+
* against a {@link lunr.Index}.
|
2432
|
+
*
|
2433
|
+
* Prefer constructing a lunr.Query using the {@link lunr.Index#query} method
|
2434
|
+
* so the query object is pre-initialized with the right index fields.
|
2435
|
+
*
|
2436
|
+
* @constructor
|
2437
|
+
* @property {lunr.Query~Clause[]} clauses - An array of query clauses.
|
2438
|
+
* @property {string[]} allFields - An array of all available fields in a lunr.Index.
|
2439
|
+
*/
|
2440
|
+
lunr.Query = function (allFields) {
|
2441
|
+
this.clauses = []
|
2442
|
+
this.allFields = allFields
|
2443
|
+
}
|
2444
|
+
|
2445
|
+
/**
|
2446
|
+
* Constants for indicating what kind of automatic wildcard insertion will be used when constructing a query clause.
|
2447
|
+
*
|
2448
|
+
* This allows wildcards to be added to the beginning and end of a term without having to manually do any string
|
2449
|
+
* concatenation.
|
2450
|
+
*
|
2451
|
+
* The wildcard constants can be bitwise combined to select both leading and trailing wildcards.
|
2452
|
+
*
|
2453
|
+
* @constant
|
2454
|
+
* @default
|
2455
|
+
* @property {number} wildcard.NONE - The term will have no wildcards inserted, this is the default behaviour
|
2456
|
+
* @property {number} wildcard.LEADING - Prepend the term with a wildcard, unless a leading wildcard already exists
|
2457
|
+
* @property {number} wildcard.TRAILING - Append a wildcard to the term, unless a trailing wildcard already exists
|
2458
|
+
* @see lunr.Query~Clause
|
2459
|
+
* @see lunr.Query#clause
|
2460
|
+
* @see lunr.Query#term
|
2461
|
+
* @example <caption>query term with trailing wildcard</caption>
|
2462
|
+
* query.term('foo', { wildcard: lunr.Query.wildcard.TRAILING })
|
2463
|
+
* @example <caption>query term with leading and trailing wildcard</caption>
|
2464
|
+
* query.term('foo', {
|
2465
|
+
* wildcard: lunr.Query.wildcard.LEADING | lunr.Query.wildcard.TRAILING
|
2466
|
+
* })
|
2467
|
+
*/
|
2468
|
+
lunr.Query.wildcard = new String ("*")
|
2469
|
+
lunr.Query.wildcard.NONE = 0
|
2470
|
+
lunr.Query.wildcard.LEADING = 1
|
2471
|
+
lunr.Query.wildcard.TRAILING = 2
|
2472
|
+
|
2473
|
+
/**
|
2474
|
+
* A single clause in a {@link lunr.Query} contains a term and details on how to
|
2475
|
+
* match that term against a {@link lunr.Index}.
|
2476
|
+
*
|
2477
|
+
* @typedef {Object} lunr.Query~Clause
|
2478
|
+
* @property {string[]} fields - The fields in an index this clause should be matched against.
|
2479
|
+
* @property {number} [boost=1] - Any boost that should be applied when matching this clause.
|
2480
|
+
* @property {number} [editDistance] - Whether the term should have fuzzy matching applied, and how fuzzy the match should be.
|
2481
|
+
* @property {boolean} [usePipeline] - Whether the term should be passed through the search pipeline.
|
2482
|
+
* @property {number} [wildcard=0] - Whether the term should have wildcards appended or prepended.
|
2483
|
+
*/
|
2484
|
+
|
2485
|
+
/**
|
2486
|
+
* Adds a {@link lunr.Query~Clause} to this query.
|
2487
|
+
*
|
2488
|
+
* Unless the clause contains the fields to be matched all fields will be matched. In addition
|
2489
|
+
* a default boost of 1 is applied to the clause.
|
2490
|
+
*
|
2491
|
+
* @param {lunr.Query~Clause} clause - The clause to add to this query.
|
2492
|
+
* @see lunr.Query~Clause
|
2493
|
+
* @returns {lunr.Query}
|
2494
|
+
*/
|
2495
|
+
lunr.Query.prototype.clause = function (clause) {
|
2496
|
+
if (!('fields' in clause)) {
|
2497
|
+
clause.fields = this.allFields
|
2498
|
+
}
|
2499
|
+
|
2500
|
+
if (!('boost' in clause)) {
|
2501
|
+
clause.boost = 1
|
2502
|
+
}
|
2503
|
+
|
2504
|
+
if (!('usePipeline' in clause)) {
|
2505
|
+
clause.usePipeline = true
|
2506
|
+
}
|
2507
|
+
|
2508
|
+
if (!('wildcard' in clause)) {
|
2509
|
+
clause.wildcard = lunr.Query.wildcard.NONE
|
2510
|
+
}
|
2511
|
+
|
2512
|
+
if ((clause.wildcard & lunr.Query.wildcard.LEADING) && (clause.term.charAt(0) != lunr.Query.wildcard)) {
|
2513
|
+
clause.term = "*" + clause.term
|
2514
|
+
}
|
2515
|
+
|
2516
|
+
if ((clause.wildcard & lunr.Query.wildcard.TRAILING) && (clause.term.slice(-1) != lunr.Query.wildcard)) {
|
2517
|
+
clause.term = "" + clause.term + "*"
|
2518
|
+
}
|
2519
|
+
|
2520
|
+
this.clauses.push(clause)
|
2521
|
+
|
2522
|
+
return this
|
2523
|
+
}
|
2524
|
+
|
2525
|
+
/**
|
2526
|
+
* Adds a term to the current query, under the covers this will create a {@link lunr.Query~Clause}
|
2527
|
+
* to the list of clauses that make up this query.
|
2528
|
+
*
|
2529
|
+
* @param {string} term - The term to add to the query.
|
2530
|
+
* @param {Object} [options] - Any additional properties to add to the query clause.
|
2531
|
+
* @returns {lunr.Query}
|
2532
|
+
* @see lunr.Query#clause
|
2533
|
+
* @see lunr.Query~Clause
|
2534
|
+
* @example <caption>adding a single term to a query</caption>
|
2535
|
+
* query.term("foo")
|
2536
|
+
* @example <caption>adding a single term to a query and specifying search fields, term boost and automatic trailing wildcard</caption>
|
2537
|
+
* query.term("foo", {
|
2538
|
+
* fields: ["title"],
|
2539
|
+
* boost: 10,
|
2540
|
+
* wildcard: lunr.Query.wildcard.TRAILING
|
2541
|
+
* })
|
2542
|
+
*/
|
2543
|
+
lunr.Query.prototype.term = function (term, options) {
|
2544
|
+
var clause = options || {}
|
2545
|
+
clause.term = term
|
2546
|
+
|
2547
|
+
this.clause(clause)
|
2548
|
+
|
2549
|
+
return this
|
2550
|
+
}
|
2551
|
+
lunr.QueryParseError = function (message, start, end) {
|
2552
|
+
this.name = "QueryParseError"
|
2553
|
+
this.message = message
|
2554
|
+
this.start = start
|
2555
|
+
this.end = end
|
2556
|
+
}
|
2557
|
+
|
2558
|
+
lunr.QueryParseError.prototype = new Error
|
2559
|
+
lunr.QueryLexer = function (str) {
|
2560
|
+
this.lexemes = []
|
2561
|
+
this.str = str
|
2562
|
+
this.length = str.length
|
2563
|
+
this.pos = 0
|
2564
|
+
this.start = 0
|
2565
|
+
this.escapeCharPositions = []
|
2566
|
+
}
|
2567
|
+
|
2568
|
+
lunr.QueryLexer.prototype.run = function () {
|
2569
|
+
var state = lunr.QueryLexer.lexText
|
2570
|
+
|
2571
|
+
while (state) {
|
2572
|
+
state = state(this)
|
2573
|
+
}
|
2574
|
+
}
|
2575
|
+
|
2576
|
+
lunr.QueryLexer.prototype.sliceString = function () {
|
2577
|
+
var subSlices = [],
|
2578
|
+
sliceStart = this.start,
|
2579
|
+
sliceEnd = this.pos
|
2580
|
+
|
2581
|
+
for (var i = 0; i < this.escapeCharPositions.length; i++) {
|
2582
|
+
sliceEnd = this.escapeCharPositions[i]
|
2583
|
+
subSlices.push(this.str.slice(sliceStart, sliceEnd))
|
2584
|
+
sliceStart = sliceEnd + 1
|
2585
|
+
}
|
2586
|
+
|
2587
|
+
subSlices.push(this.str.slice(sliceStart, this.pos))
|
2588
|
+
this.escapeCharPositions.length = 0
|
2589
|
+
|
2590
|
+
return subSlices.join('')
|
2591
|
+
}
|
2592
|
+
|
2593
|
+
lunr.QueryLexer.prototype.emit = function (type) {
|
2594
|
+
this.lexemes.push({
|
2595
|
+
type: type,
|
2596
|
+
str: this.sliceString(),
|
2597
|
+
start: this.start,
|
2598
|
+
end: this.pos
|
2599
|
+
})
|
2600
|
+
|
2601
|
+
this.start = this.pos
|
2602
|
+
}
|
2603
|
+
|
2604
|
+
lunr.QueryLexer.prototype.escapeCharacter = function () {
|
2605
|
+
this.escapeCharPositions.push(this.pos - 1)
|
2606
|
+
this.pos += 1
|
2607
|
+
}
|
2608
|
+
|
2609
|
+
lunr.QueryLexer.prototype.next = function () {
|
2610
|
+
if (this.pos >= this.length) {
|
2611
|
+
return lunr.QueryLexer.EOS
|
2612
|
+
}
|
2613
|
+
|
2614
|
+
var char = this.str.charAt(this.pos)
|
2615
|
+
this.pos += 1
|
2616
|
+
return char
|
2617
|
+
}
|
2618
|
+
|
2619
|
+
lunr.QueryLexer.prototype.width = function () {
|
2620
|
+
return this.pos - this.start
|
2621
|
+
}
|
2622
|
+
|
2623
|
+
lunr.QueryLexer.prototype.ignore = function () {
|
2624
|
+
if (this.start == this.pos) {
|
2625
|
+
this.pos += 1
|
2626
|
+
}
|
2627
|
+
|
2628
|
+
this.start = this.pos
|
2629
|
+
}
|
2630
|
+
|
2631
|
+
lunr.QueryLexer.prototype.backup = function () {
|
2632
|
+
this.pos -= 1
|
2633
|
+
}
|
2634
|
+
|
2635
|
+
lunr.QueryLexer.prototype.acceptDigitRun = function () {
|
2636
|
+
var char, charCode
|
2637
|
+
|
2638
|
+
do {
|
2639
|
+
char = this.next()
|
2640
|
+
charCode = char.charCodeAt(0)
|
2641
|
+
} while (charCode > 47 && charCode < 58)
|
2642
|
+
|
2643
|
+
if (char != lunr.QueryLexer.EOS) {
|
2644
|
+
this.backup()
|
2645
|
+
}
|
2646
|
+
}
|
2647
|
+
|
2648
|
+
lunr.QueryLexer.prototype.more = function () {
|
2649
|
+
return this.pos < this.length
|
2650
|
+
}
|
2651
|
+
|
2652
|
+
lunr.QueryLexer.EOS = 'EOS'
|
2653
|
+
lunr.QueryLexer.FIELD = 'FIELD'
|
2654
|
+
lunr.QueryLexer.TERM = 'TERM'
|
2655
|
+
lunr.QueryLexer.EDIT_DISTANCE = 'EDIT_DISTANCE'
|
2656
|
+
lunr.QueryLexer.BOOST = 'BOOST'
|
2657
|
+
|
2658
|
+
lunr.QueryLexer.lexField = function (lexer) {
|
2659
|
+
lexer.backup()
|
2660
|
+
lexer.emit(lunr.QueryLexer.FIELD)
|
2661
|
+
lexer.ignore()
|
2662
|
+
return lunr.QueryLexer.lexText
|
2663
|
+
}
|
2664
|
+
|
2665
|
+
lunr.QueryLexer.lexTerm = function (lexer) {
|
2666
|
+
if (lexer.width() > 1) {
|
2667
|
+
lexer.backup()
|
2668
|
+
lexer.emit(lunr.QueryLexer.TERM)
|
2669
|
+
}
|
2670
|
+
|
2671
|
+
lexer.ignore()
|
2672
|
+
|
2673
|
+
if (lexer.more()) {
|
2674
|
+
return lunr.QueryLexer.lexText
|
2675
|
+
}
|
2676
|
+
}
|
2677
|
+
|
2678
|
+
lunr.QueryLexer.lexEditDistance = function (lexer) {
|
2679
|
+
lexer.ignore()
|
2680
|
+
lexer.acceptDigitRun()
|
2681
|
+
lexer.emit(lunr.QueryLexer.EDIT_DISTANCE)
|
2682
|
+
return lunr.QueryLexer.lexText
|
2683
|
+
}
|
2684
|
+
|
2685
|
+
lunr.QueryLexer.lexBoost = function (lexer) {
|
2686
|
+
lexer.ignore()
|
2687
|
+
lexer.acceptDigitRun()
|
2688
|
+
lexer.emit(lunr.QueryLexer.BOOST)
|
2689
|
+
return lunr.QueryLexer.lexText
|
2690
|
+
}
|
2691
|
+
|
2692
|
+
lunr.QueryLexer.lexEOS = function (lexer) {
|
2693
|
+
if (lexer.width() > 0) {
|
2694
|
+
lexer.emit(lunr.QueryLexer.TERM)
|
2695
|
+
}
|
2696
|
+
}
|
2697
|
+
|
2698
|
+
// This matches the separator used when tokenising fields
|
2699
|
+
// within a document. These should match otherwise it is
|
2700
|
+
// not possible to search for some tokens within a document.
|
2701
|
+
//
|
2702
|
+
// It is possible for the user to change the separator on the
|
2703
|
+
// tokenizer so it _might_ clash with any other of the special
|
2704
|
+
// characters already used within the search string, e.g. :.
|
2705
|
+
//
|
2706
|
+
// This means that it is possible to change the separator in
|
2707
|
+
// such a way that makes some words unsearchable using a search
|
2708
|
+
// string.
|
2709
|
+
lunr.QueryLexer.termSeparator = lunr.tokenizer.separator
|
2710
|
+
|
2711
|
+
lunr.QueryLexer.lexText = function (lexer) {
|
2712
|
+
while (true) {
|
2713
|
+
var char = lexer.next()
|
2714
|
+
|
2715
|
+
if (char == lunr.QueryLexer.EOS) {
|
2716
|
+
return lunr.QueryLexer.lexEOS
|
2717
|
+
}
|
2718
|
+
|
2719
|
+
// Escape character is '\'
|
2720
|
+
if (char.charCodeAt(0) == 92) {
|
2721
|
+
lexer.escapeCharacter()
|
2722
|
+
continue
|
2723
|
+
}
|
2724
|
+
|
2725
|
+
if (char == ":") {
|
2726
|
+
return lunr.QueryLexer.lexField
|
2727
|
+
}
|
2728
|
+
|
2729
|
+
if (char == "~") {
|
2730
|
+
lexer.backup()
|
2731
|
+
if (lexer.width() > 0) {
|
2732
|
+
lexer.emit(lunr.QueryLexer.TERM)
|
2733
|
+
}
|
2734
|
+
return lunr.QueryLexer.lexEditDistance
|
2735
|
+
}
|
2736
|
+
|
2737
|
+
if (char == "^") {
|
2738
|
+
lexer.backup()
|
2739
|
+
if (lexer.width() > 0) {
|
2740
|
+
lexer.emit(lunr.QueryLexer.TERM)
|
2741
|
+
}
|
2742
|
+
return lunr.QueryLexer.lexBoost
|
2743
|
+
}
|
2744
|
+
|
2745
|
+
if (char.match(lunr.QueryLexer.termSeparator)) {
|
2746
|
+
return lunr.QueryLexer.lexTerm
|
2747
|
+
}
|
2748
|
+
}
|
2749
|
+
}
|
2750
|
+
|
2751
|
+
lunr.QueryParser = function (str, query) {
|
2752
|
+
this.lexer = new lunr.QueryLexer (str)
|
2753
|
+
this.query = query
|
2754
|
+
this.currentClause = {}
|
2755
|
+
this.lexemeIdx = 0
|
2756
|
+
}
|
2757
|
+
|
2758
|
+
lunr.QueryParser.prototype.parse = function () {
|
2759
|
+
this.lexer.run()
|
2760
|
+
this.lexemes = this.lexer.lexemes
|
2761
|
+
|
2762
|
+
var state = lunr.QueryParser.parseFieldOrTerm
|
2763
|
+
|
2764
|
+
while (state) {
|
2765
|
+
state = state(this)
|
2766
|
+
}
|
2767
|
+
|
2768
|
+
return this.query
|
2769
|
+
}
|
2770
|
+
|
2771
|
+
lunr.QueryParser.prototype.peekLexeme = function () {
|
2772
|
+
return this.lexemes[this.lexemeIdx]
|
2773
|
+
}
|
2774
|
+
|
2775
|
+
lunr.QueryParser.prototype.consumeLexeme = function () {
|
2776
|
+
var lexeme = this.peekLexeme()
|
2777
|
+
this.lexemeIdx += 1
|
2778
|
+
return lexeme
|
2779
|
+
}
|
2780
|
+
|
2781
|
+
lunr.QueryParser.prototype.nextClause = function () {
|
2782
|
+
var completedClause = this.currentClause
|
2783
|
+
this.query.clause(completedClause)
|
2784
|
+
this.currentClause = {}
|
2785
|
+
}
|
2786
|
+
|
2787
|
+
lunr.QueryParser.parseFieldOrTerm = function (parser) {
|
2788
|
+
var lexeme = parser.peekLexeme()
|
2789
|
+
|
2790
|
+
if (lexeme == undefined) {
|
2791
|
+
return
|
2792
|
+
}
|
2793
|
+
|
2794
|
+
switch (lexeme.type) {
|
2795
|
+
case lunr.QueryLexer.FIELD:
|
2796
|
+
return lunr.QueryParser.parseField
|
2797
|
+
case lunr.QueryLexer.TERM:
|
2798
|
+
return lunr.QueryParser.parseTerm
|
2799
|
+
default:
|
2800
|
+
var errorMessage = "expected either a field or a term, found " + lexeme.type
|
2801
|
+
|
2802
|
+
if (lexeme.str.length >= 1) {
|
2803
|
+
errorMessage += " with value '" + lexeme.str + "'"
|
2804
|
+
}
|
2805
|
+
|
2806
|
+
throw new lunr.QueryParseError (errorMessage, lexeme.start, lexeme.end)
|
2807
|
+
}
|
2808
|
+
}
|
2809
|
+
|
2810
|
+
lunr.QueryParser.parseField = function (parser) {
|
2811
|
+
var lexeme = parser.consumeLexeme()
|
2812
|
+
|
2813
|
+
if (lexeme == undefined) {
|
2814
|
+
return
|
2815
|
+
}
|
2816
|
+
|
2817
|
+
if (parser.query.allFields.indexOf(lexeme.str) == -1) {
|
2818
|
+
var possibleFields = parser.query.allFields.map(function (f) { return "'" + f + "'" }).join(', '),
|
2819
|
+
errorMessage = "unrecognised field '" + lexeme.str + "', possible fields: " + possibleFields
|
2820
|
+
|
2821
|
+
throw new lunr.QueryParseError (errorMessage, lexeme.start, lexeme.end)
|
2822
|
+
}
|
2823
|
+
|
2824
|
+
parser.currentClause.fields = [lexeme.str]
|
2825
|
+
|
2826
|
+
var nextLexeme = parser.peekLexeme()
|
2827
|
+
|
2828
|
+
if (nextLexeme == undefined) {
|
2829
|
+
var errorMessage = "expecting term, found nothing"
|
2830
|
+
throw new lunr.QueryParseError (errorMessage, lexeme.start, lexeme.end)
|
2831
|
+
}
|
2832
|
+
|
2833
|
+
switch (nextLexeme.type) {
|
2834
|
+
case lunr.QueryLexer.TERM:
|
2835
|
+
return lunr.QueryParser.parseTerm
|
2836
|
+
default:
|
2837
|
+
var errorMessage = "expecting term, found '" + nextLexeme.type + "'"
|
2838
|
+
throw new lunr.QueryParseError (errorMessage, nextLexeme.start, nextLexeme.end)
|
2839
|
+
}
|
2840
|
+
}
|
2841
|
+
|
2842
|
+
lunr.QueryParser.parseTerm = function (parser) {
|
2843
|
+
var lexeme = parser.consumeLexeme()
|
2844
|
+
|
2845
|
+
if (lexeme == undefined) {
|
2846
|
+
return
|
2847
|
+
}
|
2848
|
+
|
2849
|
+
parser.currentClause.term = lexeme.str.toLowerCase()
|
2850
|
+
|
2851
|
+
if (lexeme.str.indexOf("*") != -1) {
|
2852
|
+
parser.currentClause.usePipeline = false
|
2853
|
+
}
|
2854
|
+
|
2855
|
+
var nextLexeme = parser.peekLexeme()
|
2856
|
+
|
2857
|
+
if (nextLexeme == undefined) {
|
2858
|
+
parser.nextClause()
|
2859
|
+
return
|
2860
|
+
}
|
2861
|
+
|
2862
|
+
switch (nextLexeme.type) {
|
2863
|
+
case lunr.QueryLexer.TERM:
|
2864
|
+
parser.nextClause()
|
2865
|
+
return lunr.QueryParser.parseTerm
|
2866
|
+
case lunr.QueryLexer.FIELD:
|
2867
|
+
parser.nextClause()
|
2868
|
+
return lunr.QueryParser.parseField
|
2869
|
+
case lunr.QueryLexer.EDIT_DISTANCE:
|
2870
|
+
return lunr.QueryParser.parseEditDistance
|
2871
|
+
case lunr.QueryLexer.BOOST:
|
2872
|
+
return lunr.QueryParser.parseBoost
|
2873
|
+
default:
|
2874
|
+
var errorMessage = "Unexpected lexeme type '" + nextLexeme.type + "'"
|
2875
|
+
throw new lunr.QueryParseError (errorMessage, nextLexeme.start, nextLexeme.end)
|
2876
|
+
}
|
2877
|
+
}
|
2878
|
+
|
2879
|
+
lunr.QueryParser.parseEditDistance = function (parser) {
|
2880
|
+
var lexeme = parser.consumeLexeme()
|
2881
|
+
|
2882
|
+
if (lexeme == undefined) {
|
2883
|
+
return
|
2884
|
+
}
|
2885
|
+
|
2886
|
+
var editDistance = parseInt(lexeme.str, 10)
|
2887
|
+
|
2888
|
+
if (isNaN(editDistance)) {
|
2889
|
+
var errorMessage = "edit distance must be numeric"
|
2890
|
+
throw new lunr.QueryParseError (errorMessage, lexeme.start, lexeme.end)
|
2891
|
+
}
|
2892
|
+
|
2893
|
+
parser.currentClause.editDistance = editDistance
|
2894
|
+
|
2895
|
+
var nextLexeme = parser.peekLexeme()
|
2896
|
+
|
2897
|
+
if (nextLexeme == undefined) {
|
2898
|
+
parser.nextClause()
|
2899
|
+
return
|
2900
|
+
}
|
2901
|
+
|
2902
|
+
switch (nextLexeme.type) {
|
2903
|
+
case lunr.QueryLexer.TERM:
|
2904
|
+
parser.nextClause()
|
2905
|
+
return lunr.QueryParser.parseTerm
|
2906
|
+
case lunr.QueryLexer.FIELD:
|
2907
|
+
parser.nextClause()
|
2908
|
+
return lunr.QueryParser.parseField
|
2909
|
+
case lunr.QueryLexer.EDIT_DISTANCE:
|
2910
|
+
return lunr.QueryParser.parseEditDistance
|
2911
|
+
case lunr.QueryLexer.BOOST:
|
2912
|
+
return lunr.QueryParser.parseBoost
|
2913
|
+
default:
|
2914
|
+
var errorMessage = "Unexpected lexeme type '" + nextLexeme.type + "'"
|
2915
|
+
throw new lunr.QueryParseError (errorMessage, nextLexeme.start, nextLexeme.end)
|
2916
|
+
}
|
2917
|
+
}
|
2918
|
+
|
2919
|
+
lunr.QueryParser.parseBoost = function (parser) {
|
2920
|
+
var lexeme = parser.consumeLexeme()
|
2921
|
+
|
2922
|
+
if (lexeme == undefined) {
|
2923
|
+
return
|
2924
|
+
}
|
2925
|
+
|
2926
|
+
var boost = parseInt(lexeme.str, 10)
|
2927
|
+
|
2928
|
+
if (isNaN(boost)) {
|
2929
|
+
var errorMessage = "boost must be numeric"
|
2930
|
+
throw new lunr.QueryParseError (errorMessage, lexeme.start, lexeme.end)
|
2931
|
+
}
|
2932
|
+
|
2933
|
+
parser.currentClause.boost = boost
|
2934
|
+
|
2935
|
+
var nextLexeme = parser.peekLexeme()
|
2936
|
+
|
2937
|
+
if (nextLexeme == undefined) {
|
2938
|
+
parser.nextClause()
|
2939
|
+
return
|
2940
|
+
}
|
2941
|
+
|
2942
|
+
switch (nextLexeme.type) {
|
2943
|
+
case lunr.QueryLexer.TERM:
|
2944
|
+
parser.nextClause()
|
2945
|
+
return lunr.QueryParser.parseTerm
|
2946
|
+
case lunr.QueryLexer.FIELD:
|
2947
|
+
parser.nextClause()
|
2948
|
+
return lunr.QueryParser.parseField
|
2949
|
+
case lunr.QueryLexer.EDIT_DISTANCE:
|
2950
|
+
return lunr.QueryParser.parseEditDistance
|
2951
|
+
case lunr.QueryLexer.BOOST:
|
2952
|
+
return lunr.QueryParser.parseBoost
|
2953
|
+
default:
|
2954
|
+
var errorMessage = "Unexpected lexeme type '" + nextLexeme.type + "'"
|
2955
|
+
throw new lunr.QueryParseError (errorMessage, nextLexeme.start, nextLexeme.end)
|
2956
|
+
}
|
2957
|
+
}
|
2958
|
+
|
2959
|
+
/**
|
2960
|
+
* export the module via AMD, CommonJS or as a browser global
|
2961
|
+
* Export code from https://github.com/umdjs/umd/blob/master/returnExports.js
|
2962
|
+
*/
|
2963
|
+
;(function (root, factory) {
|
2964
|
+
if (typeof define === 'function' && define.amd) {
|
2965
|
+
// AMD. Register as an anonymous module.
|
2966
|
+
define(factory)
|
2967
|
+
} else if (typeof exports === 'object') {
|
2968
|
+
/**
|
2969
|
+
* Node. Does not work with strict CommonJS, but
|
2970
|
+
* only CommonJS-like enviroments that support module.exports,
|
2971
|
+
* like Node.
|
2972
|
+
*/
|
2973
|
+
module.exports = factory()
|
2974
|
+
} else {
|
2975
|
+
// Browser globals (root is window)
|
2976
|
+
root.lunr = factory()
|
2977
|
+
}
|
2978
|
+
}(this, function () {
|
2979
|
+
/**
|
2980
|
+
* Just return a value to define the module export.
|
2981
|
+
* This example returns an object, but the module
|
2982
|
+
* can return a function as the exported value.
|
2983
|
+
*/
|
2984
|
+
return lunr
|
2985
|
+
}))
|
2986
|
+
})();
|