Skip to content

Commit

Permalink
fix: add commnets and remove foreach
Browse files Browse the repository at this point in the history
- foreach를 제거하고 callback 함수 형 findAndUpdate로 교체
- 적당한 변수명이 생각이 나질 않아 주석 추가
  • Loading branch information
kgpyo committed Dec 18, 2019
1 parent a79b9a8 commit 0a9e8d5
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions apis/product/db/model/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { mongoosasticSettings } from '../../config';

const { Schema } = mongoose;

const KEYWORD_ANALYSIS_CYCLE = 5000;

const documentsToAnalyze = { title: [] };

const productSchema = new Schema({
Expand Down Expand Up @@ -190,16 +188,27 @@ 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);
console.log('finish');
}
return;
}

// insert keyword index
const insertKeyword = (err, { tokens }) => {
if (err) {
return;
Expand All @@ -209,17 +218,20 @@ 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: {
text: title,
analyzer: 'korean',
},
}, insertKeyword);
}, KEYWORD_ANALYSIS_CYCLE);
}, KEYWORD_ANALYSIS_INTERVAL);

module.exports = Product;

0 comments on commit 0a9e8d5

Please sign in to comment.