search.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * Search
  3. */
  4. //module.exports = SearchClient;
  5. /*
  6. var elasticsearch = require('elasticsearch'),
  7. debug = require('debug')('crowi:lib:search'),
  8. function SearchClient(crowi) {
  9. this.crowi = crowi;
  10. this.Page = crowi.model('Page');
  11. this.Config = crowi.model('Config');
  12. this.config = crowi.getConfig();
  13. }
  14. SearchClient.prototype.deleteIndex = function() {
  15. };
  16. */
  17. module.exports = function(crowi) {
  18. var elasticsearch = require('elasticsearch'),
  19. debug = require('debug')('crowi:lib:search'),
  20. Page = crowi.model('Page'),
  21. Config = crowi.model('Config'),
  22. config = crowi.getConfig(),
  23. TYPE_PAGE = 'page',
  24. SLOW_INTERVAL = 200, // 200ms interval.
  25. lib = {};
  26. // TODO: configurable
  27. var host = '127.0.0.1:9200';
  28. var index_name = 'crowi';
  29. var default_mapping_file = crowi.resourceDir + 'search/mappings.json';
  30. var client = new elasticsearch.Client({
  31. host: host,
  32. });
  33. lib.deleteIndex = function() {
  34. return client.indices.delete({
  35. index: index_name
  36. });
  37. };
  38. lib.buildIndex = function() {
  39. return client.indices.create({
  40. index: index_name,
  41. body: require(default_mapping_file)
  42. });
  43. };
  44. lib.rebuildIndex = function() {
  45. var self = this;
  46. return self.deleteIndex()
  47. .then(function(data) {
  48. return self.buildIndex();
  49. });
  50. };
  51. lib.addAllPages = function() {
  52. var offset = 0;
  53. var stream = Page.getStreamOfFindAll();
  54. var self = this;
  55. stream.on('data', function (doc) {
  56. if (!doc.creator || !doc.revision) {
  57. debug('Skipped', doc.path);
  58. return ;
  59. }
  60. var likeCount = doc.liker.length;
  61. var bookmarkCount = 0; // TODO
  62. var updated = doc.updatedAt; // TODO
  63. self.addPage(doc._id.toString(), doc.path, doc.revision.body, doc.creator.username, likeCount, bookmarkCount, updated, true)
  64. .then(function(data) {
  65. debug('Page Added', data);
  66. }).catch(function (err) {
  67. debug('Error addPage:', err);
  68. });
  69. //debug('Data received: ', doc.path, doc.liker.length, doc.revision.body);
  70. }).on('error', function (err) {
  71. debug('Error stream:', err);
  72. // handle err
  73. }).on('close', function () {
  74. debug('Close');
  75. // all done
  76. });
  77. };
  78. /**
  79. * @return Promise
  80. */
  81. lib.addPage = function(id, path, body, creator, likeCount, bookmarkCount, updated, is_public) {
  82. var self = this;
  83. return client.create({
  84. index: index_name,
  85. type: 'page',
  86. id: id,
  87. body: {
  88. path: path,
  89. body: body,
  90. creator: creator,
  91. likeCount: likeCount,
  92. bookmarkCount: bookmarkCount,
  93. is_public: is_public,
  94. updated: updated,
  95. }
  96. });
  97. };
  98. lib.updatePage = function(id, path, body, creator, likeCount, bookmarkCount, updated, is_public) {
  99. };
  100. lib.searchPageByKeyword = function(keyword) {
  101. var queryBody = {
  102. query: {
  103. bool: {
  104. should: [
  105. {term: { path: { term: keyword, boost: 2.0 } }},
  106. {term: { body: { term: keyword } }}
  107. ]
  108. }
  109. },
  110. highlight : { fields : { body : {} } },
  111. //sort: [{ updated: { order: "desc" } } ]
  112. };
  113. return client.search({
  114. index: index_name,
  115. body: queryBody
  116. });
  117. /*
  118. {
  119. "query": {
  120. "bool": {
  121. "should": [
  122. {"term": {
  123. "path": {
  124. "term": "php",
  125. "boost": 2.0
  126. }
  127. }},
  128. {"term": {
  129. "body": {
  130. "term": "php"
  131. }
  132. }}
  133. ]
  134. }
  135. },
  136. "highlight" : {
  137. "fields" : {
  138. "body" : {}
  139. }
  140. },
  141. "sort": [
  142. {
  143. "updated": {
  144. "order": "desc"
  145. }
  146. }
  147. ]
  148. }
  149. */
  150. };
  151. lib.searchPageByLikeCount = function() {
  152. };
  153. return lib;
  154. };