app.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. require([
  2. '../lunr.js',
  3. 'text!example_data.json'
  4. ], function (lunr, data) {
  5. var questions = JSON.parse(data).questions.map(function (raw) {
  6. return {
  7. id: raw.question_id,
  8. title: raw.title,
  9. body: raw.body,
  10. tags: raw.tags.join(' ')
  11. }
  12. })
  13. console.profile('load')
  14. window.idx = lunr(function () {
  15. this.ref('id')
  16. this.field('title')
  17. this.field('body')
  18. this.field('tags')
  19. questions.forEach(function (q) {
  20. this.add(q)
  21. }, this)
  22. })
  23. console.profileEnd('load')
  24. window.profile = function (term) {
  25. console.profile('search')
  26. window.idx.search(term)
  27. console.profileEnd('search')
  28. }
  29. window.search = function (term) {
  30. console.time('search')
  31. window.idx.search(term)
  32. console.timeEnd('search')
  33. }
  34. window.serialize = function () {
  35. console.time('dump')
  36. var json = JSON.stringify(window.idx)
  37. console.timeEnd('dump')
  38. var serialized = JSON.parse(json)
  39. console.profile("load")
  40. var newIdx = lunr.Index.load(serialized)
  41. console.profileEnd("load")
  42. return newIdx
  43. }
  44. })