-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathnodejs-documents-values.js
More file actions
311 lines (297 loc) · 11.1 KB
/
Copy pathnodejs-documents-values.js
File metadata and controls
311 lines (297 loc) · 11.1 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* Copyright (c) 2015-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
*/
var should = require('should');
var testconfig = require('../etc/test-config-qa.js');
var marklogic = require('../');
var q = marklogic.queryBuilder;
var t = marklogic.valuesBuilder;
var db = marklogic.createDatabaseClient(testconfig.restReaderConnection);
var dbWriter = marklogic.createDatabaseClient(testconfig.restWriterConnection);
var dbAdmin = marklogic.createDatabaseClient(testconfig.restAdminConnection);
describe('Document tuples test', function () {
before(function (done) {
this.timeout(10000);
dbWriter.documents.write({
uri: '/test/query/matchDir/doc1.json',
collections: ['matchCollection1'],
contentType: 'application/json',
content: {
title: 'Vannevar Bush',
popularity: 5,
id: '0011',
date: '2005-01-01',
price: {
amt: 0.1
},
values: [{ score: 56.7 }, { rate: 3 }],
p: 'Vannevar Bush wrote an article for The Atlantic Monthly'
}
}, {
uri: '/test/query/matchDir/doc2.json',
collections: ['matchCollection1', 'matchCollection2'],
contentType: 'application/json',
content: {
title: 'The Bush article',
popularity: 4,
id: '0012',
date: '2006-02-02',
price: {
amt: 0.12
},
values: [{ score: 92.45 }, { rate: 5 }],
p: 'The Bush article described a device called a Memex'
}
}, {
uri: '/test/query/matchDir/doc3.json',
collections: ['matchCollection2'],
contentType: 'application/json',
content: {
title: 'For 1945',
popularity: 3,
id: '0013',
date: '2007-03-03',
price: {
amt: 1.23
},
values: [{ score: 33.56 }, { rate: 1 }],
p: 'For 1945, the thoughts expressed in the Atlantic Monthly were groundbreaking'
}
}, {
uri: '/test/query/matchDir/doc4.json',
collections: [],
contentType: 'application/json',
content: {
title: 'Vannevar served',
popularity: 5,
id: '0024',
date: '2008-04-04',
price: {
amt: 12.34
},
values: [{ score: 12.34 }, { rate: 3 }],
p: 'Vannevar served as a prominent policymaker and public intellectual'
}
}, {
uri: '/test/query/matchList/doc5.json',
collections: ['matchList'],
contentType: 'application/json',
content: {
title: 'The memex',
popularity: 5,
id: '0026',
date: '2009-05-05',
price: {
amt: 123.45
},
values: [{ score: 77.678 }, { rate: 2 }],
p: 'The Memex, unfortunately, had no automated search feature'
}
}, {
uri: '/test/query/matchList/doc6.xml',
collections: ['matchList'],
contentType: 'application/xml',
content: '<Employee><name>John</name></Employee>'
}, {
uri: '/test/query/matchList/doc7.xml',
collections: ['matchList'],
contentType: 'application/xml',
content: '<Employee xmlns="http://aaa.com" ><firstname>John</firstname><id>0081</id></Employee>'
}).
result(function (response) {
done();
}, done);
});
/*it('should do values on pathindex Bug35160', function(done){
this.timeout(10000);
db.values.read(
t.fromIndexes(
t.range(t.pathIndex("/hi:Employee/hi:firstname",{hi:"http://aaa.com"}))
)
).result(function(response) {
//console.log(JSON.stringify(response, null, 4));
var strData = JSON.stringify(response);
strData.should.containEql('"frequency":1,"distinct-value":["John"]');
// strData.should.containEql('"frequency":1,"distinct-value":["92.45"]');
done();
}, done);
});*/
it('should do values on score', function (done) {
this.timeout(10000);
db.values.read(
t.fromIndexes(
t.range('score', 'xs:double')
).
where(
t.word('title', 'bush')
)
).result(function (response) {
//console.log(JSON.stringify(response, null, 4));
var strData = JSON.stringify(response);
strData.should.containEql('"frequency":1,"distinct-value":["56.7"]');
strData.should.containEql('"frequency":1,"distinct-value":["92.45"]');
done();
}, done);
});
it('should do values on simple parse', function (done) {
this.timeout(10000);
db.values.read(
t.fromIndexes(
t.range('score', 'xs:double')
).
where(
q.parsedFrom('intitle:"The memex"',
q.parseBindings(
q.value('title', q.bind('intitle'))
)
)
)
).result(function (response) {
//console.log(JSON.stringify(response, null, 2));
response['values-response'].tuple.length.should.equal(1);
done();
}, done);
});
it('should do values on complex parse', function (done) {
this.timeout(10000);
db.values.read(
t.fromIndexes(
t.range('score', 'xs:double')
).
where(
q.parsedFrom('"Vannevar" AND theId:0024 OR (pop LT 4)',
q.parseBindings(
q.word('title', q.bindDefault()),
q.value('id', q.bind('theId')),
q.range('popularity', q.datatype('int'), q.bind('pop'))
)
)
)
).result(function (response) {
//console.log(JSON.stringify(response, null, 2));
response['values-response'].tuple.length.should.equal(2);
done();
}, done);
});
it('should do values aggregate on parse', function (done) {
this.timeout(10000);
db.values.read(
t.fromIndexes(
t.range('score', 'xs:double')
).
where(
q.parsedFrom('intitle:bush',
q.parseBindings(
q.word('title', q.bind('intitle'))
)
)
).
aggregates('sum')
).result(function (response) {
//console.log(JSON.stringify(response, null, 2));
var strData = JSON.stringify(response);
strData.should.containEql('"aggregate-result":[{"name":"sum","_value":"149.15"}]');
done();
}, done);
});
it('should do values on path', function (done) {
this.timeout(10000);
db.values.read(
t.fromIndexes(t.pathIndex('price/amt'))
).result(function (values) {
//console.log(JSON.stringify(values, null, 2));
values.should.have.property('values-response');
values['values-response'].should.have.property('tuple');
values['values-response'].tuple.length.should.equal(5);
values['values-response'].tuple[0].should.have.property('frequency');
values['values-response'].tuple[0].should.have.property('distinct-value');
values['values-response'].tuple[0].frequency.should.equal(1);
done();
}, function (error) {
console.log(JSON.stringify(error, null, 2));
done();
}, done);
});
/*
//Issue with Index settings working on it
//Need to create a filed name : filed_for_valueread and a range index in it on title type string
it('should do values on field', function(done){
this.timeout(10000);
db.values.read(
t.fromIndexes(
t.field('filed_for_valueread')
).
where(
t.word('title', 'bush'))
).result(function (result) {
//console.log(JSON.stringify(result, null, 2));
result.should.have.property('values-response');
done();
}, function(error) {
console.log(JSON.stringify(error, null, 2));
done();
}, done);
}); */
it('should do sum aggregates', function (done) {
this.timeout(10000);
db.values.read(
t.fromIndexes(
t.range('score', 'xs:double')
).
where(
t.word('title', 'bush')
).
aggregates('sum')
).result(function (response) {
//console.log(JSON.stringify(response, null, 4));
var strData = JSON.stringify(response);
strData.should.containEql('"aggregate-result":[{"name":"sum","_value":"149.15"}]');
done();
}, done);
});
it('should do correlation and covariance aggregates', function (done) {
this.timeout(10000);
db.values.read(
t.fromIndexes(
t.range('rate', 'xs:int'),
t.range(t.property('popularity'), t.datatype('int'))
).
where(
t.word('id', '00*', q.termOptions('wildcarded'))
).
aggregates('correlation', 'covariance')
).result(function (response) {
//console.log(JSON.stringify(response, null, 4));
var strData = JSON.stringify(response);
//console.log(strData);
strData.should.containEql('"name":"correlation","_value":"0.263822426505543"');
strData.should.containEql(('"name":"covariance","_value":"0.35"') || ('"name":"covariance","_value":"0.349999999999998"'));
done();
}).catch(error=> done(error));
});
it('should do max, min, sum, average aggregates', function (done) {
this.timeout(10000);
db.values.read(
t.fromIndexes(
t.range(t.property('popularity'), t.datatype('int'))
).
where(
t.word('id', '00*', q.termOptions('wildcarded'))
).
aggregates('max', 'min', 'sum', 'avg')
).result(function (response) {
//console.log(JSON.stringify(response, null, 4));
var strData = JSON.stringify(response);
strData.should.containEql('"aggregate-result":[{"name":"max","_value":"5"},{"name":"min","_value":"3"},{"name":"sum","_value":"22"},{"name":"avg","_value":"4.4"}]');
done();
}, done);
});
it('should delete all documents', function (done) {
dbAdmin.documents.removeAll({
all: true
}).
result(function (response) {
done();
}, done);
});
});