-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathoptic-fromDocs.js
More file actions
349 lines (327 loc) · 14.7 KB
/
Copy pathoptic-fromDocs.js
File metadata and controls
349 lines (327 loc) · 14.7 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* Copyright (c) 2015-2026 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
*/
'use strict';
const should = require('should');
const marklogic = require('../');
const op = marklogic.planBuilder;
const pbb = require('./plan-builder-base');
var testconfig = require('../etc/test-config.js');
const execPlan = pbb.execPlan;
const getResults = pbb.getResults;
var db = marklogic.createDatabaseClient(testconfig.restWriterConnection);
const Stream = require('stream');
const testlib = require("../etc/test-lib");
let result = new Set();
let uris = [];
let serverConfiguration = {};
describe('optic-update fromDocs tests', function() {
// NOTE: op.fromDocs() with op.columnBuilder() is only supported in MarkLogic 12.1.0 and later.
// Tests in this suite are skipped automatically on earlier versions.
this.timeout(15000);
before(function (done) {
try {
testlib.findServerConfiguration(serverConfiguration);
setTimeout(()=>{done();}, 3000);
} catch(error){
done(error);
}
});
describe('fromDocs', function () {
before(function (done) {
if (serverConfiguration.serverVersion < 12.1) {
this.skip();
return;
}
// Insert test documents
const testDocs = [
{
uri: '/test/fromDocs/artist-incomplete.json',
contentType: 'application/json',
content: {
artist: {
firstName: "Charlie",
lastName: "Parker",
// birthDate is missing - will test default value
instrument: "saxophone"
// genre is missing - will test default value
}
}
},
{
uri: '/test/fromDocs/product-widget.json',
contentType: 'application/json',
content: {
product: {
name: "Widget",
price: 25.50,
quantity: 100
}
}
},
{
uri: '/test/fromDocs/location-seattle.json',
contentType: 'application/json',
collections: ['fromDocs'],
content: {
location: {
city: "Seattle",
point: "47.61, -122.33",
description: "Emerald City"
}
}
},
{
// we already have a geospatial element index for 'point' in wgs84
// in the test-app ml-gradle project. Use that. Use 'point' to indicate location.
uri: '/test/fromDocs/location-portland.json',
contentType: 'application/json',
collections: ['fromDocs'],
content: {
location: {
city: "Portland",
point: "45.52, -122.68",
description: "City of Roses"
}
}
},
{
uri: '/test/fromDocs/location-san-francisco.json',
contentType: 'application/json',
collections: ['fromDocs'],
content: {
location: {
city: "San Francisco",
point: "37.77, -122.42",
description: "City by the Bay"
}
}
},
{
uri: '/test/fromDocs/location-new-york.json',
contentType: 'application/json',
collections: ['fromDocs'],
content: {
location: {
city: "New York",
point: "40.71, -74.01",
description: "The Big Apple"
}
}
},
{
// Bob and Alice are already loaded by ml-gradle test app
uri: '/test/fromDocs/person-donald.json',
contentType: 'application/json',
content: {
person: {
name: "Donald",
summary: "Bad Donald",
embedding: [
-1.1,
-2.2,
-3.3
]
}
}
}
];
let readable = new Stream.Readable({objectMode: true});
testDocs.forEach(doc => {
readable.push(doc);
uris.push(doc.uri);
});
readable.push(null);
db.documents.writeAll(readable, {
onCompletion: () => done()
});
});
after(function (done) {
if (uris.length > 0) {
db.documents.remove(uris)
.result(() => done())
.catch(done);
} else {
done();
}
});
it('fromDocs basic', function (done) {
const plan = op.fromDocs(
op.cts.wordQuery('Coltrane'),
'/musician',
op.columnBuilder()
.addColumn('lastName').xpath('./lastName').type('string')
.addColumn('firstName').xpath('./firstName').type('string')
.addColumn('dob').xpath('./dob').type('string')
.addColumn('instrument').xpath('./instrument').type('string'),
"MyView"
);
execPlan(plan).then(function (response) {
const output = getResults(response);
output.length.should.equal(1);
output[0]['MyView.lastName'].value.should.equal('Coltrane');
output[0]['MyView.firstName'].value.should.equal('John');
output[0]['MyView.dob'].value.should.equal('1926-09-23');
output[0]['MyView.instrument'].value.should.equal('saxophone');
done();
}).catch(done);
});
it('fromDocs with default', function (done) {
const plan = op.fromDocs(
op.cts.wordQuery('Parker'),
'/artist',
op.columnBuilder()
.addColumn('lastName').xpath('./lastName').type('string').collation('http://marklogic.com/collation/')
.addColumn('firstName').xpath('./firstName').type('string').collation('http://marklogic.com/collation/')
.addColumn('birthDate').xpath('./birthDate').type('string').default('Unknown')
.addColumn('instrument').xpath('./instrument').type('string')
.addColumn('genre').xpath('./genre').type('string').default('Jazz'),
"ArtistView"
);
execPlan(plan).then(function (response) {
const output = getResults(response);
output.length.should.equal(1);
output[0]['ArtistView.lastName'].value.should.equal('Parker');
output[0]['ArtistView.firstName'].value.should.equal('Charlie');
output[0]['ArtistView.birthDate'].value.should.equal('Unknown');
output[0]['ArtistView.instrument'].value.should.equal('saxophone');
output[0]['ArtistView.genre'].value.should.equal('Jazz');
done();
}).catch(done);
});
it('fromDocs with expr', function (done) {
const plan = op.fromDocs(
op.cts.wordQuery('Widget'),
'/product',
op.columnBuilder()
.addColumn('name').xpath('./name').type('string')
.addColumn('price').xpath('./price').type('decimal')
.addColumn('quantity').xpath('./quantity').type('integer')
.addColumn('twoToTheThirdPower').nullable(true)
.expr(op.math.pow(2, 3))
.type('integer'),
"ProductView"
);
execPlan(plan).then(function (response) {
const output = getResults(response);
output.length.should.equal(1);
output[0]['ProductView.name'].value.should.equal('Widget');
output[0]['ProductView.price'].value.should.equal(25.50);
output[0]['ProductView.quantity'].value.should.equal(100);
output[0]['ProductView.twoToTheThirdPower'].value.should.equal(8);
done();
}).catch(done);
});
it('fromDocs with op.context() and op.xpath()', function (done) {
const plan = op.fromDocs(
op.cts.wordQuery('Widget'),
'/product',
op.columnBuilder()
.addColumn('name').xpath('./name').type('string')
.addColumn('quantity').xpath('./quantity').type('integer')
.addColumn('price').xpath('./price').type('decimal')
.addColumn('totalCost').nullable(true)
.expr(op.multiply(
op.xs.decimal(op.xpath(op.context(), './price')),
op.xs.integer(op.xpath(op.context(), './quantity'))
)
)
.type('decimal'),
"ProductView"
);
execPlan(plan).then(function (response) {
const output = getResults(response);
output.length.should.equal(1);
output[0]['ProductView.name'].value.should.equal('Widget');
output[0]['ProductView.price'].value.should.equal(25.50);
output[0]['ProductView.quantity'].value.should.equal(100);
output[0]['ProductView.totalCost'].value.should.equal(2550);
done();
}).catch(done);
});
it('fromDocs with geospatial query', function (done) {
const portlandPoint = op.cts.point(45.52, -122.68);
const searchRadius = 650; // miles
// geospatial element index is defined for 'point' in wgs84
const plan = op.fromDocs(
op.cts.collectionQuery('fromDocs'),
'/location',
op.columnBuilder()
.addColumn('city').xpath('./city').type('string')
.addColumn('location-wgs84').xpath('./point').type('point').coordinateSystem('wgs84')
.addColumn('description').xpath('./description').type('string'),
"LocationView"
)
.where(
op.cts.jsonPropertyGeospatialQuery
(
'point',
op.cts.circle(searchRadius, portlandPoint),
['coordinate-system=wgs84']
)
);
execPlan(plan).then(function (response) {
const output = getResults(response);
output.length.should.be.equal(3);
const cities = output.map(row => row['LocationView.city'].value);
cities.should.containEql('Portland');
cities.should.containEql('Seattle');
cities.should.containEql('San Francisco');
cities.should.not.containEql('New York');
done();
}).catch(done);
});
it('fromDocs with vector and dimension', function (done) {
const testVector = op.vec.vector([1,2,3]);
const plan = op.fromDocs(
op.cts.wordQuery('*'),
'/person',
op.columnBuilder()
.addColumn('name').xpath('./name').type('string')
.addColumn('summary').xpath('./summary').type('string')
.addColumn('embedding').xpath('vec:vector(./embedding)').type('vector').dimension(3)
.addColumn('cosineDistance').nullable(true).expr(
op.vec.cosineDistance
(
op.vec.vector(testVector),
op.vec.vector(op.xpath(op.context(), './embedding'))
)
).type('double')
.addColumn('euclideanDistance').nullable(true).expr(
op.math.trunc(
op.vec.euclideanDistance
(
op.vec.vector(testVector),
op.vec.vector(op.xpath(op.context(), './embedding'))
)
, 4)
).type('double')
,
"PersonView"
).where(
op.lt(
op.vec.cosineDistance
(
op.vec.vector(testVector),
op.viewCol('PersonView', 'embedding')
)
,
op.xs.double(0.5)
)
)
.orderBy(op.viewCol('PersonView', 'euclideanDistance'));
execPlan(plan).then(function (response) {
const output = getResults(response);
output.length.should.be.equal(2);
const distances = output.map(row => row['PersonView.euclideanDistance'].value);
distances[0].should.be.approximately(0.3741, 0.0001); // Alice is .3741 from testVector
distances[1].should.be.approximately(12.1466, 0.0001); // Bob is 12.1466 from testVector
const names = output.map(row => row['PersonView.name'].value);
names.should.containEql('Alice');
names.should.containEql('Bob');
names.should.not.containEql('Donald');
done();
}).catch(done);
});
});
});