search.js 3.5 KB

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