Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

유사어 검색을 위한 라우터 추가 및 collection 추가 #103

Merged
merged 10 commits into from
Dec 19, 2019
15 changes: 6 additions & 9 deletions apis/product/app.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import express from 'express';
import cookieParser from 'cookie-parser';
import logger from 'morgan';
import dotenv from 'dotenv';
import cors from 'cors';
import db from './db';
import indexRouter from './routes/index';
import productRouter from './routes/product';
import infoRouter from './routes/info';
import secretRouter from './routes/secret';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

secretRouter 좀 더 직관적인 이름으로 변경해주세요

import etagGenerator from './routes/middleware/etag-generator';

dotenv.config();
db().catch(() => {
process.exit();
});
import { dbConnect } from './config';

const app = express();

app.use(dbConnect());

app.use(cors());
app.use(logger('dev'));
app.use(express.json());
Expand All @@ -27,12 +24,12 @@ app.get('*', etagGenerator);
app.use('/', indexRouter);
app.use('/info', infoRouter);
app.use('/products', productRouter);
app.use('/secrets', secretRouter);

// eslint-disable-next-line no-unused-vars
app.use((err, req, res, next) => {
const message = err.message.split('|')[0].trim();
res.status(err.status || 500);
res.json(message);
res.json(err.message);
});

module.exports = app;
1 change: 1 addition & 0 deletions apis/product/bin/www.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Module dependencies.
*/

var dotenv = require('dotenv').config();
var app = require('../app');
var debug = require('debug')('template:server');
var http = require('http');
Expand Down
37 changes: 37 additions & 0 deletions apis/product/config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import mongoose from 'mongoose';

export const dbConnect = () => {
const options = {
useNewUrlParser: true,
useUnifiedTopology: true,
keepAlive: false,
};
let errorMessage;
mongoose.connect(`${process.env.MONGO_URL}`, options, (err) => {
if (err) {
errorMessage = err.toString();
}
});
return (req, res, next) => {
if (errorMessage) {
next({ status: 500, message: errorMessage });
} else {
next();
}
};
};

export const mongoosasticSettings = (process.env.NODE_ENV === 'test') ? {} : {
hosts: process.env.ELASTICSEARCH,
port: process.env.ESPORT,
bulk: {
size: 100,
delay: 1000,
},
};

export const redisConnection = {
port: process.env.REDIS_PORT,
host: process.env.REDIS_HOST,
password: process.env.REDIS_PASSWORD,
};
40 changes: 34 additions & 6 deletions apis/product/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import message from './message';
import CODE from './code';

const Product = model.product;
const Keyword = model.keyword;

