-
Notifications
You must be signed in to change notification settings - Fork 2
/
research.js
158 lines (136 loc) · 4.59 KB
/
research.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
let redis = require("redis")
let _ = require("lodash")
'use strict'
class Researchjs {
/**
*
* @param {Object} rediConnection {host, port}
* @param {Boolean} withScores
*/
constructor(rediConnection, opts) {
this.db = redis.createClient(rediConnection)
this.withScores = true
this.highlight = opts.highlight!=undefined ? opts.highlight : false
}
/**
* Check Index is Available or Not
* @param {String} indexName
* @return {Promise}
*/
indexIsAvailable( indexName ) {
console.info("== CHECKING INDEX ==")
return new Promise( (resolve, reject) => {
this.db.send_command('FT.INFO',[indexName], (err, reply) => {
if (err) {
return resolve(false)
}
return resolve(true)
})
})
}
/**
*
* @param {String} indexName
* @param {Object} schemas
*/
createIndex( indexName, schemas ) {
return new Promise( async (resolve, reject) => {
try{
var isAvailable = await this.indexIsAvailable(indexName)
if (isAvailable){
return reject("Index is already exists")
}
var sch = [indexName, 'SCHEMA']
schemas.forEach( (schema) =>{
sch.push(schema.field)
schema.attributes.forEach( (attr) => {
sch.push(attr)
})
})
this.db.send_command('FT.CREATE',sch, (err, reply) => {
if (err) {
return reject(err)
}
return resolve(true)
})
}catch(err){
return reject(err)
}
})
}
/**
* Search into Redisearch
* @param {String} key
* @param {Array} query
* @return {Promise}
*/
search( key, query ) {
var totalRS, dataRS, errRS = null
return new Promise( (resolve, reject) => {
try {
console.log("======= SEARCHING DOCUMENT=======")
console.log(`QUERY : ${query}`)
var q = [key]
for (var i in query){
q.push(query[i])
}
if (this.withScores) q.push('WITHSCORES')
if (this.highlight) q.push('HIGHLIGHT','TAGS','<span style="background:yellow">','</span>')
console.time("EXECUTION TIME")
this.db.send_command('FT.SEARCH',q, (err, reply) => {
if (err){
return reject(err)
}
// Keep total record into new variable
totalRS = reply[0]
reply.shift()
// We need chunk an array, so this will readable for user
let chuckSize = this.withScores ? 3 : 2
var dataDetail = _.chunk(reply, chuckSize)
dataRS = this.parseData(dataDetail)
console.timeEnd("EXECUTION TIME")
return resolve({totalRS, dataRS, errRS})
})
}catch(err){
resolve({totalRS, dataRS, errRS})
}
})
}
add(newRecord){
return new Promise( (resolve, reject) => {
this.db.send_command('FT.ADD',newRecord, (err, reply) => {
if (err) {
return reject(err)
}else{
return resolve(newRecord)
}
})
} )
}
/**
* Parse Data from redisearch
* so it will readable
* @param {Array} array
* @return {Array}
*/
parseData(array) {
return [].concat.apply([],
array.map(function(elem,i) {
var dt = elem[2]
var newArray = new Array()
var newRecord = {}
newRecord["id"] = elem[0]
newRecord["score"] = elem[1]
for (i in dt){
if (i%2===0) {
var k = dt[i]
newRecord[ k ] = dt[ parseInt(i) + 1 ]
newArray = newRecord
}
}
return [ newArray ]
})
);
}
}
module.exports = Researchjs