lunr.js 97 KB

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