-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
114 lines (104 loc) · 4.6 KB
/
index.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
var _ = require('underscore')
module.exports = {
/**
* Convert a mongodb query into an ES query
* @param {Object} q - the mongodb query
* @return {Object} the ES query
*/
// https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html
convert: function(q) {
console.log('query')
console.log(JSON.stringify(q))
// http://stackoverflow.com/questions/15690706/recursively-looping-through-an-object-to-build-a-property-list
// recursively list the properties of this object
var es = {
query: {
bool: {
must: [],
//should: []
}
}
}
es.query.bool.must.push({bool:{must:[]}})
es.query.bool.must.push({bool:{should:[]}})
//es.query.bool.should.push({bool:{must:[]}})
//es.query.bool.should.push({bool:{should:[]}})
var inAnd, inOr, inIn
var inProps = []
function iterate(obj) {
for (var property in obj) {
console.log('\n')
if (obj.hasOwnProperty(property)) {
// operator
if (typeof obj[property] == "object" && (property == "$and" || obj[property].$and)) {
console.log('step1')
console.log(property)
console.log(obj[property])
inAnd = true
inOr = false
iterate(obj[property])
}
// operator
else if (typeof obj[property] == "object" && (property == "$or" || obj[property].$or)) {
console.log('step2')
console.log(property)
console.log(obj[property])
inOr = true
inAnd = false
iterate(obj[property])
}
// operator
else if (typeof obj[property] == "object" && (property == "$in" || obj[property].$in)) {
console.log('step3')
console.log(property)
console.log(obj[property])
inProps.push(property)
inIn = true
inOr = false
inAnd = false
iterate(obj[property])
}
// non-operator (a number index) ... could have operator inside though...
else if (typeof obj[property] == "object" && !obj[property].$and && !obj[property].$or && !obj[property].$in) {
var innerOperator = false
_.each(Object.keys(obj[property]), function(key) {
if (obj[property][key].$in || obj[property][key].$and || obj[property][key].$or) {
innerOperator = true
}
})
if (!innerOperator) {
console.log('step4')
console.log(property)
console.log(obj[property])
if (inAnd === true) {
console.log('step5 pushing: ' + JSON.stringify({match: obj[property]}))
es.query.bool.must[0].bool.must.push({match: obj[property]})
}
if (inOr === true) {
console.log('step7 pushing: ' + JSON.stringify({match: obj[property]}))
es.query.bool.must[1].bool.should.push({match: obj[property]})
}
}
else iterate(obj[property])
}
else if (inIn) {
var mq = {match:{}}
mq.match[inProps[0]] = obj[property]
console.log('step8 pushing: ' + JSON.stringify(mq))
es.query.bool.must[0].bool.must.push(mq)
}
else {
var mq = {match:{}}
mq.match[property] = obj[property]
console.log('step9 pushing: ' + JSON.stringify(mq))
es.query.bool.must[0].bool.must.push(mq)
}
}
}
}
iterate(q)
console.log('result')
console.log(JSON.stringify(es))
return es
}
};