search.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. var program = require('commander')
  2. , debug = require('debug')('crowi:console:search-util')
  3. , colors = require('colors')
  4. , crowi = new (require('../lib/crowi'))(__dirname + '/../', process.env)
  5. ;
  6. crowi.init()
  7. .then(function(app) {
  8. program
  9. .version(crowi.version);
  10. program
  11. .command('create-index')
  12. .action(function (cmd, env) {
  13. var search = crowi.getSearcher();
  14. search.buildIndex()
  15. .then(function(data) {
  16. console.log(data);
  17. })
  18. .then(function() {
  19. process.exit();
  20. })
  21. .catch(function(err) {
  22. console.log("Error", err);
  23. })
  24. });
  25. program
  26. .command('add-pages')
  27. .action(function (cmd, env) {
  28. var search = crowi.getSearcher();
  29. search.addAllPages()
  30. .then(function(data) {
  31. if (data.errors) {
  32. console.error(colors.red.underline('Failed to index all pages.'));
  33. console.error("");
  34. data.items.forEach(function(item, i) {
  35. var index = item.index || null;
  36. if (index && index.status != 200) {
  37. console.error(colors.red('Error item: id=%s'), index._id)
  38. console.error('error.type=%s, error.reason=%s', index.error.type, index.error.reason);
  39. console.error(index.error.caused_by);
  40. }
  41. //debug('Item', i, item);
  42. });
  43. } else {
  44. console.log('Data is successfully indexed.');
  45. }
  46. process.exit(0);
  47. })
  48. .catch(function(err) {
  49. console.log("Error", err);
  50. });
  51. });
  52. program
  53. .command('rebuild-index')
  54. .action(function (cmd, env) {
  55. var search = crowi.getSearcher();
  56. search.deleteIndex()
  57. .then(function(data) {
  58. if (!data.errors) {
  59. console.log('Index deleted.');
  60. }
  61. return search.buildIndex();
  62. })
  63. .then(function(data) {
  64. if (!data.errors) {
  65. console.log('Index created.');
  66. }
  67. return search.addAllPages();
  68. })
  69. .then(function(data) {
  70. if (!data.errors) {
  71. console.log('Data is successfully indexed.');
  72. }
  73. process.exit(0);
  74. })
  75. .catch(function(err) {
  76. console.error('Error', err);
  77. });
  78. });
  79. program
  80. .command('search')
  81. .action(function (cmd, env) {
  82. var Page = crowi.model('Page');
  83. var search = crowi.getSearcher();
  84. var keyword = cmd;
  85. search.searchKeyword(keyword, {})
  86. .then(function(data) {
  87. debug('result is', data);
  88. console.log(colors.green('Search result: %d of %d total. (%d ms)'), data.meta.results, data.meta.total, data.meta.took);
  89. return Page.populatePageListToAnyObjects(data.data);
  90. }).then(function(pages) {
  91. pages.map(function(page) {
  92. console.log(page._score, page._id, page.path);
  93. });
  94. process.exit(0);
  95. })
  96. .catch(function(err) {
  97. console.error('Error', err);
  98. process.exit(0);
  99. });
  100. });
  101. program.parse(process.argv);
  102. }).catch(crowi.exitOnError);
  103. //program
  104. // .command('search [query]', 'search with optional query')
  105. // .command('list', 'list packages installed', {isDefault: true})