-
Notifications
You must be signed in to change notification settings - Fork 35
/
search.js
75 lines (49 loc) · 1.54 KB
/
search.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
/* Copyright (c) 2014-2017 Richard Rodger and other contributors, MIT License */
var Wreck = require('wreck')
module.exports = function search (options) {
var seneca = this
options = seneca.util.deepextend({
elastic: {
host: 'localhost',
port: 9200,
base: 'zoo'
},
},options)
seneca.add( 'role:search,cmd:insert', cmd_insert )
seneca.add( 'role:search,cmd:search', cmd_search )
function cmd_insert (msg, reply) {
var seneca = this
var elastic = options.elastic
var url = 'http://'+elastic.host+':'+elastic.port+
'/'+elastic.base+'/mod/'+msg.data.name
console.log('INSERT URL: '+url)
Wreck.post(
url,
{json: true, payload: seneca.util.clean(msg.data)},
function (err, res, payload) {
console.log(err)
reply(err, payload)
})
}
function cmd_search (msg, reply) {
var seneca = this
var elastic = options.elastic
var url = 'http://'+elastic.host+':'+elastic.port+
'/'+elastic.base+'/_search?q='+encodeURIComponent(msg.query)
console.log('SEARCH URL: '+url)
Wreck.get(url, {json: true}, function (err, res, payload) {
console.log(err)
if( err ) return reply(err)
var qr = payload
var items = []
var hits = qr.hits && qr.hits.hits
if( hits ) {
for( var i = 0; i < hits.length; i++ ) {
var hit = hits[i]
items.push( seneca.util.clean(hit._source) )
}
}
setTimeout( function(){ reply({items:items}) }, 400)
})
}
}