Просмотр исходного кода

Merge pull request #892 from weseek/feat/add-tag-field-in-ES-index

Feat/add tag field in es index
Yuki Takei 7 лет назад
Родитель
Сommit
c7315eebef

+ 3 - 0
resource/search/mappings.json

@@ -93,6 +93,9 @@
         "updated_at": {
           "type": "date",
           "format": "dateOptionalTime"
+        },
+        "tag_names": {
+          "type": "text"
         }
       }
     }

+ 1 - 0
src/client/js/components/Page/TagViewer.jsx

@@ -33,6 +33,7 @@ export default class TagViewer extends React.Component {
     if (pageId) {
       const res = await this.props.crowi.apiGet('/pages.getPageTag', { pageId });
       this.setState({ currentPageTags: res.tags });
+      this.props.sendTagData(res.tags);
     }
   }
 

+ 1 - 1
src/server/models/page-tag-relation.js

@@ -33,7 +33,7 @@ class PageTagRelation {
 
   static async createIfNotExist(pageId, tagId) {
     if (!await this.findOne({ relatedPage: pageId, relatedTag: tagId })) {
-      this.create({ relatedPage: pageId, relatedTag: tagId });
+      await this.create({ relatedPage: pageId, relatedTag: tagId });
     }
   }
 

+ 12 - 11
src/server/models/page.js

@@ -326,10 +326,11 @@ module.exports = function(crowi) {
     });
 
     // create tag and relations
-    newTags.forEach(async(tag) => {
+    /* eslint-disable no-await-in-loop */
+    for (const tag of newTags) {
       const setTag = await Tag.findOrCreate(tag);
-      PageTagRelation.createIfNotExist(page._id, setTag._id);
-    });
+      await PageTagRelation.createIfNotExist(page._id, setTag._id);
+    }
   };
 
 
@@ -1010,10 +1011,6 @@ module.exports = function(crowi) {
     await validateAppliedScope(user, grant, grantUserGroupId);
     page.applyScope(user, grant, grantUserGroupId);
 
-    if (pageTags != null) {
-      page.updateTags(pageTags);
-    }
-
     let savedPage = await page.save();
     const newRevision = Revision.prepareRevision(savedPage, body, null, user, { format });
     const revision = await pushRevision(savedPage, newRevision, user, grant, grantUserGroupId);
@@ -1021,6 +1018,10 @@ module.exports = function(crowi) {
       .populate('revision')
       .populate('creator');
 
+    if (pageTags != null) {
+      await page.updateTags(pageTags);
+    }
+
     if (socketClientId != null) {
       pageEvent.emit('create', savedPage, user, socketClientId);
     }
@@ -1052,12 +1053,12 @@ module.exports = function(crowi) {
       savedPage = await this.syncRevisionToHackmd(savedPage);
     }
 
-    if (socketClientId != null) {
-      pageEvent.emit('update', savedPage, user, socketClientId);
+    if (pageTags != null) {
+      await savedPage.updateTags(pageTags);
     }
 
-    if (pageTags != null) {
-      savedPage.updateTags(pageTags);
+    if (socketClientId != null) {
+      pageEvent.emit('update', savedPage, user, socketClientId);
     }
 
     return savedPage;

+ 15 - 5
src/server/util/search.js

@@ -170,6 +170,7 @@ SearchClient.prototype.prepareBodyForUpdate = function(body, page) {
     bookmark_count: page.bookmarkCount || 0,
     like_count: page.liker.length || 0,
     updated_at: page.updatedAt,
+    tag_names: page.tagNames,
   };
 
   document = Object.assign(document, generateDocContentsRelatedToRestriction(page));
@@ -204,6 +205,7 @@ SearchClient.prototype.prepareBodyForCreate = function(body, page) {
     like_count: page.liker.length || 0,
     created_at: page.createdAt,
     updated_at: page.updatedAt,
+    tag_names: page.tagNames,
   };
 
   document = Object.assign(document, generateDocContentsRelatedToRestriction(page));
@@ -230,11 +232,14 @@ SearchClient.prototype.prepareBodyForDelete = function(body, page) {
 
 SearchClient.prototype.addPages = async function(pages) {
   const Bookmark = this.crowi.model('Bookmark');
+  const PageTagRelation = this.crowi.model('PageTagRelation');
   const body = [];
 
   /* eslint-disable no-await-in-loop */
   for (const page of pages) {
     page.bookmarkCount = await Bookmark.countByPageId(page._id);
+    const tagRelations = await PageTagRelation.find({ relatedPage: page._id }).populate('relatedTag');
+    page.tagNames = tagRelations.map((relation) => { return relation.relatedTag.name });
     this.prepareBodyForCreate(body, page);
   }
   /* eslint-enable no-await-in-loop */
@@ -245,14 +250,17 @@ SearchClient.prototype.addPages = async function(pages) {
   });
 };
 
-SearchClient.prototype.updatePages = function(pages) {
+SearchClient.prototype.updatePages = async function(pages) {
   const self = this;
+  const PageTagRelation = this.crowi.model('PageTagRelation');
   const body = [];
 
-  pages.map((page) => {
+  /* eslint-disable no-await-in-loop */
+  for (const page of pages) {
+    const tagRelations = await PageTagRelation.find({ relatedPage: page._id }).populate('relatedTag');
+    page.tagNames = tagRelations.map((relation) => { return relation.relatedTag.name });
     self.prepareBodyForUpdate(body, page);
-    return;
-  });
+  }
 
   logger.debug('updatePages(): Sending Request to ES', body);
   return this.client.bulk({
@@ -280,6 +288,7 @@ SearchClient.prototype.addAllPages = async function() {
   const Page = this.crowi.model('Page');
   const allPageCount = await Page.allPageCount();
   const Bookmark = this.crowi.model('Bookmark');
+  const PageTagRelation = this.crowi.model('PageTagRelation');
   const cursor = Page.getStreamOfFindAll();
   let body = [];
   let sent = 0;
@@ -311,7 +320,8 @@ SearchClient.prototype.addAllPages = async function() {
         total++;
 
         const bookmarkCount = await Bookmark.countByPageId(doc._id);
-        const page = { ...doc, bookmarkCount };
+        const tagRelations = await PageTagRelation.find({ relatedPage: doc._id }).populate('relatedTag');
+        const page = { ...doc, bookmarkCount, tagNames: tagRelations.map((relation) => { return relation.relatedTag.name }) };
         self.prepareBodyForCreate(body, page);
 
         if (body.length >= 4000) {