Skip to content

Commit

Permalink
Merge pull request #109 from pelias/leaf-queries-and-libs
Browse files Browse the repository at this point in the history
Add new 'leaf' queries
  • Loading branch information
orangejulius authored Sep 11, 2019
2 parents b847841 + 83509c6 commit 7c70ae0
Show file tree
Hide file tree
Showing 16 changed files with 608 additions and 15 deletions.
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ module.exports.layout = {
};

module.exports.view = {
leaf: {
match_all: require('./view/leaf/match_all'),
match_phrase: require('./view/leaf/match_phrase'),
match: require('./view/leaf/match')
},
focus: require('./view/focus'),
focus_only_function: require('./view/focus_only_function'),
popularity: require('./view/popularity'),
Expand Down
34 changes: 34 additions & 0 deletions lib/leaf/match.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const optional_params = [
'boost',
'operator',
'analyzer',
'cutoff_frequency',
'fuzziness',
'max_expansions',
'prefix_length',
'fuzzy_transpositions',
'minimum_should_match',
'zero_terms_query'
];

module.exports = function( property, value, params) {
if( !property || !value) {
return null;
}

const query = {
match: {
[property]: {
query: value
}
}
};

optional_params.forEach(function(param) {
if (params && params[param]) {
query.match[property][param] = params[param];
}
});

return query;
};
23 changes: 23 additions & 0 deletions lib/leaf/match_phrase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module.exports = function( property, value, params ) {
if( !property || !value) {
return null;
}

const query = {
match_phrase: {
[property]: {
query: value
}
}
};

const optional_params = ['boost', 'slop', 'analyzer'];

optional_params.forEach(function(param) {
if (params && params[param]) {
query.match_phrase[property][param] = params[param];
}
});

return query;
};
17 changes: 17 additions & 0 deletions lib/leaf/terms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = function( property, value, parameters ){
if( !property || !value) {
return null;
}

const query = {
terms: {
[property]: value
}
};

if (parameters && parameters.boost) {
query.terms.boost = parameters.boost;
}

return query;
};
139 changes: 139 additions & 0 deletions test/lib/leaf/match.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
const match = require('../../../lib/leaf/match');

module.exports.tests = {};

module.exports.tests.match = function(test, common) {
test('null returned if property missing', function(t) {
const query = match();

t.equal(null, query, 'null query returned');
t.end();
});

test('null returned if value missing', function(t) {
const query = match('theproperty');

t.equal(null, query, 'null query returned');
t.end();
});

test('match query returned with property and value', function(t) {
const query = match('theproperty', 'thevalue');

const expected = {
match: {
theproperty: {
query: 'thevalue'
}
}
};

t.deepEqual(query, expected, 'valid match query');
t.end();
});

test('match query can handle optional boost parameter', function(t) {
const query = match('property', 'value', { boost: 5});

const expected = {
match: {
property: {
query: 'value',
boost: 5
}
}
};

t.deepEqual(query, expected, 'valid match query with boost');
t.end();
});

test('match query can handle optional cutoff_frequency parameter', function(t) {
const query = match('property', 'value', { cutoff_frequency: 0.01});

const expected = {
match: {
property: {
query: 'value',
cutoff_frequency: 0.01
}
}
};

t.deepEqual(query, expected, 'valid match query with cutoff_frequency');
t.end();
});

test('match query can handle optional minimum_should_match parameter', function(t) {
const query = match('property', 'value', { minimum_should_match: '25%'});

const expected = {
match: {
property: {
query: 'value',
minimum_should_match: '25%'
}
}
};

t.deepEqual(query, expected, 'valid match query with minimum_should_match');
t.end();
});

test('match query can handle optional fuzziness parameter', function(t) {
const query = match('property', 'value', { fuzziness: 1});

const expected = {
match: {
property: {
query: 'value',
fuzziness: 1
}
}
};

t.deepEqual(query, expected, 'valid match query with fuzziness');
t.end();
});

test('match query can handle optional operator parameter', function(t) {
const query = match('property', 'value', { operator: 'and'});

const expected = {
match: {
property: {
query: 'value',
operator: 'and'
}
}
};

t.deepEqual(query, expected, 'valid match query with operator');
t.end();
});

test('match query can handle optional analyzer parameter', function(t) {
const query = match('property', 'value', { analyzer: 'standard'});

const expected = {
match: {
property: {
query: 'value',
analyzer: 'standard'
}
}
};

t.deepEqual(query, expected, 'valid match query with analyzer');
t.end();
});
};

module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('lib/leaf/match ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};
91 changes: 91 additions & 0 deletions test/lib/leaf/match_phrase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const match_phrase = require('../../../lib/leaf/match_phrase');

module.exports.tests = {};

module.exports.tests.match_phrase = function(test, common) {
test('null returned if property missing', function(t) {
const query = match_phrase();

t.equal(null, query, 'null query returned');
t.end();
});

test('null returned if value missing', function(t) {
const query = match_phrase('theproperty');

t.equal(null, query, 'null query returned');
t.end();
});

test('match_phrase query returned with property and value', function(t) {
const query = match_phrase('theproperty', 'thevalue');

const expected = {
match_phrase: {
theproperty: {
query: 'thevalue'
}
}
};

t.deepEqual(query, expected, 'valid match_phrase query');
t.end();
});

test('match_phrase query can handle optional boost parameter', function(t) {
const query = match_phrase('property', 'value', { boost: 5});

const expected = {
match_phrase: {
property: {
query: 'value',
boost: 5
}
}
};

t.deepEqual(query, expected, 'valid match_phrase query with boost');
t.end();
});

test('match_phrase query can handle optional slop parameter', function(t) {
const query = match_phrase('property', 'value', { slop: 1});

const expected = {
match_phrase: {
property: {
query: 'value',
slop: 1
}
}
};

t.deepEqual(query, expected, 'valid match_phrase query with slop');
t.end();
});

test('match_phrase query can handle optional analyzer parameter', function(t) {
const query = match_phrase('property', 'value', { analyzer: 1});

const expected = {
match_phrase: {
property: {
query: 'value',
analyzer: 1
}
}
};

t.deepEqual(query, expected, 'valid match_phrase query with analyzer');
t.end();
});
};

module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('lib/leaf/match_phrase ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};
55 changes: 55 additions & 0 deletions test/lib/leaf/terms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const terms = require('../../../lib/leaf/terms');

module.exports.tests = {};

module.exports.tests.terms = function(test, common) {
test('null returned if property missing', function(t) {
const query = terms();

t.equal(null, query, 'null query returned');
t.end();
});

test('null returned if value missing', function(t) {
const query = terms('theproperty');

t.equal(null, query, 'null query returned');
t.end();
});

test('terms query returned with property and value', function(t) {
const query = terms('theproperty', 'thevalue');

const expected = {
terms: {
theproperty: 'thevalue'
}
};

t.deepEqual(query, expected, 'valid terms query');
t.end();
});

test('terms query can handle optional boost parameter', function(t) {
const query = terms('property', 'value', { boost: 5});

const expected = {
terms: {
property: 'value',
boost: 5
}
};

t.deepEqual(query, expected, 'valid terms query with boost');
t.end();
});
};

module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('lib/leaf/terms ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};
7 changes: 6 additions & 1 deletion test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ var tests = [
require('./layout/FilteredBooleanQuery.js'),
require('./layout/StructuredFallbackQuery.js'),
require('./layout/VenuesQuery.js'),
require('./lib/leaf/match.js'),
require('./lib/leaf/match_phrase.js'),
require('./lib/leaf/terms.js'),
require('./lib/Variable.js'),
require('./lib/VariableStore.js'),
require('./view/address.js'),
Expand All @@ -29,7 +32,9 @@ var tests = [
require('./view/population_only_function.js'),
require('./view/sort_distance.js'),
require('./view/sources.js'),
require('./view/boundary_gid.js')
require('./view/boundary_gid.js'),
require('./view/leaf/match.js'),
require('./view/leaf/match_phrase.js')
];

tests.map(function(t) {
Expand Down
Loading

0 comments on commit 7c70ae0

Please sign in to comment.