@@ -2,6 +2,8 @@ const { Client } = require('@elastic/elasticsearch');
22const esClient = new Client ( { node : process . env . ELASTICSEARCH_NODE || 'http://elasticsearch:9200' } ) ;
33const pool = require ( '../../db' ) ;
44
5+
6+
57// [Helper] ๋ ์ง ํฌ๋งท ๋ณํ (yyyy-MM-dd HH:mm:ss)
68const toEsDate = ( dateStr ) => {
79 if ( ! dateStr ) return null ;
@@ -229,13 +231,32 @@ async function enrichDataWithMySQL(results, currentUserId = null) {
229231/**
230232 * ์ ๋ ฌ ์คํฌ๋ฆฝํธ ํ๋ผ๋ฏธํฐ ์์ฑ ํฌํผ
231233 */
234+ /*
232235const getSortParams = () => {
233236 const now = new Date();
234237 return {
235238 now: now.getTime(),
236239 dayStart: new Date(now.setHours(0, 0, 0, 0)).getTime(),
237240 dayEnd: new Date(now.setHours(23, 59, 59, 999)).getTime()
238241 };
242+ };*/
243+
244+ const getSortParams = ( ) => {
245+ const now = new Date ( ) ;
246+ // ํ๊ตญ ์๊ฐ์ UTC๋ณด๋ค 9์๊ฐ ๋น ๋ฆ
247+ const kstOffset = 9 * 60 * 60 * 1000 ;
248+
249+ // 1. ํ์ฌ ์๊ฐ์ KST ๊ธฐ์ค์ผ๋ก ๋ณํ
250+ const kstNow = new Date ( now . getTime ( ) + kstOffset ) ;
251+
252+ // 2. KST ๊ธฐ์ค ์ค๋ 00:00:00๊ณผ 23:59:59์ ์์ ์ UTC ์ซ์๋ก ๊ณ์ฐ
253+ const dayStart = new Date ( kstNow . getFullYear ( ) , kstNow . getMonth ( ) , kstNow . getDate ( ) , 0 , 0 , 0 , 0 ) . getTime ( ) - kstOffset ;
254+ const dayEnd = new Date ( kstNow . getFullYear ( ) , kstNow . getMonth ( ) , kstNow . getDate ( ) , 23 , 59 , 59 , 999 ) . getTime ( ) - kstOffset ;
255+
256+ return {
257+ dayStart : dayStart ,
258+ dayEnd : dayEnd
259+ } ;
239260} ;
240261
241262/**
@@ -295,26 +316,32 @@ exports.searchPosts = async (req, res) => {
295316 index : 'boards' ,
296317 from, size,
297318 query : esQuery . bool . must . length > 0 || esQuery . bool . filter . length > 0 ? esQuery : { match_all : { } } ,
298- sort : [
299- {
300- _script : {
301- type : "number" ,
302- script : {
303- lang : "painless" ,
304- source : `
305- if (doc['end_date'].size() == 0) return 2;
306- long end = doc['end_date'].value.toInstant().toEpochMilli();
307- if (end >= params.dayStart && end <= params.dayEnd) return 0; // ์ค๋ ์ข
๋ฃ
308- if (end > params.dayEnd) return 1; // ๋ฏธ๋ ์ข
๋ฃ
309- return 3; // ๋ง๊ฐ๋จ
310- ` ,
311- params : getSortParams ( )
312- } ,
313- order : "asc"
314- }
315- } ,
316- { "created_at" : { "order" : "desc" } } // ๊ทธ ์ธ ์ต์ ์
317- ]
319+ // searchPosts์ getAllPosts์ sort ๋ถ๋ถ์ ์๋ ๋ด์ฉ์ผ๋ก ๊ต์ฒดํ์ธ์.
320+ sort : [
321+ {
322+ _script : {
323+ type : "number" ,
324+ script : {
325+ lang : "painless" ,
326+ source : `
327+ // 1. ๋ง๊ฐ์ผ ์์ผ๋ฉด ์์(2)
328+ if (doc['end_date'].size() == 0) return 2;
329+
330+ // 2. ์ ์ฅ๋ ๋ง๊ฐ ์๊ฐ์ ์ ๋์ ์ธ ์ซ์(๋ฐ๋ฆฌ์ด)๋ก ์ถ์ถ
331+ long end = doc['end_date'].value.toInstant().toEpochMilli();
332+
333+ // 3. ์ซ์๋ก ์ง์ ๋น๊ต (ํ์์กด ์ค์ ๋ฌด๊ดํ๊ฒ ์ ํํจ)
334+ if (end >= params.dayStart && end <= params.dayEnd) return 0; // ์ค๋ ์ข
๋ฃ
335+ if (end > params.dayEnd) return 1; // ๋ฏธ๋ ์ข
๋ฃ
336+ return 3; // ๊ณผ๊ฑฐ ์ข
๋ฃ (๋ง๊ฐ๋จ)
337+ ` ,
338+ params : getSortParams ( )
339+ } ,
340+ order : "asc"
341+ }
342+ } ,
343+ { "created_at" : { "order" : "desc" } }
344+ ]
318345 } ) ;
319346
320347 const cardData = await enrichDataWithMySQL ( response . hits . hits . map ( hit => hit . _source ) , req . user ?. id ) ;
@@ -339,18 +366,24 @@ exports.getAllPosts = async (req, res) => {
339366 index : 'boards' ,
340367 from, size,
341368 query : { match_all : { } } ,
369+ // searchPosts์ getAllPosts์ sort ๋ถ๋ถ์ ์๋ ๋ด์ฉ์ผ๋ก ๊ต์ฒดํ์ธ์.
342370 sort : [
343371 {
344372 _script : {
345373 type : "number" ,
346374 script : {
347375 lang : "painless" ,
348376 source : `
377+ // 1. ๋ง๊ฐ์ผ ์์ผ๋ฉด ์์(2)
349378 if (doc['end_date'].size() == 0) return 2;
379+
380+ // 2. ์ ์ฅ๋ ๋ง๊ฐ ์๊ฐ์ ์ ๋์ ์ธ ์ซ์(๋ฐ๋ฆฌ์ด)๋ก ์ถ์ถ
350381 long end = doc['end_date'].value.toInstant().toEpochMilli();
351- if (end >= params.dayStart && end <= params.dayEnd) return 0;
352- if (end > params.dayEnd) return 1;
353- return 3;
382+
383+ // 3. ์ซ์๋ก ์ง์ ๋น๊ต (ํ์์กด ์ค์ ๋ฌด๊ดํ๊ฒ ์ ํํจ)
384+ if (end >= params.dayStart && end <= params.dayEnd) return 0; // ์ค๋ ์ข
๋ฃ
385+ if (end > params.dayEnd) return 1; // ๋ฏธ๋ ์ข
๋ฃ
386+ return 3; // ๊ณผ๊ฑฐ ์ข
๋ฃ (๋ง๊ฐ๋จ)
354387 ` ,
355388 params : getSortParams ( )
356389 } ,
0 commit comments