Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

search.js 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. 'use strict';
  2. const config = require('./.config');
  3. const cheerio = require('cheerio');
  4. const filemap = require('./docs/source');
  5. const fs = require('fs');
  6. const jade = require('jade');
  7. const mongoose = require('./');
  8. mongoose.set('useCreateIndex', true);
  9. const contentSchema = new mongoose.Schema({
  10. title: { type: String, required: true },
  11. body: { type: String, required: true },
  12. url: { type: String, required: true }
  13. });
  14. contentSchema.index({ title: 'text', body: 'text' });
  15. const Content = mongoose.model('Content', contentSchema, 'Content');
  16. const contents = [];
  17. const files = Object.keys(filemap);
  18. for (const filename of files) {
  19. const file = filemap[filename];
  20. console.log(file)
  21. if (file.api) {
  22. // API docs are special, raw content is in the `docs` property
  23. for (const _class of file.docs) {
  24. for (const prop of _class.props) {
  25. const content = new Content({
  26. title: `API: ${prop.string}`,
  27. body: prop.description,
  28. url: `api.html#${prop.anchorId}`
  29. });
  30. const err = content.validateSync();
  31. if (err != null) {
  32. throw err;
  33. }
  34. contents.push(content);
  35. }
  36. }
  37. } else if (file.guide) {
  38. let text = fs.readFileSync(filename, 'utf8');
  39. text = text.substr(text.indexOf('block content') + 'block content\n'.length);
  40. text = jade.render(`div\n${text}`);
  41. const content = new Content({
  42. title: file.title,
  43. body: text,
  44. url: filename.replace('.jade', '.html').replace(/^docs/, '')
  45. });
  46. content.validateSync();
  47. const $ = cheerio.load(text);
  48. contents.push(content);
  49. // Break up individual h3's into separate content for more fine grained search
  50. $('h3').each((index, el) => {
  51. el = $(el);
  52. const title = el.text();
  53. const html = el.nextUntil('h3').html();
  54. const content = new Content({
  55. title: `${file.title}: ${title}`,
  56. body: html,
  57. url: `${filename.replace('.jade', '.html').replace(/^docs/, '')}#${el.prop('id')}`
  58. });
  59. content.validateSync();
  60. contents.push(content);
  61. });
  62. }
  63. }
  64. run().catch(error => console.error(error.stack));
  65. async function run() {
  66. await mongoose.connect(config.uri, { useNewUrlParser: true, dbName: 'mongoose' });
  67. await Content.deleteMany({});
  68. for (const content of contents) {
  69. await content.save();
  70. }
  71. const results = await Content.
  72. find({ $text: { $search: 'validate' } }, { score: { $meta: 'textScore' } }).
  73. sort({ score: { $meta: 'textScore' } }).
  74. limit(10);
  75. console.log(results.map(res => res.url));
  76. }