Sotaro KARASAWA 10 лет назад
Родитель
Сommit
ab7087512b
4 измененных файлов с 41 добавлено и 36 удалено
  1. 7 6
      bin/search.js
  2. 18 23
      lib/models/page.js
  3. 13 7
      lib/util/search.js
  4. 3 0
      resource/search/mappings.json

+ 7 - 6
bin/search.js

@@ -92,19 +92,20 @@ crowi.init()
       .action(function (cmd, env) {
         var Page = crowi.model('Page');
         var search = crowi.getSearcher();
-        var keyword = "";
-
-        debug(cmd, env);
+        var keyword = cmd;
 
         search.searchKeyword(keyword, {})
           .then(function(data) {
-            console.log(colors.green('Search result: %d of %d total.'), data.meta.results, data.meta.total);
+            debug('result is', data);
+            console.log(colors.green('Search result: %d of %d total. (%d ms)'), data.meta.results, data.meta.total, data.meta.took);
 
-            return Page.findListByPageIds(data.data.map(function(p) { return p._id; }));
+            return Page.populatePageListToAnyObjects(data.data);
           }).then(function(pages) {
             pages.map(function(page) {
-              console.log(page.path);
+              console.log(page._score, page._id, page.path);
             });
+
+            process.exit(0);
           })
           .catch(function(err) {
             console.error('Error', err);

+ 18 - 23
lib/models/page.js

@@ -248,35 +248,30 @@ module.exports = function(crowi) {
     });
   };
 
-  pageSchema.statics.populatePageList = function(pageList) {
-    var Page = self;
-    var User = crowi.model('User');
+  pageSchema.statics.populatePageListToAnyObjects = function(pageIdObjectArray) {
+    var Page = this;
+    var pageIdMappings = {};
+    var pageIds = pageIdObjectArray.map(function(page, idx) {
+      if (!page._id) {
+        throw new Error('Pass the arg of populatePageListToAnyObjects() must have _id on each element.');
+      }
 
-    return new Promise(function(resolve, reject) {
-      Page.populate(
-        pageList,
-        [
-          {path: 'creator', model: 'User', select: User.USER_PUBLIC_FIELDS},
-          {path: 'revision', model: 'Revision'}
-        ],
-        function(err, pageList) {
-          if (err) {
-            return reject(err);
-          }
+      pageIdMappings[String(page._id)] = idx;
+      return page._id;
+    });
 
-          Page.populate(pageList, {path: 'revision.author', model: 'User', select: User.USER_PUBLIC_FIELDS}, function(err, data) {
-            if (err) {
-              return reject(err);
-            }
+    return new Promise(function(resolve, reject) {
+      Page.findListByPageIds(pageIds, {limit: 100}) // limit => if the pagIds is greater than 100, ignore
+      .then(function(pages) {
+        pages.forEach(function(page) {
+          Object.assign(pageIdObjectArray[pageIdMappings[String(page._id)]], page._doc);
+        });
 
-            resolve(data);
-          });
-        }
-      );
+        resolve(pageIdObjectArray);
+      });
     });
   };
 
-
   pageSchema.statics.updateCommentCount = function (page, num)
   {
     var self = this;

+ 13 - 7
lib/util/search.js

@@ -18,6 +18,7 @@ function SearchClient(crowi, esUri) {
 
   this.client = new elasticsearch.Client({
     host: this.host,
+    requestTimeout: 5000,
   });
 
   this.mappingFile = crowi.resourceDir + 'search/mappings.json';
@@ -94,6 +95,7 @@ SearchClient.prototype.prepareBodyForCreate = function(body, page) {
     body: page.revision.body,
     username: page.creator.username,
     comment_count: page.commentCount,
+    bookmark_count: 0, // todo
     like_count: page.liker.length || 0,
     created_at: page.createdAt,
     updated_at: page.updatedAt,
@@ -180,12 +182,16 @@ SearchClient.prototype.search = function(query)
   return new Promise(function(resolve, reject) {
     self.client.search(query)
     .then(function(data) {
-      var result = {};
-      result.total = data.hits.total;
-      result.results = data.hits.hits.length;
-      result.data = data.hits.hits.map(function(elm) {
-        return {_id: elm._id, _score: elm._score};
-      });
+      var result = {
+        meta: {
+          took: data.took,
+          total: data.hits.total,
+          results: data.hits.hits.length,
+        },
+        data: data.hits.hits.map(function(elm) {
+          return {_id: elm._id, _score: elm._score};
+        })
+      };
 
       resolve(result);
     }).catch(function(err) {
@@ -260,7 +266,7 @@ SearchClient.prototype.appendCriteriaForKeywordContains = function(query, keywor
     multi_match: {
       query: keyword,
       fields: [
-        "path.ja",
+        "path.ja^2", // ためしに。
         "body.ja"
       ],
       operator: "and"

+ 3 - 0
resource/search/mappings.json

@@ -67,6 +67,9 @@
         "comment_count": {
           "type": "integer"
         },
+        "bookmark_count": {
+          "type": "integer"
+        },
         "like_count": {
           "type": "integer"
         },