From 0a9e8d5fbbe5b081a936433d9225eec60a5fa04e Mon Sep 17 00:00:00 2001 From: kgpyo Date: Wed, 18 Dec 2019 22:55:34 +0900 Subject: [PATCH] fix: add commnets and remove foreach MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - foreach를 제거하고 callback 함수 형 findAndUpdate로 교체 - 적당한 변수명이 생각이 나질 않아 주석 추가 --- apis/product/db/model/product.js | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/apis/product/db/model/product.js b/apis/product/db/model/product.js index 9a2b23e..dfe67ee 100644 --- a/apis/product/db/model/product.js +++ b/apis/product/db/model/product.js @@ -6,8 +6,6 @@ import { mongoosasticSettings } from '../../config'; const { Schema } = mongoose; -const KEYWORD_ANALYSIS_CYCLE = 5000; - const documentsToAnalyze = { title: [] }; const productSchema = new Schema({ @@ -190,9 +188,18 @@ Product.createMapping({ }, }, () => { }); +/* + 사용자가 등록한 제목을 분석하여 키워드 index에 저장 + 요청할 때마다 모든 단어를 처리하기에는 과도한 요청이 올 수 있어 + 5초 단위로 처리 +*/ +const KEYWORD_ANALYSIS_INTERVAL = 5000; const timer = setInterval(() => { - const title = documentsToAnalyze.title.slice(0, 100).join(' '); + // 100개씩 끊기 + const title = documentsToAnalyze.title.slice(0, 100).join('\n'); documentsToAnalyze.title = documentsToAnalyze.title.slice(100); + + // seed 인경우 더이상 업데이트할 데이터가 없으면 종료 if (!title.length) { if (process.env.NODE_ENV === 'development') { clearInterval(timer); @@ -200,6 +207,8 @@ const timer = setInterval(() => { } return; } + + // insert keyword index const insertKeyword = (err, { tokens }) => { if (err) { return; @@ -209,10 +218,13 @@ const timer = setInterval(() => { .map(({ token }) => ({ word: token })); const wordSet = new Set(); words.forEach((word) => (wordSet.add(word))); - wordSet.forEach(async (word) => { - await Keyword.findOneAndUpdate(word, word, { upsert: true }); + wordSet.forEach((word) => { + Keyword.findOneAndUpdate(word, word, { upsert: true }, () => { + }); }); }; + + // analyze test 결과를 keyword index에 저장 Product.esClient.indices.analyze({ index: 'products', body: { @@ -220,6 +232,6 @@ const timer = setInterval(() => { analyzer: 'korean', }, }, insertKeyword); -}, KEYWORD_ANALYSIS_CYCLE); +}, KEYWORD_ANALYSIS_INTERVAL); module.exports = Product;