-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from all commits
77bfc24
3c31aeb
5d7470d
186ba51
75ab5e6
66054aa
3107ae6
873f3d1
a79b9a8
0a9e8d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(), | ||
|
@@ -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); | ||
|
@@ -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) { | ||
|
@@ -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 일래스틱 서치 검색결과를 조회합니다. | ||
|
@@ -118,7 +145,7 @@ const Core = { | |
if (isNotDefaultSetSortOption(sort)) { | ||
sort = [ | ||
...sort, | ||
{ order: 'desc' }, | ||
{ _score: 'desc', order: 'desc' }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [질문] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 조건이 없으면 _score 가 반영이 되지 않고 출력이 되어서 관련순이 높은 순서대로 출력되지 않는 문제가 있어서 _socre sort를 추가하여 해결하였습니다. |
||
]; | ||
} | ||
const query = { _source: true, ...esquery, sort }; | ||
|
@@ -143,4 +170,5 @@ export const { | |
getProductSchemaByKey, | ||
getElasticSearchResults, | ||
getLastModified, | ||
getRecommandKeyword, | ||
} = Core; |
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); | ||
} | ||
}); | ||
}); | ||
} |
This file was deleted.
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; |
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, | ||
|
@@ -47,7 +50,9 @@ const productSchema = new Schema({ | |
}, | ||
message: '10장 이하의 사진만 등록 가능합니다.', | ||
}, | ||
es_type: 'string', | ||
required: true, | ||
es_indexed: true, | ||
}, | ||
location: { | ||
geo_point: { | ||
|
@@ -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: { | ||
|
@@ -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급', '사용감 있음', '전투용', '고장/부품'], | ||
|
@@ -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); | ||
|
||
|
@@ -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 })); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 위의 의견과 같습니다. 별도의 함수로 분리해 주세요 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; |
Large diffs are not rendered by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
secretRouter 좀 더 직관적인 이름으로 변경해주세요