pelias-query
Advanced tools
Comparing version 8.4.0 to 8.5.0
@@ -31,2 +31,3 @@ // This query is useful for specifying all the combinations of inputs starting | ||
this._score = []; | ||
this._filter = []; | ||
} | ||
@@ -39,2 +40,7 @@ | ||
Layout.prototype.filter = function( view ){ | ||
this._filter.push( view ); | ||
return this; | ||
}; | ||
function addPrimary(value, layer, fields, likely_to_have_abbreviation) { | ||
@@ -376,2 +382,29 @@ // base primary query should match on layer and one of the admin fields via | ||
// handle scoring views under 'query' section (both 'must' & 'should') | ||
if( this._score.length ){ | ||
this._score.forEach( function( condition ){ | ||
var view = condition[0], operator = condition[1]; | ||
var rendered = view( vs ); | ||
if( rendered ){ | ||
if( !q.query.bool.hasOwnProperty( operator ) ){ | ||
q.query.bool[ operator ] = []; | ||
} | ||
q.query.bool[ operator ].push( rendered ); | ||
} | ||
}); | ||
} | ||
// handle filter views under 'filter' section (only 'must' is allowed here) | ||
if( this._filter.length ){ | ||
this._filter.forEach( function( view ){ | ||
var rendered = view( vs ); | ||
if( rendered ){ | ||
if( !q.query.bool.hasOwnProperty( 'filter' ) ){ | ||
q.query.bool.filter = []; | ||
} | ||
q.query.bool.filter.push( rendered ); | ||
} | ||
}); | ||
} | ||
return q; | ||
@@ -378,0 +411,0 @@ }; |
@@ -43,2 +43,3 @@ // This query is useful for querying a value across a number of different | ||
this._score = []; | ||
this._filter = []; | ||
} | ||
@@ -51,2 +52,7 @@ | ||
Layout.prototype.filter = function( view ){ | ||
this._filter.push( view ); | ||
return this; | ||
}; | ||
function addCoarseLayer(layer, coarse_value, fields) { | ||
@@ -106,2 +112,29 @@ var o = { | ||
// handle scoring views under 'query' section (both 'must' & 'should') | ||
if( this._score.length ){ | ||
this._score.forEach( function( condition ){ | ||
var view = condition[0], operator = condition[1]; | ||
var rendered = view( vs ); | ||
if( rendered ){ | ||
if( !q.query.bool.hasOwnProperty( operator ) ){ | ||
q.query.bool[ operator ] = []; | ||
} | ||
q.query.bool[ operator ].push( rendered ); | ||
} | ||
}); | ||
} | ||
// handle filter views under 'filter' section (only 'must' is allowed here) | ||
if( this._filter.length ){ | ||
this._filter.forEach( function( view ){ | ||
var rendered = view( vs ); | ||
if( rendered ){ | ||
if( !q.query.bool.hasOwnProperty( 'filter' ) ){ | ||
q.query.bool.filter = []; | ||
} | ||
q.query.bool.filter.push( rendered ); | ||
} | ||
}); | ||
} | ||
return q; | ||
@@ -108,0 +141,0 @@ }; |
{ | ||
"name": "pelias-query", | ||
"version": "8.4.0", | ||
"version": "8.5.0", | ||
"description": "An Elasticsearch query builder for Pelias", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -126,2 +126,208 @@ var FallbackQuery = require('../../layout/FallbackQuery'); | ||
module.exports.tests.scores = function(test, common) { | ||
test('score with operator specified should be honored and in order', function(t) { | ||
var score_view1 = function(vs) { | ||
console.assert(vs !== null); | ||
return { 'score field 1': 'score value 1' }; | ||
}; | ||
var score_view2 = function(vs) { | ||
console.assert(vs !== null); | ||
return { 'score field 2': 'score value 2' }; | ||
}; | ||
var score_view3 = function(vs) { | ||
console.assert(vs !== null); | ||
return { 'score field 3': 'score value 3' }; | ||
}; | ||
var score_view4 = function(vs) { | ||
console.assert(vs !== null); | ||
return { 'score field 4': 'score value 4' }; | ||
}; | ||
var query = new FallbackQuery(); | ||
query.score(score_view1, 'must'); | ||
query.score(score_view2, 'should'); | ||
query.score(score_view3, 'must'); | ||
query.score(score_view4, 'should'); | ||
var vs = new VariableStore(); | ||
vs.var('size', 'size value'); | ||
vs.var('track_scores', 'track_scores value'); | ||
var actual = query.render(vs); | ||
var expected = { | ||
query: { | ||
bool: { | ||
should: [ | ||
{ 'score field 2': 'score value 2'}, | ||
{ 'score field 4': 'score value 4'} | ||
], | ||
must: [ | ||
{ 'score field 1': 'score value 1'}, | ||
{ 'score field 3': 'score value 3'} | ||
] | ||
} | ||
}, | ||
size: { $: 'size value' }, | ||
track_scores: { $: 'track_scores value' } | ||
}; | ||
t.deepEquals(actual, expected); | ||
t.end(); | ||
}); | ||
test('score without operator specified should be added as \'should\'', function(t) { | ||
var score_view = function(vs) { | ||
console.assert(vs !== null); | ||
return { 'score field': 'score value' }; | ||
}; | ||
var query = new FallbackQuery(); | ||
query.score(score_view); | ||
var vs = new VariableStore(); | ||
vs.var('size', 'size value'); | ||
vs.var('track_scores', 'track_scores value'); | ||
var actual = query.render(vs); | ||
var expected = { | ||
query: { | ||
bool: { | ||
should: [ | ||
{ 'score field': 'score value'} | ||
] | ||
} | ||
}, | ||
size: { $: 'size value' }, | ||
track_scores: { $: 'track_scores value' } | ||
}; | ||
t.deepEquals(actual, expected); | ||
t.end(); | ||
}); | ||
test('score with non-must or -should operator specified should be added as \'should\'', function(t) { | ||
var score_view = function(vs) { | ||
console.assert(vs !== null); | ||
return { 'score field': 'score value' }; | ||
}; | ||
var query = new FallbackQuery(); | ||
query.score(score_view, 'non must or should value'); | ||
var vs = new VariableStore(); | ||
vs.var('size', 'size value'); | ||
vs.var('track_scores', 'track_scores value'); | ||
var actual = query.render(vs); | ||
var expected = { | ||
query: { | ||
bool: { | ||
should: [ | ||
{ 'score field': 'score value'} | ||
] | ||
} | ||
}, | ||
size: { $: 'size value' }, | ||
track_scores: { $: 'track_scores value' } | ||
}; | ||
t.deepEquals(actual, expected); | ||
t.end(); | ||
}); | ||
test('scores rendering to falsy values should not be added', function(t) { | ||
var score_views_called = 0; | ||
var query = new FallbackQuery(); | ||
[ | ||
{ 'score field 1': 'score value 1' }, | ||
false, '', 0, null, undefined, NaN, | ||
{ 'score field 2': 'score value 2' }, | ||
].forEach(function(value) { | ||
query.score(function(vs) { | ||
// assert that `vs` was actually passed | ||
console.assert(vs !== null); | ||
// make a note that the view was actually called | ||
score_views_called++; | ||
return value; | ||
}); | ||
}); | ||
var vs = new VariableStore(); | ||
vs.var('size', 'size value'); | ||
vs.var('track_scores', 'track_scores value'); | ||
var actual = query.render(vs); | ||
var expected = { | ||
query: { | ||
bool: { | ||
should: [ | ||
{ 'score field 1': 'score value 1'}, | ||
{ 'score field 2': 'score value 2'} | ||
] | ||
} | ||
}, | ||
size: { $: 'size value' }, | ||
track_scores: { $: 'track_scores value' } | ||
}; | ||
t.deepEquals(actual, expected); | ||
t.equals(score_views_called, 8); | ||
t.end(); | ||
}); | ||
}; | ||
module.exports.tests.filter = function(test, common) { | ||
test('all filter views returning truthy values should be added in order to sort', function(t) { | ||
// the views assert that the VariableStore was passed, otherwise there's no | ||
// guarantee that it was actually passed | ||
var filter_views_called = 0; | ||
var query = new FallbackQuery(); | ||
[ | ||
{ 'filter field 1': 'filter value 1' }, | ||
false, '', 0, null, undefined, NaN, | ||
{ 'filter field 2': 'filter value 2' }, | ||
].forEach(function(value) { | ||
query.filter(function(vs) { | ||
// assert that `vs` was actually passed | ||
console.assert(vs !== null); | ||
// make a note that the view was actually called | ||
filter_views_called++; | ||
return value; | ||
}); | ||
}); | ||
var vs = new VariableStore(); | ||
vs.var('size', 'size value'); | ||
vs.var('track_scores', 'track_scores value'); | ||
var actual = query.render(vs); | ||
var expected_filter = [ | ||
{ 'filter field 1': 'filter value 1'}, | ||
{ 'filter field 2': 'filter value 2'} | ||
]; | ||
t.equals(filter_views_called, 8); | ||
t.deepEquals(actual.query.bool.filter, expected_filter); | ||
t.end(); | ||
}); | ||
}; | ||
module.exports.all = function (tape, common) { | ||
@@ -128,0 +334,0 @@ function test(name, testFunction) { |
@@ -281,2 +281,184 @@ var GeodisambiguationQuery = require('../../layout/GeodisambiguationQuery'); | ||
module.exports.tests.scores = function(test, common) { | ||
test('score with operator specified should be honored and in order', function(t) { | ||
var score_view1 = function(vs) { | ||
console.assert(vs !== null); | ||
return { 'score field 1': 'score value 1' }; | ||
}; | ||
var score_view2 = function(vs) { | ||
console.assert(vs !== null); | ||
return { 'score field 2': 'score value 2' }; | ||
}; | ||
var score_view3 = function(vs) { | ||
console.assert(vs !== null); | ||
return { 'score field 3': 'score value 3' }; | ||
}; | ||
var score_view4 = function(vs) { | ||
console.assert(vs !== null); | ||
return { 'score field 4': 'score value 4' }; | ||
}; | ||
var query = new GeodisambiguationQuery(); | ||
query.score(score_view1, 'must'); | ||
query.score(score_view2, 'should'); | ||
query.score(score_view3, 'must'); | ||
query.score(score_view4, 'should'); | ||
var vs = new VariableStore(); | ||
vs.var('size', 'size value'); | ||
vs.var('track_scores', 'track_scores value'); | ||
var actual = query.render(vs); | ||
var actual_shoulds = actual.query.bool.should; | ||
var actual_musts = actual.query.bool.must; | ||
var expected_shoulds = [ | ||
{ 'score field 2': 'score value 2'}, | ||
{ 'score field 4': 'score value 4'} | ||
]; | ||
var expected_musts = [ | ||
{ 'score field 1': 'score value 1'}, | ||
{ 'score field 3': 'score value 3'} | ||
]; | ||
t.deepEquals(actual_shoulds.slice(actual_shoulds.length-2), expected_shoulds); | ||
t.deepEquals(actual_musts.slice(actual_musts.length-2), expected_musts); | ||
t.end(); | ||
}); | ||
test('score without operator specified should be added as \'should\'', function(t) { | ||
var score_view = function(vs) { | ||
console.assert(vs !== null); | ||
return { 'score field': 'score value' }; | ||
}; | ||
var query = new GeodisambiguationQuery(); | ||
query.score(score_view); | ||
var vs = new VariableStore(); | ||
vs.var('size', 'size value'); | ||
vs.var('track_scores', 'track_scores value'); | ||
var actual = query.render(vs); | ||
var actual_shoulds = actual.query.bool.should; | ||
var expected_shoulds = [ | ||
{ 'score field': 'score value'} | ||
]; | ||
t.deepEquals(actual_shoulds.slice(actual_shoulds.length-1), expected_shoulds); | ||
t.end(); | ||
}); | ||
test('score with non-must or -should operator specified should be added as \'should\'', function(t) { | ||
var score_view = function(vs) { | ||
console.assert(vs !== null); | ||
return { 'score field': 'score value' }; | ||
}; | ||
var query = new GeodisambiguationQuery(); | ||
query.score(score_view, 'non must or should value'); | ||
var vs = new VariableStore(); | ||
vs.var('size', 'size value'); | ||
vs.var('track_scores', 'track_scores value'); | ||
var actual = query.render(vs); | ||
var actual_shoulds = actual.query.bool.should; | ||
var expected_shoulds = [ | ||
{ 'score field': 'score value'} | ||
]; | ||
t.deepEquals(actual_shoulds.slice(actual_shoulds.length-1), expected_shoulds); | ||
t.end(); | ||
}); | ||
test('scores rendering to falsy values should not be added', function(t) { | ||
var score_views_called = 0; | ||
var query = new GeodisambiguationQuery(); | ||
[ | ||
{ 'score field 1': 'score value 1' }, | ||
false, '', 0, null, undefined, NaN, | ||
{ 'score field 2': 'score value 2' }, | ||
].forEach(function(value) { | ||
query.score(function(vs) { | ||
// assert that `vs` was actually passed | ||
console.assert(vs !== null); | ||
// make a note that the view was actually called | ||
score_views_called++; | ||
return value; | ||
}); | ||
}); | ||
var vs = new VariableStore(); | ||
vs.var('size', 'size value'); | ||
vs.var('track_scores', 'track_scores value'); | ||
var actual = query.render(vs); | ||
var actual_shoulds = actual.query.bool.should; | ||
var expected_shoulds = [ | ||
{ 'score field 1': 'score value 1'}, | ||
{ 'score field 2': 'score value 2'} | ||
]; | ||
t.deepEquals(actual_shoulds.slice(actual_shoulds.length-2), expected_shoulds); | ||
t.equals(score_views_called, 8); | ||
t.end(); | ||
}); | ||
}; | ||
module.exports.tests.filter = function(test, common) { | ||
test('all filter views returning truthy values should be added in order to sort', function(t) { | ||
// the views assert that the VariableStore was passed, otherwise there's no | ||
// guarantee that it was actually passed | ||
var filter_views_called = 0; | ||
var query = new GeodisambiguationQuery(); | ||
[ | ||
{ 'filter field 1': 'filter value 1' }, | ||
false, '', 0, null, undefined, NaN, | ||
{ 'filter field 2': 'filter value 2' }, | ||
].forEach(function(value) { | ||
query.filter(function(vs) { | ||
// assert that `vs` was actually passed | ||
console.assert(vs !== null); | ||
// make a note that the view was actually called | ||
filter_views_called++; | ||
return value; | ||
}); | ||
}); | ||
var vs = new VariableStore(); | ||
vs.var('size', 'size value'); | ||
vs.var('track_scores', 'track_scores value'); | ||
var actual = query.render(vs); | ||
var expected_filter = [ | ||
{ 'filter field 1': 'filter value 1'}, | ||
{ 'filter field 2': 'filter value 2'} | ||
]; | ||
t.equals(filter_views_called, 8); | ||
t.deepEquals(actual.query.bool.filter, expected_filter); | ||
t.end(); | ||
}); | ||
}; | ||
module.exports.all = function (tape, common) { | ||
@@ -283,0 +465,0 @@ function test(name, testFunction) { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
169618
4751