index_builder.js 706 B

12345678910111213141516171819202122232425262728293031323334
  1. var lunr = require('./../lunr.js'),
  2. fs = require('fs')
  3. var idx = lunr(function () {
  4. this.ref('id')
  5. this.field('title', { boost: 10 })
  6. this.field('tags', { boost: 100 })
  7. this.field('body')
  8. })
  9. fs.readFile('./example/example_data.json', function (err, data) {
  10. if (err) throw err
  11. var raw = JSON.parse(data)
  12. var questions = raw.questions.map(function (q) {
  13. return {
  14. id: q.question_id,
  15. title: q.title,
  16. body: q.body,
  17. tags: q.tags.join(' ')
  18. }
  19. })
  20. questions.forEach(function (question) {
  21. idx.add(question)
  22. })
  23. fs.writeFile('./example/example_index.json', JSON.stringify(idx), function (err) {
  24. if (err) throw err
  25. console.log('done')
  26. })
  27. })