search_test.js 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. suite('search', function () {
  2. setup(function () {
  3. this.documents = [{
  4. id: 'a',
  5. title: 'Mr. Green kills Colonel Mustard',
  6. body: 'Mr. Green killed Colonel Mustard in the study with the candlestick. Mr. Green is not a very nice fellow.',
  7. wordCount: 19
  8. },{
  9. id: 'b',
  10. title: 'Plumb waters plant',
  11. body: 'Professor Plumb has a green plant in his study',
  12. wordCount: 9
  13. },{
  14. id: 'c',
  15. title: 'Scarlett helps Professor',
  16. body: 'Miss Scarlett watered Professor Plumbs green plant while he was away from his office last week.',
  17. wordCount: 16
  18. }]
  19. })
  20. suite('with build-time field boosts', function () {
  21. setup(function () {
  22. var self = this
  23. this.idx = lunr(function () {
  24. this.ref('id')
  25. this.field('title')
  26. this.field('body', { boost: 10 })
  27. self.documents.forEach(function (document) {
  28. this.add(document)
  29. }, this)
  30. })
  31. })
  32. suite('no query boosts', function () {
  33. var assertions = function () {
  34. test('document b ranks highest', function () {
  35. assert.equal('b', this.results[0].ref)
  36. })
  37. }
  38. suite('#search', function () {
  39. setup(function () {
  40. this.results = this.idx.search('professor')
  41. })
  42. assertions()
  43. })
  44. suite('#query', function () {
  45. setup(function () {
  46. this.results = this.idx.query(function (q) {
  47. q.term('professor')
  48. })
  49. })
  50. assertions()
  51. })
  52. })
  53. })
  54. suite('with build-time document boost', function () {
  55. setup(function () {
  56. var self = this
  57. this.idx = lunr(function () {
  58. this.ref('id')
  59. this.field('title')
  60. this.field('body')
  61. self.documents.forEach(function (document) {
  62. var boost = document.id == 'c' ? 10 : 1
  63. this.add(document, { boost: boost })
  64. }, this)
  65. })
  66. })
  67. suite('no query boost', function () {
  68. var assertions = function () {
  69. test('document c ranks highest', function () {
  70. assert.equal('c', this.results[0].ref)
  71. })
  72. }
  73. suite('#search', function () {
  74. setup(function () {
  75. this.results = this.idx.search('plumb')
  76. })
  77. assertions()
  78. })
  79. suite('#query', function () {
  80. setup(function () {
  81. this.results = this.idx.query(function (q) {
  82. q.term('plumb')
  83. })
  84. })
  85. assertions()
  86. })
  87. })
  88. suite('with query boost', function () {
  89. var assertions = function () {
  90. test('document b ranks highest', function () {
  91. assert.equal('b', this.results[0].ref)
  92. })
  93. }
  94. suite('#search', function () {
  95. setup(function () {
  96. this.results = this.idx.search('green study^10')
  97. })
  98. assertions()
  99. })
  100. suite('#query', function () {
  101. setup(function () {
  102. this.results = this.idx.query(function (q) {
  103. q.term('green')
  104. q.term('study', { boost: 10 })
  105. })
  106. })
  107. assertions()
  108. })
  109. })
  110. })
  111. suite('without build-time boosts', function () {
  112. setup(function () {
  113. var self = this
  114. this.idx = lunr(function () {
  115. this.ref('id')
  116. this.field('title')
  117. this.field('body')
  118. self.documents.forEach(function (document) {
  119. this.add(document)
  120. }, this)
  121. })
  122. })
  123. suite('single term search', function () {
  124. suite('one match', function () {
  125. var assertions = function () {
  126. test('one result returned', function () {
  127. assert.lengthOf(this.results, 1)
  128. })
  129. test('document c matches', function () {
  130. assert.equal('c', this.results[0].ref)
  131. })
  132. test('matching term', function () {
  133. assert.sameMembers(['scarlett'], Object.keys(this.results[0].matchData.metadata))
  134. })
  135. }
  136. suite('#seach', function () {
  137. setup(function () {
  138. this.results = this.idx.search('scarlett')
  139. })
  140. assertions()
  141. })
  142. suite('#query', function () {
  143. setup(function () {
  144. this.results = this.idx.query(function (q) {
  145. q.term('scarlett')
  146. })
  147. })
  148. assertions()
  149. })
  150. })
  151. suite('no match', function () {
  152. setup(function () {
  153. this.results = this.idx.search('foo')
  154. })
  155. test('no matches', function () {
  156. assert.lengthOf(this.results, 0)
  157. })
  158. })
  159. suite('multiple matches', function () {
  160. setup(function () {
  161. this.results = this.idx.search('plant')
  162. })
  163. test('has two matches', function () {
  164. assert.lengthOf(this.results, 2)
  165. })
  166. test('sorted by relevance', function () {
  167. assert.equal('b', this.results[0].ref)
  168. assert.equal('c', this.results[1].ref)
  169. })
  170. })
  171. suite('pipeline processing', function () {
  172. // study would be stemmed to studi, tokens
  173. // are stemmed by default on index and must
  174. // also be stemmed on search to match
  175. suite('enabled (default)', function () {
  176. setup(function () {
  177. this.results = this.idx.query(function (q) {
  178. q.clause({term: 'study', usePipeline: true})
  179. })
  180. })
  181. test('has two matches', function () {
  182. assert.lengthOf(this.results, 2)
  183. })
  184. test('sorted by relevance', function () {
  185. assert.equal('b', this.results[0].ref)
  186. assert.equal('a', this.results[1].ref)
  187. })
  188. })
  189. suite('disabled', function () {
  190. setup(function () {
  191. this.results = this.idx.query(function (q) {
  192. q.clause({term: 'study', usePipeline: false})
  193. })
  194. })
  195. test('no matches', function () {
  196. assert.lengthOf(this.results, 0)
  197. })
  198. })
  199. })
  200. })
  201. suite('multiple terms', function () {
  202. suite('all terms match', function () {
  203. setup(function () {
  204. this.results = this.idx.search('fellow candlestick')
  205. })
  206. test('has one match', function () {
  207. assert.lengthOf(this.results, 1)
  208. })
  209. test('correct document returned', function () {
  210. assert.equal('a', this.results[0].ref)
  211. })
  212. test('matched terms returned', function () {
  213. assert.sameMembers(['fellow', 'candlestick'], Object.keys(this.results[0].matchData.metadata))
  214. assert.sameMembers(['body'], Object.keys(this.results[0].matchData.metadata['fellow']));
  215. assert.sameMembers(['body'], Object.keys(this.results[0].matchData.metadata['candlestick']));
  216. })
  217. })
  218. suite('one term matches', function () {
  219. setup(function () {
  220. this.results = this.idx.search('week foo')
  221. })
  222. test('has one match', function () {
  223. assert.lengthOf(this.results, 1)
  224. })
  225. test('correct document returned', function () {
  226. assert.equal('c', this.results[0].ref)
  227. })
  228. test('only matching terms returned', function () {
  229. assert.sameMembers(['week'], Object.keys(this.results[0].matchData.metadata))
  230. })
  231. })
  232. suite('duplicate query terms', function () {
  233. // https://github.com/olivernn/lunr.js/issues/256
  234. // previously this would throw a duplicate index error
  235. // because the query vector already contained an entry
  236. // for the term 'fellow'
  237. test('no errors', function () {
  238. var idx = this.idx
  239. assert.doesNotThrow(function () {
  240. idx.search('fellow candlestick foo bar green plant fellow')
  241. })
  242. })
  243. })
  244. suite('documents with all terms score higher', function () {
  245. setup(function () {
  246. this.results = this.idx.search('candlestick green')
  247. })
  248. test('has three matches', function () {
  249. assert.lengthOf(this.results, 3)
  250. })
  251. test('correct documents returned', function () {
  252. var matchingDocuments = this.results.map(function (r) {
  253. return r.ref
  254. })
  255. assert.sameMembers(['a', 'b', 'c'], matchingDocuments)
  256. })
  257. test('documents with all terms score highest', function () {
  258. assert.equal('a', this.results[0].ref)
  259. })
  260. test('matching terms are returned', function () {
  261. assert.sameMembers(['candlestick', 'green'], Object.keys(this.results[0].matchData.metadata))
  262. assert.sameMembers(['green'], Object.keys(this.results[1].matchData.metadata))
  263. assert.sameMembers(['green'], Object.keys(this.results[2].matchData.metadata))
  264. })
  265. })
  266. suite('no terms match', function () {
  267. setup(function () {
  268. this.results = this.idx.search('foo bar')
  269. })
  270. test('no matches', function () {
  271. assert.lengthOf(this.results, 0)
  272. })
  273. })
  274. suite('corpus terms are stemmed', function () {
  275. setup(function () {
  276. this.results = this.idx.search('water')
  277. })
  278. test('matches two documents', function () {
  279. assert.lengthOf(this.results, 2)
  280. })
  281. test('matches correct documents', function () {
  282. var matchingDocuments = this.results.map(function (r) {
  283. return r.ref
  284. })
  285. assert.sameMembers(['b', 'c'], matchingDocuments)
  286. })
  287. })
  288. suite('field scoped terms', function () {
  289. suite('only matches on scoped field', function () {
  290. setup(function () {
  291. this.results = this.idx.search('title:plant')
  292. })
  293. test('one result returned', function () {
  294. assert.lengthOf(this.results, 1)
  295. })
  296. test('returns the correct document', function () {
  297. assert.equal('b', this.results[0].ref)
  298. })
  299. test('match data', function () {
  300. assert.sameMembers(['plant'], Object.keys(this.results[0].matchData.metadata))
  301. })
  302. })
  303. suite('no matching terms', function () {
  304. setup(function () {
  305. this.results = this.idx.search('title:candlestick')
  306. })
  307. test('no results returned', function () {
  308. assert.lengthOf(this.results, 0)
  309. })
  310. })
  311. })
  312. suite('wildcard matching', function () {
  313. suite('trailing wildcard', function () {
  314. suite('no matches', function () {
  315. setup(function () {
  316. this.results = this.idx.search('fo*')
  317. })
  318. test('no results returned', function () {
  319. assert.lengthOf(this.results, 0)
  320. })
  321. })
  322. suite('one match', function () {
  323. setup(function () {
  324. this.results = this.idx.search('candle*')
  325. })
  326. test('one result returned', function () {
  327. assert.lengthOf(this.results, 1)
  328. })
  329. test('correct document matched', function () {
  330. assert.equal('a', this.results[0].ref)
  331. })
  332. test('matching terms returned', function () {
  333. assert.sameMembers(['candlestick'], Object.keys(this.results[0].matchData.metadata))
  334. })
  335. })
  336. suite('multiple terms match', function () {
  337. setup(function () {
  338. this.results = this.idx.search('pl*')
  339. })
  340. test('two results returned', function () {
  341. assert.lengthOf(this.results, 2)
  342. })
  343. test('correct documents matched', function () {
  344. var matchingDocuments = this.results.map(function (r) {
  345. return r.ref
  346. })
  347. assert.sameMembers(['b', 'c'], matchingDocuments)
  348. })
  349. test('matching terms returned', function () {
  350. assert.sameMembers(['plumb', 'plant'], Object.keys(this.results[0].matchData.metadata))
  351. assert.sameMembers(['plumb', 'plant'], Object.keys(this.results[1].matchData.metadata))
  352. })
  353. })
  354. })
  355. })
  356. })
  357. suite('wildcard matching', function () {
  358. suite('trailing wildcard', function () {
  359. suite('no matches found', function () {
  360. setup(function () {
  361. this.results = this.idx.search('fo*')
  362. })
  363. test('no results returned', function () {
  364. assert.lengthOf(this.results, 0)
  365. })
  366. })
  367. suite('results found', function () {
  368. setup(function () {
  369. this.results = this.idx.search('pl*')
  370. })
  371. test('two results returned', function () {
  372. assert.lengthOf(this.results, 2)
  373. })
  374. test('matching documents returned', function () {
  375. assert.equal('b', this.results[0].ref)
  376. assert.equal('c', this.results[1].ref)
  377. })
  378. test('matching terms returned', function () {
  379. assert.sameMembers(['plant', 'plumb'], Object.keys(this.results[0].matchData.metadata))
  380. assert.sameMembers(['plant', 'plumb'], Object.keys(this.results[1].matchData.metadata))
  381. })
  382. })
  383. })
  384. suite('leading wildcard', function () {
  385. suite('no results found', function () {
  386. setup(function () {
  387. this.results = this.idx.search('*oo')
  388. })
  389. test('no results found', function () {
  390. assert.lengthOf(this.results, 0)
  391. })
  392. })
  393. suite('results found', function () {
  394. setup(function () {
  395. this.results = this.idx.search('*ant')
  396. })
  397. test('two results found', function () {
  398. assert.lengthOf(this.results, 2)
  399. })
  400. test('matching documents returned', function () {
  401. assert.equal('b', this.results[0].ref)
  402. assert.equal('c', this.results[1].ref)
  403. })
  404. test('matching terms returned', function () {
  405. assert.sameMembers(['plant'], Object.keys(this.results[0].matchData.metadata))
  406. assert.sameMembers(['plant'], Object.keys(this.results[1].matchData.metadata))
  407. })
  408. })
  409. })
  410. suite('contained wildcard', function () {
  411. suite('no results found', function () {
  412. setup(function () {
  413. this.results = this.idx.search('f*o')
  414. })
  415. test('no results found', function () {
  416. assert.lengthOf(this.results, 0)
  417. })
  418. })
  419. suite('results found', function () {
  420. setup(function () {
  421. this.results = this.idx.search('pl*nt')
  422. })
  423. test('two results found', function () {
  424. assert.lengthOf(this.results, 2)
  425. })
  426. test('matching documents returned', function () {
  427. assert.equal('b', this.results[0].ref)
  428. assert.equal('c', this.results[1].ref)
  429. })
  430. test('matching terms returned', function () {
  431. assert.sameMembers(['plant'], Object.keys(this.results[0].matchData.metadata))
  432. assert.sameMembers(['plant'], Object.keys(this.results[1].matchData.metadata))
  433. })
  434. })
  435. })
  436. })
  437. suite('edit distance', function () {
  438. suite('no results found', function () {
  439. setup(function () {
  440. this.results = this.idx.search('foo~1')
  441. })
  442. test('no results returned', function () {
  443. assert.lengthOf(this.results, 0)
  444. })
  445. })
  446. suite('results found', function () {
  447. setup(function () {
  448. this.results = this.idx.search('plont~1')
  449. })
  450. test('two results found', function () {
  451. assert.lengthOf(this.results, 2)
  452. })
  453. test('matching documents returned', function () {
  454. assert.equal('b', this.results[0].ref)
  455. assert.equal('c', this.results[1].ref)
  456. })
  457. test('matching terms returned', function () {
  458. assert.sameMembers(['plant'], Object.keys(this.results[0].matchData.metadata))
  459. assert.sameMembers(['plant'], Object.keys(this.results[1].matchData.metadata))
  460. })
  461. })
  462. })
  463. suite('searching by field', function () {
  464. suite('unknown field', function () {
  465. test('throws lunr.QueryParseError', function () {
  466. assert.throws(function () {
  467. this.idx.search('unknown-field:plant')
  468. }.bind(this), lunr.QueryParseError)
  469. })
  470. })
  471. suite('no results found', function () {
  472. setup(function () {
  473. this.results = this.idx.search('title:candlestick')
  474. })
  475. test('no results found', function () {
  476. assert.lengthOf(this.results, 0)
  477. })
  478. })
  479. suite('results found', function () {
  480. setup(function () {
  481. this.results = this.idx.search('title:plant')
  482. })
  483. test('one results found', function () {
  484. assert.lengthOf(this.results, 1)
  485. })
  486. test('matching documents returned', function () {
  487. assert.equal('b', this.results[0].ref)
  488. })
  489. test('matching terms returned', function () {
  490. assert.sameMembers(['plant'], Object.keys(this.results[0].matchData.metadata))
  491. })
  492. })
  493. })
  494. suite('term boosts', function () {
  495. suite('no results found', function () {
  496. setup(function () {
  497. this.results = this.idx.search('foo^10')
  498. })
  499. test('no results found', function () {
  500. assert.lengthOf(this.results, 0)
  501. })
  502. })
  503. suite('results found', function () {
  504. setup(function () {
  505. this.results = this.idx.search('scarlett candlestick^5')
  506. })
  507. test('two results found', function () {
  508. assert.lengthOf(this.results, 2)
  509. })
  510. test('matching documents returned', function () {
  511. assert.equal('a', this.results[0].ref)
  512. assert.equal('c', this.results[1].ref)
  513. })
  514. test('matching terms returned', function () {
  515. assert.sameMembers(['candlestick'], Object.keys(this.results[0].matchData.metadata))
  516. assert.sameMembers(['scarlett'], Object.keys(this.results[1].matchData.metadata))
  517. })
  518. })
  519. })
  520. suite('typeahead style search', function () {
  521. suite('no results found', function () {
  522. setup(function () {
  523. this.results = this.idx.query(function (q) {
  524. q.term("xyz", { boost: 100, usePipeline: true })
  525. q.term("xyz", { boost: 10, usePipeline: false, wildcard: lunr.Query.wildcard.TRAILING })
  526. q.term("xyz", { boost: 1, editDistance: 1 })
  527. })
  528. })
  529. test('no results found', function () {
  530. assert.lengthOf(this.results, 0)
  531. })
  532. })
  533. suite('results found', function () {
  534. setup(function () {
  535. this.results = this.idx.query(function (q) {
  536. q.term("pl", { boost: 100, usePipeline: true })
  537. q.term("pl", { boost: 10, usePipeline: false, wildcard: lunr.Query.wildcard.TRAILING })
  538. q.term("pl", { boost: 1, editDistance: 1 })
  539. })
  540. })
  541. test('two results found', function () {
  542. assert.lengthOf(this.results, 2)
  543. })
  544. test('matching documents returned', function () {
  545. assert.equal('b', this.results[0].ref)
  546. assert.equal('c', this.results[1].ref)
  547. })
  548. test('matching terms returned', function () {
  549. assert.sameMembers(['plumb', 'plant'], Object.keys(this.results[0].matchData.metadata))
  550. assert.sameMembers(['plumb', 'plant'], Object.keys(this.results[1].matchData.metadata))
  551. })
  552. })
  553. })
  554. suite('term presence', function () {
  555. suite('prohibited', function () {
  556. suite('match', function () {
  557. var assertions = function () {
  558. test('two results found', function () {
  559. assert.lengthOf(this.results, 2)
  560. })
  561. test('matching documents returned', function () {
  562. assert.equal('b', this.results[0].ref)
  563. assert.equal('c', this.results[1].ref)
  564. })
  565. test('matching terms returned', function () {
  566. assert.sameMembers(['green'], Object.keys(this.results[0].matchData.metadata))
  567. assert.sameMembers(['green'], Object.keys(this.results[1].matchData.metadata))
  568. })
  569. }
  570. suite('#query', function () {
  571. setup(function () {
  572. this.results = this.idx.query(function (q) {
  573. q.term('candlestick', { presence: lunr.Query.presence.PROHIBITED })
  574. q.term('green', { presence: lunr.Query.presence.OPTIONAL })
  575. })
  576. })
  577. assertions()
  578. })
  579. suite('#search', function () {
  580. setup(function () {
  581. this.results = this.idx.search('-candlestick green')
  582. })
  583. assertions()
  584. })
  585. })
  586. suite('no match', function () {
  587. var assertions = function () {
  588. test('no matches', function () {
  589. assert.lengthOf(this.results, 0)
  590. })
  591. }
  592. suite('#query', function () {
  593. setup(function () {
  594. this.results = this.idx.query(function (q) {
  595. q.term('green', { presence: lunr.Query.presence.PROHIBITED })
  596. })
  597. })
  598. assertions()
  599. })
  600. suite('#search', function () {
  601. setup(function () {
  602. this.results = this.idx.search('-green')
  603. })
  604. assertions()
  605. })
  606. })
  607. suite('negated query no match', function () {
  608. var assertions = function () {
  609. test('all documents returned', function () {
  610. assert.lengthOf(this.results, 3)
  611. })
  612. test('all results have same score', function () {
  613. assert.isTrue(this.results.every(function (r) { return r.score === 0 }))
  614. })
  615. }
  616. suite('#query', function () {
  617. setup(function () {
  618. this.results = this.idx.query(function (q) {
  619. q.term('qwertyuiop', { presence: lunr.Query.presence.PROHIBITED })
  620. })
  621. })
  622. assertions()
  623. })
  624. suite('#search', function () {
  625. setup(function () {
  626. this.results = this.idx.search("-qwertyuiop")
  627. })
  628. assertions()
  629. })
  630. })
  631. suite('negated query some match', function () {
  632. var assertions = function () {
  633. test('all documents returned', function () {
  634. assert.lengthOf(this.results, 1)
  635. })
  636. test('all results have same score', function () {
  637. assert.isTrue(this.results.every(function (r) { return r.score === 0 }))
  638. })
  639. test('matching documents returned', function () {
  640. assert.equal('a', this.results[0].ref)
  641. })
  642. }
  643. suite('#query', function () {
  644. setup(function () {
  645. this.results = this.idx.query(function (q) {
  646. q.term('plant', { presence: lunr.Query.presence.PROHIBITED })
  647. })
  648. })
  649. assertions()
  650. })
  651. suite('#search', function () {
  652. setup(function () {
  653. this.results = this.idx.search("-plant")
  654. })
  655. assertions()
  656. })
  657. })
  658. suite('field match', function () {
  659. var assertions = function () {
  660. test('one result found', function () {
  661. assert.lengthOf(this.results, 1)
  662. })
  663. test('matching documents returned', function () {
  664. assert.equal('c', this.results[0].ref)
  665. })
  666. test('matching terms returned', function () {
  667. assert.sameMembers(['plumb'], Object.keys(this.results[0].matchData.metadata))
  668. })
  669. }
  670. suite('#query', function () {
  671. setup(function () {
  672. this.results = this.idx.query(function (q) {
  673. q.term('plant', { presence: lunr.Query.presence.PROHIBITED, fields: ['title'] })
  674. q.term('plumb', { presence: lunr.Query.presence.OPTIONAL })
  675. })
  676. })
  677. assertions()
  678. })
  679. suite('#search', function () {
  680. setup(function () {
  681. this.results = this.idx.search('-title:plant plumb')
  682. })
  683. assertions()
  684. })
  685. })
  686. })
  687. suite('required', function () {
  688. suite('match', function () {
  689. var assertions = function () {
  690. test('one result found', function () {
  691. assert.lengthOf(this.results, 1)
  692. })
  693. test('matching documents returned', function () {
  694. assert.equal('a', this.results[0].ref)
  695. })
  696. test('matching terms returned', function () {
  697. assert.sameMembers(['candlestick', 'green'], Object.keys(this.results[0].matchData.metadata))
  698. })
  699. }
  700. suite('#search', function () {
  701. setup(function () {
  702. this.results = this.idx.search("+candlestick green")
  703. })
  704. assertions()
  705. })
  706. suite('#query', function () {
  707. setup(function () {
  708. this.results = this.idx.query(function (q) {
  709. q.term('candlestick', { presence: lunr.Query.presence.REQUIRED })
  710. q.term('green', { presence: lunr.Query.presence.OPTIONAL })
  711. })
  712. })
  713. assertions()
  714. })
  715. })
  716. suite('no match', function () {
  717. var assertions = function () {
  718. test('no matches', function () {
  719. assert.lengthOf(this.results, 0)
  720. })
  721. }
  722. suite('#query', function () {
  723. setup(function () {
  724. this.results = this.idx.query(function (q) {
  725. q.term('mustard', { presence: lunr.Query.presence.REQUIRED })
  726. q.term('plant', { presence: lunr.Query.presence.REQUIRED })
  727. })
  728. })
  729. assertions()
  730. })
  731. suite('#search', function () {
  732. setup(function () {
  733. this.results = this.idx.search('+mustard +plant')
  734. })
  735. assertions()
  736. })
  737. })
  738. suite('no matching term', function () {
  739. var assertions = function () {
  740. test('no matches', function () {
  741. assert.lengthOf(this.results, 0)
  742. })
  743. }
  744. suite('#query', function () {
  745. setup(function () {
  746. this.results = this.idx.query(function (q) {
  747. q.term('qwertyuiop', { presence: lunr.Query.presence.REQUIRED })
  748. q.term('green', { presence: lunr.Query.presence.OPTIONAL })
  749. })
  750. })
  751. assertions()
  752. })
  753. suite('#search', function () {
  754. setup(function () {
  755. this.results = this.idx.search('+qwertyuiop green')
  756. })
  757. assertions()
  758. })
  759. })
  760. suite('field match', function () {
  761. var assertions = function () {
  762. test('one result found', function () {
  763. assert.lengthOf(this.results, 1)
  764. })
  765. test('matching documents returned', function () {
  766. assert.equal('b', this.results[0].ref)
  767. })
  768. test('matching terms returned', function () {
  769. assert.sameMembers(['plant', 'green'], Object.keys(this.results[0].matchData.metadata))
  770. })
  771. }
  772. suite('#query', function () {
  773. setup(function () {
  774. this.results = this.idx.query(function (q) {
  775. q.term('plant', { presence: lunr.Query.presence.REQUIRED, fields: ['title'] })
  776. q.term('green', { presence: lunr.Query.presence.OPTIONAL })
  777. })
  778. })
  779. assertions()
  780. })
  781. suite('#search', function () {
  782. setup(function () {
  783. this.results = this.idx.search('+title:plant green')
  784. })
  785. assertions()
  786. })
  787. })
  788. suite('field and non field match', function () {
  789. var assertions = function () {
  790. test('one result found', function () {
  791. assert.lengthOf(this.results, 1)
  792. })
  793. test('matching documents returned', function () {
  794. assert.equal('b', this.results[0].ref)
  795. })
  796. test('matching terms returned', function () {
  797. assert.sameMembers(['plant', 'green'], Object.keys(this.results[0].matchData.metadata))
  798. })
  799. }
  800. suite('#search', function () {
  801. setup(function () {
  802. this.results = this.idx.search('+title:plant +green')
  803. })
  804. assertions()
  805. })
  806. suite('#query', function () {
  807. setup(function () {
  808. this.results = this.idx.query(function (q) {
  809. q.term('plant', { fields: ['title'], presence: lunr.Query.presence.REQUIRED })
  810. q.term('green', { presence: lunr.Query.presence.REQUIRED })
  811. })
  812. })
  813. assertions()
  814. })
  815. })
  816. suite('different fields', function () {
  817. var assertions = function () {
  818. test('one result found', function () {
  819. assert.lengthOf(this.results, 1)
  820. })
  821. test('matching documents returned', function () {
  822. assert.equal('b', this.results[0].ref)
  823. })
  824. test('matching terms returned', function () {
  825. assert.sameMembers(['studi', 'plant'], Object.keys(this.results[0].matchData.metadata))
  826. })
  827. }
  828. suite('#search', function () {
  829. setup(function () {
  830. this.results = this.idx.search('+title:plant +body:study')
  831. })
  832. assertions()
  833. })
  834. suite('#query', function () {
  835. setup(function () {
  836. this.results = this.idx.query(function (q) {
  837. q.term('plant', { fields: ['title'], presence: lunr.Query.presence.REQUIRED })
  838. q.term('study', { fields: ['body'], presence: lunr.Query.presence.REQUIRED })
  839. })
  840. })
  841. assertions()
  842. })
  843. })
  844. suite('different fields one without match', function () {
  845. var assertions = function () {
  846. test('no matches', function () {
  847. assert.lengthOf(this.results, 0)
  848. })
  849. }
  850. suite('#search', function () {
  851. setup(function () {
  852. this.results = this.idx.search('+title:plant +body:qwertyuiop')
  853. })
  854. assertions()
  855. })
  856. suite('#query', function () {
  857. setup(function () {
  858. this.results = this.idx.query(function (q) {
  859. q.term('plant', { fields: ['title'], presence: lunr.Query.presence.REQUIRED })
  860. q.term('qwertyuiop', { fields: ['body'], presence: lunr.Query.presence.REQUIRED })
  861. })
  862. })
  863. assertions()
  864. })
  865. })
  866. })
  867. suite('combined', function () {
  868. var assertions = function () {
  869. test('one result found', function () {
  870. assert.lengthOf(this.results, 1)
  871. })
  872. test('matching documents returned', function () {
  873. assert.equal('b', this.results[0].ref)
  874. })
  875. test('matching terms returned', function () {
  876. assert.sameMembers(['plant', 'green'], Object.keys(this.results[0].matchData.metadata))
  877. })
  878. }
  879. suite('#query', function () {
  880. setup(function () {
  881. this.results = this.idx.query(function (q) {
  882. q.term('plant', { presence: lunr.Query.presence.REQUIRED })
  883. q.term('green', { presence: lunr.Query.presence.OPTIONAL })
  884. q.term('office', { presence: lunr.Query.presence.PROHIBITED })
  885. })
  886. })
  887. assertions()
  888. })
  889. suite('#search', function () {
  890. setup(function () {
  891. this.results = this.idx.search('+plant green -office')
  892. })
  893. assertions()
  894. })
  895. })
  896. })
  897. })
  898. })