Skip to content

Commit

Permalink
feat(match_phrase): Add match_phrase leaf query function
Browse files Browse the repository at this point in the history
  • Loading branch information
orangejulius committed Sep 10, 2019
1 parent bf0c689 commit 955fc71
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
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;
};
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);
}
};
1 change: 1 addition & 0 deletions test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var tests = [
require('./layout/FilteredBooleanQuery.js'),
require('./layout/StructuredFallbackQuery.js'),
require('./layout/VenuesQuery.js'),
require('./lib/leaf/match_phrase.js'),
require('./lib/leaf/terms.js'),
require('./lib/Variable.js'),
require('./lib/VariableStore.js'),
Expand Down

0 comments on commit 955fc71

Please sign in to comment.