Sotaro KARASAWA 10 лет назад
Родитель
Сommit
26ddef3807
3 измененных файлов с 50 добавлено и 34 удалено
  1. 2 2
      lib/events/page.js
  2. 9 1
      lib/models/page.js
  3. 39 31
      lib/util/search.js

+ 2 - 2
lib/events/page.js

@@ -10,12 +10,12 @@ function PageEvent(crowi) {
 }
 util.inherits(PageEvent, events.EventEmitter);
 
-PageEvent.prototype.onCreate = function(context, page, user) {
+PageEvent.prototype.onCreate = function(page, user) {
   var User = this.crowi.model('User');
   var Page = this.crowi.model('Page');
 
 };
-PageEvent.prototype.onUpdate = function(context, page, user) {
+PageEvent.prototype.onUpdate = function(page, user) {
   var User = this.crowi.model('User');
   var Page = this.crowi.model('Page');
 };

+ 9 - 1
lib/models/page.js

@@ -640,6 +640,11 @@ module.exports = function(crowi) {
   };
 
   pageSchema.statics.pushRevision = function(pageData, newRevision, user) {
+    var isCreate = false;
+    if (pageData.revision === undefined) {
+      debug('pushRevision on Create');
+      isCreate = true;
+    }
 
     return new Promise(function(resolve, reject) {
       newRevision.save(function(err, newRevision) {
@@ -659,7 +664,10 @@ module.exports = function(crowi) {
           }
 
           resolve(data);
-          pageEvent.emit('update', data, user);
+          if (!isCreate) {
+            debug('pushRevision on Update');
+            pageEvent.emit('update', data, user);
+          }
         });
       });
     });

+ 39 - 31
lib/util/search.js

@@ -21,12 +21,17 @@ function SearchClient(crowi, esUri) {
     requestTimeout: 5000,
   });
 
+  this.registerUpdateEvent();
+
   this.mappingFile = crowi.resourceDir + 'search/mappings.json';
-  //this.Page = crowi.model('Page');
-  //this.Config = crowi.model('Config');
-  //this.config = crowi.getConfig();
 }
 
+SearchClient.prototype.registerUpdateEvent = function() {
+  var pageEvent = this.crowi.event('page');
+  pageEvent.on('create', this.syncPageCreated.bind(this))
+  pageEvent.on('update', this.syncPageUpdated.bind(this))
+};
+
 SearchClient.prototype.parseUri = function(uri) {
   if (!(m = uri.match(/^elasticsearch:\/\/([^:]+):([^\/]+)\/(.+)$/))) {
     throw new Error('Invalid ELASTICSEARCH_URI format. Should be elasticsearch://host:port/index_name');
@@ -65,12 +70,14 @@ SearchClient.prototype.prepareBodyForUpdate = function(body, page) {
   };
 
   var document = {
-    path: page.path,
-    body: page.revision.body,
-    username: page.creator.username,
-    comment_count: page.commentCount,
-    like_count: page.liker.length || 0,
-    updated_at: page.updatedAt,
+    doc: {
+      path: page.path,
+      body: page.revision.body,
+      comment_count: page.commentCount,
+      bookmark_count: 0, // todo
+      like_count: page.liker.length || 0,
+      updated_at: page.updatedAt,
+    }
   };
 
   body.push(command);
@@ -114,6 +121,7 @@ SearchClient.prototype.addPages = function(pages)
     self.prepareBodyForCreate(body, page);
   });
 
+  debug('addPages(): Sending Request to ES', body);
   return this.client.bulk({
     body: body,
   });
@@ -128,6 +136,7 @@ SearchClient.prototype.updatePages = function(pages)
     self.prepareBodyForUpdate(body, page);
   });
 
+  debug('updatePages(): Sending Request to ES', body);
   return this.client.bulk({
     body: body,
   });
@@ -323,28 +332,27 @@ SearchClient.prototype.searchKeywordUnderPath = function(keyword, path, option)
   return this.search(query);
 };
 
-module.exports = SearchClient;
+SearchClient.prototype.syncPageCreated = function(page, user)
+{
+  this.addPages([page])
+  .then(function(res) {
+    debug('ES Response', res);
+  })
+  .catch(function(err){
+    debug('ES Error', err);
+  });
+};
 
-/*
-
-  lib.searchPageByKeyword = function(keyword) {
-    var queryBody = {
-       query: {
-        bool: {
-          should: [
-            {term: { path: { term: keyword, boost: 2.0 } }},
-            {term: { body: { term: keyword } }}
-          ]
-        }
-      },
-      highlight : { fields : { body : {} } },
-      //sort: [{ updated: { order: "desc" } } ]
-    };
-    return client.search({
-      index: index_name,
-      body: queryBody
-    });
+SearchClient.prototype.syncPageUpdated = function(page, user)
+{
+  this.updatePages([page])
+  .then(function(res) {
+    debug('ES Response', res);
+  })
+  .catch(function(err){
+    debug('ES Error', err);
+  });
+};
 
-  };
 
-*/
+module.exports = SearchClient;