search_test.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. module('search', {
  2. setup: function () {
  3. var idx = new lunr.Index
  4. idx.field('body')
  5. idx.field('title', { boost: 10 })
  6. ;([{
  7. id: 'a',
  8. title: 'Mr. Green kills Colonel Mustard',
  9. body: 'Mr. Green killed Colonel Mustard in the study with the candlestick. Mr. Green is not a very nice fellow.',
  10. wordCount: 19
  11. },{
  12. id: 'b',
  13. title: 'Plumb waters plant',
  14. body: 'Professor Plumb has a green plant in his study',
  15. wordCount: 9
  16. },{
  17. id: 'c',
  18. title: 'Scarlett helps Professor',
  19. body: 'Miss Scarlett watered Professor Plumbs green plant while he was away from his office last week.',
  20. wordCount: 16
  21. },{
  22. id: 'd',
  23. title: 'title',
  24. body: 'handsome',
  25. },{
  26. id: 'e',
  27. title: 'title',
  28. body: 'hand',
  29. }]).forEach(function (doc) { idx.add(doc) })
  30. this.idx = idx
  31. }
  32. })
  33. test('returning the correct results', function () {
  34. var results = this.idx.search('green plant')
  35. equal(results.length, 2)
  36. equal(results[0].ref, 'b')
  37. })
  38. test('search term not in the index', function () {
  39. var results = this.idx.search('foo')
  40. equal(results.length, 0)
  41. })
  42. test('one search term not in the index', function () {
  43. var results = this.idx.search('foo green')
  44. equal(results.length, 0)
  45. })
  46. test('search contains one term not in the index', function () {
  47. var results = this.idx.search('green foo')
  48. equal(results.length, 0)
  49. })
  50. test('search takes into account boosts', function () {
  51. var results = this.idx.search('professor')
  52. equal(results.length, 2)
  53. equal(results[0].ref, 'c')
  54. ok(results[0].score > 10 * results[1].score)
  55. })
  56. test('search boosts exact matches', function () {
  57. var results = this.idx.search('hand')
  58. equal(results.length, 2)
  59. equal(results[0].ref, 'e')
  60. ok(results[0].score > results[1].score)
  61. })
  62. test('ref type is not changed to a string', function () {
  63. var idx = new lunr.Index
  64. idx.field('type')
  65. var objKey = {},
  66. arrKey = [],
  67. dateKey = new Date,
  68. numKey = 1,
  69. strKey = "foo"
  70. idx.add({id: objKey, type: "object"})
  71. idx.add({id: arrKey, type: "array"})
  72. idx.add({id: dateKey, type: "date"})
  73. idx.add({id: numKey, type: "number"})
  74. idx.add({id: strKey, type: "string"})
  75. deepEqual(idx.search("object")[0].ref, objKey)
  76. deepEqual(idx.search("array")[0].ref, arrKey)
  77. deepEqual(idx.search("date")[0].ref, dateKey)
  78. deepEqual(idx.search("number")[0].ref, numKey)
  79. deepEqual(idx.search("string")[0].ref, strKey)
  80. })