-
Notifications
You must be signed in to change notification settings - Fork 9
/
indexSearch.js
139 lines (120 loc) · 3.32 KB
/
indexSearch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
require('dotenv').config()
const keys = require('lodash/keys')
const db = require('monk')(process.env.MONGO_DB)
const querystring = require('querystring')
const algoliasearch = require('algoliasearch')
const Throttle = require('promise-parallel-throttle')
const striptags = require('striptags')
const moment = require('moment')
const posts = db.get('posts')
const threads = db.get('forumthreads')
const client = algoliasearch(
process.env.ALGOLIA_APP_ID,
process.env.ALGOLIA_API_KEY
);
const postsIndex = client.initIndex(
process.env.ALGOLIA_POSTS_INDEX
);
const convertNumberCodes = (text) => {
return text
.replace(/–/g, ' — ')
.replace(/…/g, '...')
.replace(/’/g, '’')
.replace(/“/g, '“')
.replace(/”/g, '”')
}
function prepSearchObj(obj) {
const validKeys = [
'_id',
'id',
'date',
'date_gmt',
'guid',
'modified',
'modified_gmt',
'slug',
'status',
'type',
'link',
'title',
'topics',
'author',
'excerpt',
'score',
'mp3',
'thread',
'filterTags',
'transcriptURL',
'featuredImage',
'sponsors',
'objectID',
]
let _keys = keys(obj)
let _obj = {
objectID: obj.id,
likeCount: obj.likeCount || 0,
}
// Provide a clean title
if (obj.title && obj.title.rendered) {
_obj._title = obj.title.rendered
}
// Provide a clean description
if (obj.excerpt && obj.excerpt.rendered) {
let description = obj.excerpt.rendered
.replace(/<p[^>]*>Podcast:\s.+?<\/p>/g, '') // Remove download info
.replace(/<a[^>]*>.+?<\/a>/g, '') // Remove links
.replace(/http.{0,}Podcast: Play in new window \| Download\s/g, '') // Remove Podcast link from description
.match(/<p[^>]*>.+?<\/p>/g) // Seperate paragraphs
_obj.description = convertNumberCodes((description.length > 0) ? description[0] : '')
_obj.description = _obj.description.replace(/<p[^>]*>|<\/p>/g, '')
}
// Trim Content
if (obj.content && obj.content.rendered) {
_obj.content = {
rendered: obj.content.rendered.slice(0, 512)
}
}
if (obj.date) {
_obj.date = moment(obj.date).toDate()
_obj.date_timestamp = new Date(obj.date).getTime()
}
_keys.forEach(key => {
if (validKeys.indexOf(key) >= 0) {
_obj[key] = obj[key]
}
})
return _obj
}
const indexSearch = () => {
let query = { search_index: { $exists: false } }
let options = {}
posts
.find(query, options)
.then((reply) => {
const queue = reply.map(post => {
return async () => {
let _post = prepSearchObj(post)
let thread = _post.thread ? await threads.findOne(_post.thread) : { commentsCount: 0 }
_post.thread = {
_id: _post.thread,
commentsCount: thread.commentsCount || 0,
}
console.log('indexing ', _post.id, _post.date_timestamp)
return await postsIndex
.saveObject(_post)
.then(async ({ objectID: search_index }) => {
return await posts.update({ id: _post.id }, { $set: { search_index } })
})
}
})
return Throttle.all(queue).then(() => {
console.log('Processed all posts')
db.close()
process.exit()
})
})
.catch((err) => {
console.error('Error indexing ', err)
})
}
indexSearch()