Skip to content

Commit 97ee2eb

Browse files
author
ehennum
committed
#554 range and geo weights in query builder
1 parent af3c021 commit 97ee2eb

File tree

2 files changed

+90
-11
lines changed

2 files changed

+90
-11
lines changed

lib/query-builder.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,6 +1532,9 @@ function geoPath() {
15321532
* @memberof queryBuilder#
15331533
* @param {queryBuilder.GeoLocation} location - the JSON property or XML element
15341534
* representing the geospatial locations
1535+
* @param {queryBuilder.WeightParam} [weight] - a weight returned
1536+
* by {@link queryBuilder#weight} to increase or decrease the score
1537+
* of the query relative to other queries in the complete search
15351538
* @param {queryBuilder.FragmentScopeParam} [fragmentScope] - whether the query
15361539
* applies to document content (the default) or document metadata properties
15371540
* as returned by the {@link queryBuilder#fragmentScope} function
@@ -1562,6 +1565,9 @@ function geospatial() {
15621565
* representing the geospatial locations
15631566
* @param {string} operator - a spatial operator:
15641567
* disjoint|contains|covers|intersects|within|covered-by|overlaps
1568+
* @param {queryBuilder.WeightParam} [weight] - a weight returned
1569+
* by {@link queryBuilder#weight} to increase or decrease the score
1570+
* of the query relative to other queries in the complete search
15651571
* @param {queryBuilder.FragmentScopeParam} [fragmentScope] - whether the query
15661572
* applies to document content (the default) or document metadata properties
15671573
* as returned by the {@link queryBuilder#fragmentScope} function
@@ -1598,6 +1604,7 @@ function geospatialImpl(hasOperator, args) {
15981604
var variant = null;
15991605
var constraintName = null;
16001606
var suggestOptions = null;
1607+
var seekingWeight = true;
16011608
var seekingFragmentScope = true;
16021609
var seekingGeoOption = true;
16031610
var seekingRegion = true;
@@ -1621,6 +1628,11 @@ function geospatialImpl(hasOperator, args) {
16211628
seekingGeoOption = false;
16221629
continue;
16231630
}
1631+
if (seekingWeight && arg instanceof WeightDef) {
1632+
query.weight = arg.weight;
1633+
seekingWeight = false;
1634+
continue;
1635+
}
16241636
if (seekingFragmentScope && arg instanceof FragmentScopeDef) {
16251637
query['fragment-scope'] = arg['fragment-scope'];
16261638
seekingFragmentScope = false;
@@ -2816,6 +2828,9 @@ function TemporalOptionsDef(options) {
28162828
* @param {queryBuilder.DefaultBindingParam} [defaultBinding] - a binding
28172829
* (returned by the {@link queryBuilder#bindDefault} function) for parsing
28182830
* the comparison operator and value from untagged values in a query string
2831+
* @param {queryBuilder.WeightParam} [weight] - a weight returned
2832+
* by {@link queryBuilder#weight} to increase or decrease the score
2833+
* of the query relative to other queries in the complete search
28192834
* @param {queryBuilder.FragmentScopeParam} [fragmentScope] - whether the query
28202835
* applies to document content (the default) or document metadata properties
28212836
* as returned by the {@link queryBuilder#fragmentScope} function
@@ -2829,6 +2844,7 @@ function range() {
28292844
var index = null;
28302845
var datatype = null;
28312846
var collation = null;
2847+
var weight = null;
28322848
var fragmentScope = null;
28332849
var rangeOptions = null;
28342850
var values = null;
@@ -2848,6 +2864,10 @@ function range() {
28482864
collation = arg.collation;
28492865
continue;
28502866
}
2867+
if (weight === null && arg instanceof WeightDef) {
2868+
weight = arg.weight;
2869+
continue;
2870+
}
28512871
if (fragmentScope === null && arg instanceof FragmentScopeDef) {
28522872
fragmentScope = arg['fragment-scope'];
28532873
continue;
@@ -2900,7 +2920,7 @@ function range() {
29002920
}
29012921

29022922
var rangeDef = new RangeDef(
2903-
index, datatype, collation, operator, values, rangeOptions, fragmentScope
2923+
index, datatype, collation, operator, values, weight, rangeOptions, fragmentScope
29042924
);
29052925

29062926
if (values !== null) {
@@ -2958,11 +2978,11 @@ function RangeQueryDef(rangeDef) {
29582978
util.inherits(RangeQueryDef, QueryDef);
29592979
/** @ignore */
29602980
function RangeDef(
2961-
index, datatype, collation, operator, values, rangeOptions, fragmentScope
2981+
index, datatype, collation, operator, values, weight, rangeOptions, fragmentScope
29622982
) {
29632983
if (!(this instanceof RangeDef)) {
29642984
return new RangeDef(
2965-
index, datatype, collation, operator, values, rangeOptions, fragmentScope
2985+
index, datatype, collation, operator, values, weight, rangeOptions, fragmentScope
29662986
);
29672987
}
29682988

@@ -2990,6 +3010,13 @@ function RangeDef(
29903010
if (hasOperator) {
29913011
this['range-operator'] = operator;
29923012
}
3013+
if (weight != null) {
3014+
if (typeof weight === 'number' || weight instanceof Number) {
3015+
this.weight = weight;
3016+
} else {
3017+
throw new Error('invalid weight: '+mlutil.identify(weight, true));
3018+
}
3019+
}
29933020
if (rangeOptions != null) {
29943021
this['range-option'] = rangeOptions;
29953022
}

test-basic/query-builder.js

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,11 @@ describe('query-builder', function() {
394394
assert.deepEqual(
395395
q.geospatial(q.geoAttributePair('parent', 'latitude', 'longitude'),
396396
q.latlon(1.1, 2.2),
397-
q.fragmentScope('documents'), q.geoOptions('boundaries-included')),
397+
q.weight(2), q.fragmentScope('documents'), q.geoOptions('boundaries-included')),
398398
{'geo-attr-pair-query':{parent:{name:'parent'},
399399
lat:{name:'latitude'}, lon:{name:'longitude'},
400400
point:[{latitude:1.1, longitude:2.2}],
401+
weight: 2,
401402
'fragment-scope': 'documents',
402403
'geo-option':['boundaries-included']}}
403404
);
@@ -451,11 +452,12 @@ describe('query-builder', function() {
451452
);
452453
assert.deepEqual(
453454
q.geospatial(q.geoElement('parent', 'element'),
454-
q.latlon(1.1, 2.2), q.fragmentScope('documents'),
455-
q.geoOptions('boundaries-included')),
455+
q.latlon(1.1, 2.2), q.weight(2),
456+
q.fragmentScope('documents'), q.geoOptions('boundaries-included')),
456457
{'geo-elem-query':{parent:{name:'parent'},
457458
element:{name:'element'},
458459
point:[{latitude:1.1, longitude:2.2}],
460+
weight: 2,
459461
'fragment-scope': 'documents',
460462
'geo-option':['boundaries-included']}}
461463
);
@@ -491,10 +493,11 @@ describe('query-builder', function() {
491493
);
492494
assert.deepEqual(
493495
q.geospatial(q.geoElementPair('parent', 'latitude', 'longitude'), q.latlon(1.1, 2.2),
494-
q.fragmentScope('documents'), q.geoOptions('boundaries-included')),
496+
q.weight(2), q.fragmentScope('documents'), q.geoOptions('boundaries-included')),
495497
{'geo-elem-pair-query':{parent:{name:'parent'},
496498
lat:{name:'latitude'}, lon:{name:'longitude'},
497499
point:[{latitude:1.1, longitude:2.2}],
500+
weight: 2,
498501
'fragment-scope': 'documents',
499502
'geo-option':['boundaries-included']}}
500503
);
@@ -533,9 +536,10 @@ describe('query-builder', function() {
533536
);
534537
assert.deepEqual(
535538
q.geospatial(q.geoPath(q.pathIndex('foo', {bar: 'baz'})), q.point(1.1, 2.2),
536-
q.fragmentScope('documents'), q.geoOptions('boundaries-included')),
539+
q.weight(2), q.fragmentScope('documents'), q.geoOptions('boundaries-included')),
537540
{'geo-path-query':{'path-index':{text: 'foo', namespaces: {bar: 'baz'}},
538541
point:[{latitude:1.1, longitude:2.2}],
542+
weight: 2,
539543
'fragment-scope': 'documents',
540544
'geo-option':['boundaries-included']}}
541545
);
@@ -585,11 +589,12 @@ describe('query-builder', function() {
585589
);
586590
assert.deepEqual(
587591
q.geospatial(q.geoProperty('parent', 'property1'),
588-
q.latlon(1.1, 2.2), q.fragmentScope('documents'),
589-
q.geoOptions('boundaries-included')),
592+
q.latlon(1.1, 2.2), q.weight(2),
593+
q.fragmentScope('documents'), q.geoOptions('boundaries-included')),
590594
{'geo-json-property-query':{'parent-property':'parent',
591595
'json-property':'property1',
592596
point:[{latitude:1.1, longitude:2.2}],
597+
weight: 2,
593598
'fragment-scope': 'documents',
594599
'geo-option':['boundaries-included']}}
595600
);
@@ -621,11 +626,12 @@ describe('query-builder', function() {
621626
);
622627
assert.deepEqual(
623628
q.geospatial(q.geoPropertyPair('parent', 'latitude', 'longitude'),
624-
q.latlon(1.1, 2.2),
629+
q.latlon(1.1, 2.2), q.weight(2),
625630
q.fragmentScope('documents'), q.geoOptions('boundaries-included')),
626631
{'geo-json-property-pair-query':{'parent-property':'parent',
627632
'lat-property':'latitude', 'lon-property':'longitude',
628633
point:[{latitude:1.1, longitude:2.2}],
634+
weight: 2,
629635
'fragment-scope': 'documents',
630636
'geo-option':['boundaries-included']}}
631637
);
@@ -1154,6 +1160,15 @@ describe('query-builder', function() {
11541160
'range-option':['cached']
11551161
}}
11561162
);
1163+
assert.deepEqual(
1164+
q.range('foo', 1, q.weight(2)),
1165+
{'range-query':{
1166+
'json-property': 'foo',
1167+
value: [1],
1168+
'range-operator': 'EQ',
1169+
weight: 2
1170+
}}
1171+
);
11571172
});
11581173

11591174
it('should create range options', function(){
@@ -1493,6 +1508,15 @@ describe('query-builder', function() {
14931508
'fragment-scope': 'properties'},
14941509
name:'constraint1'}
14951510
);
1511+
assert.deepEqual(
1512+
q.geospatial(q.geoAttributePair('parent', 'latitude', 'longitude'),
1513+
q.bind('constraint1'),
1514+
q.weight(2)),
1515+
{'geo-attr-pair':{parent:{name:'parent'},
1516+
lat:{name:'latitude'}, lon:{name:'longitude'},
1517+
weight: 2},
1518+
name:'constraint1'}
1519+
);
14961520
assert.deepEqual(
14971521
q.geospatial(q.geoElement('parent', 'element'),
14981522
q.bind('constraint1')),
@@ -1516,6 +1540,14 @@ describe('query-builder', function() {
15161540
'fragment-scope': 'properties'},
15171541
name:'constraint1'}
15181542
);
1543+
assert.deepEqual(
1544+
q.geospatial(q.geoElement('parent', 'element'),
1545+
q.bind('constraint1'), q.weight(2)),
1546+
{'geo-elem':{parent:{name:'parent'},
1547+
element:{name:'element'},
1548+
weight: 2},
1549+
name:'constraint1'}
1550+
);
15191551
assert.deepEqual(
15201552
q.geospatial(q.geoElementPair('parent', 'latitude', 'longitude'),
15211553
q.bind('constraint1')),
@@ -1539,6 +1571,14 @@ describe('query-builder', function() {
15391571
'fragment-scope': 'properties'},
15401572
name:'constraint1'}
15411573
);
1574+
assert.deepEqual(
1575+
q.geospatial(q.geoElementPair('parent', 'latitude', 'longitude'),
1576+
q.bind('constraint1'), q.weight(2)),
1577+
{'geo-elem-pair':{parent:{name:'parent'},
1578+
lat:{name:'latitude'}, lon:{name:'longitude'},
1579+
weight: 2},
1580+
name:'constraint1'}
1581+
);
15421582
assert.deepEqual(
15431583
q.geospatial(q.geoPath('foo'), q.bind('constraint1')),
15441584
{'geo-path':{'path-index':{text: 'foo', namespaces: ''}},
@@ -1558,6 +1598,13 @@ describe('query-builder', function() {
15581598
'fragment-scope': 'properties'},
15591599
name:'constraint1'}
15601600
);
1601+
assert.deepEqual(
1602+
q.geospatial(q.geoPath('foo'),
1603+
q.bind('constraint1'), q.weight(2)),
1604+
{'geo-path':{'path-index':{text: 'foo', namespaces: ''},
1605+
weight: 2},
1606+
name:'constraint1'}
1607+
);
15611608
assert.deepEqual(
15621609
q.range('key1', q.bind('constraint1')),
15631610
{range:{'json-property': 'key1', facet: false},
@@ -1573,6 +1620,11 @@ describe('query-builder', function() {
15731620
{range:{'json-property': 'key1', 'fragment-scope': 'properties', facet: false},
15741621
name:'constraint1'}
15751622
);
1623+
assert.deepEqual(
1624+
q.range('key1', q.bind('constraint1'), q.weight(2)),
1625+
{range:{'json-property': 'key1', weight: 2, facet: false},
1626+
name:'constraint1'}
1627+
);
15761628
assert.deepEqual(
15771629
q.value('key1', q.bind('constraint1')),
15781630
{value:{'json-property': 'key1'},

0 commit comments

Comments
 (0)