You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
'use strict' ;
exports . main = async ( event , context ) => {
/**
* 根据搜索记录,设定时间间隔来归纳出热搜数据并存储在热搜表中
*/
const SEARCHHOT = 'opendb-search-hot' ; // 热搜数据库名称
const SEARCHLOG = 'opendb-search-log' ; // 搜索记录数据库名称
const SEARCHLOG _timeZone = 604800000 ; // 归纳搜索记录时间间隔, 毫秒数, 默认为最近7天
const SEARCHHOT _size = 10 ; // 热搜条数
const DB = uniCloud . database ( ) ;
const DBCmd = DB . command ;
const $ = DB . command . aggregate ;
const SEARCHHOT _db = DB . collection ( SEARCHHOT ) ;
const SEARCHLOG _db = DB . collection ( SEARCHLOG ) ;
const timeEnd = Date . now ( ) - SEARCHLOG _timeZone ;
let {
data : searchHotData
} = await SEARCHLOG _db
. aggregate ( )
. match ( {
create _date : DBCmd . gt ( timeEnd )
} )
. group ( {
_id : {
'content' : '$content' ,
} ,
count : $ . sum ( 1 )
} )
. replaceRoot ( {
newRoot : $ . mergeObjects ( [ '$_id' , '$$ROOT' ] )
} )
. project ( {
_id : false
} )
. sort ( {
count : - 1
} )
. end ( ) ;
let now = Date . now ( ) ;
searchHotData . map ( item => {
item . create _date = now ;
return item ;
} ) . slice ( 0 , SEARCHHOT _size ) ;
// searchHotData = searchHotData.sort((a, b) => b.count - a.count).slice(0, SEARCHHOT_size);
return searchHotData . length ? await SEARCHHOT _db . add ( searchHotData ) : ''
} ;