const Core = {
lastModifed: Date.now(),
Expand Down Expand Up @@ -53,14 +54,12 @@ const Core = {
/**
* 등록된 중고상품 정보 수정
* @description 유저 아이디와 실제 document를 등록한 사용자가 일치할 경우에만 해당 정보를 수정합니다.
* 상태값이 '비공개'일 경우 일래스틱 서치에서 인덱스 목록에 제거합니다.
* @param {String} _id 기본키(Object_ID)
* @param {String} userId 유저정보(사용자)
* @param {Object} contents 업데이트할 내용(product 모델 참조)
* @return {Promise<Object>} Object(Product object)
*/
async updateProduct(_id, userId, contents) {
const isCurrentStatusPrivate = (document) => document.currentStatus === '비공개';
Core.refreshLastModified();
try {
const product = await Product.findById(_id);
Expand All @@ -70,9 +69,6 @@ const Core = {
Object.keys(contents).forEach((key) => {
product[key] = contents[key];
});
if (isCurrentStatusPrivate(product)) {
await product.unIndex();
}
const result = await product.save();
return result;
} catch (e) {
Expand Down Expand Up @@ -101,6 +97,37 @@ const Core = {
}
},

/**
* 키워드 검색
* @description 입력한 검색어와 유사환 결과를 포함하여 리스트를 반환합니다.
* 기본값은 2글자는 최대 1글자 까지 오차를 허용하며, 그 이상은 2글자입니다.
* @param {String} keyword 추천받을 검색어
* @param {Number|String} fuzzy 유사어 빈도
*/
async getRecommandKeyword(keyword, fuzzy) {
try {
const fuzziness = fuzzy || Core.getFuzzinessDefault(keyword);
const list = await Keyword.search({
query: {
match: {
word: { query: keyword, fuzziness },
},
},
}, {});
return list;
} catch (e) {
console.log(e);
throw new Error(message.errorProcessingElasticSearch);
}
},

getFuzzinessDefault(keyword) {
if (keyword.length === 2) {
return 1;
}
return 'auto';
},

/**
* 일래스틱 서치 검색
* @description 일래스틱 서치 검색결과를 조회합니다.
Expand All @@ -118,7 +145,7 @@ const Core = {
if (isNotDefaultSetSortOption(sort)) {
sort = [
...sort,
{ order: 'desc' },
{ _score: 'desc', order: 'desc' },
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[질문]
score desc는 디폴트 아닌가요? 명시적으로 선언하는것이 필요한가요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 조건이 없으면 _score 가 반영이 되지 않고 출력이 되어서 관련순이 높은 순서대로 출력되지 않는 문제가 있어서 _socre sort를 추가하여 해결하였습니다.

];
}
const query = { _source: true, ...esquery, sort };
Expand All @@ -143,4 +170,5 @@ export const {
getProductSchemaByKey,
getElasticSearchResults,
getLastModified,
getRecommandKeyword,
} = Core;
12 changes: 12 additions & 0 deletions apis/product/db/common/statics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// esSearch convert promise
export default async function (query, options) {
return new Promise((resolve, reject) => {
this.esSearch(query, options, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
}
19 changes: 0 additions & 19 deletions apis/product/db/index.js

This file was deleted.

25 changes: 25 additions & 0 deletions apis/product/db/model/keyword.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import mongoose from 'mongoose';
import mongoosastic from 'mongoosastic';
import promiseSearch from '../common/statics';
import { mongoosasticSettings } from '../../config';

const { Schema } = mongoose;

const keywordScheme = new Schema({
word: {
type: String,
required: true,
es_indexed: true,
es_type: 'text',
unique: true,
},
});

keywordScheme.plugin(mongoosastic, mongoosasticSettings);

keywordScheme.static('search', promiseSearch);

const Keyword = mongoose.model('Keyword', keywordScheme);


module.exports = Keyword;
102 changes: 72 additions & 30 deletions apis/product/db/model/product.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import mongoose from 'mongoose';
import mongoosastic from 'mongoosastic';
import dotenv from 'dotenv';

dotenv.config();
import Keyword from './keyword';
import promiseSearch from '../common/statics';
import { mongoosasticSettings } from '../../config';

const { Schema } = mongoose;

const documentsToAnalyze = { title: [] };

const productSchema = new Schema({
title: {
type: String,
Expand Down Expand Up @@ -47,7 +50,9 @@ const productSchema = new Schema({
},
message: '10장 이하의 사진만 등록 가능합니다.',
},
es_type: 'string',
required: true,
es_indexed: true,
},
location: {
geo_point: {
Expand Down Expand Up @@ -75,10 +80,9 @@ const productSchema = new Schema({
es_indexed: true,
},
interests: {
type: Number,
default: 0,
required: true,
es_type: 'integer',
type: Array,
required: false,
es_type: 'string',
es_indexed: true,
},
currentStatus: {
Expand All @@ -89,6 +93,12 @@ const productSchema = new Schema({
es_type: 'string',
es_indexed: true,
},
buyer: {
type: String,
default: '',
es_type: 'string',
es_indexed: true,
},
productStatus: {
type: String,
enum: ['미개봉', '미사용', 'A급', '사용감 있음', '전투용', '고장/부품'],
Expand Down Expand Up @@ -123,32 +133,18 @@ const productSchema = new Schema({
timestamps: { createdAt: true, updatedAt: true },
});

productSchema.plugin(mongoosastic, mongoosasticSettings);

productSchema.plugin(mongoosastic, {
hosts: [
`${process.env.ELASTICSEARCH}`,
],
bulk: {
size: 100,
delay: 1000,
},
filter: (doc) => doc.currentStatus === '비공개',
type: '_doc',
});
productSchema.static('search', promiseSearch);

function customSearch(query, options) {
return new Promise((resolve, reject) => {
this.esSearch(query, options, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
}
const pushKeywordForTokenization = (doc) => {
documentsToAnalyze.title = [...documentsToAnalyze.title, doc.title];
};

productSchema.static('search', customSearch);
productSchema.post('save', pushKeywordForTokenization);
productSchema.post('insertMany', (error, docs) => {
docs.forEach(pushKeywordForTokenization);
});

const Product = mongoose.model('Product', productSchema);

Expand Down Expand Up @@ -192,4 +188,50 @@ Product.createMapping({
},
}, () => { });

/*
사용자가 등록한 제목을 분석하여 키워드 index에 저장
요청할 때마다 모든 단어를 처리하기에는 과도한 요청이 올 수 있어
5초 단위로 처리
*/
const KEYWORD_ANALYSIS_INTERVAL = 5000;
const timer = setInterval(() => {
// 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;
}
const words = tokens
.filter(({ token }) => token.length >= 2)
.map(({ token }) => ({ word: token }));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

위의 의견과 같습니다. 별도의 함수로 분리해 주세요

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

재사용이 안되는 것 같아서 그대로 두었습니다.

const wordSet = new Set();
words.forEach((word) => (wordSet.add(word)));
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_INTERVAL);

module.exports = Product;
2 changes: 1 addition & 1 deletion apis/product/db/seeds/20191209.json

Large diffs are not rendered by default.

Loading