-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(leaf): Add match leaf query function
- Loading branch information
1 parent
955fc71
commit e632a5d
Showing
3 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module.exports = function( property, value, params) { | ||
if( !property || !value) { | ||
return null; | ||
} | ||
|
||
const query = { | ||
match: { | ||
[property]: { | ||
query: value | ||
} | ||
} | ||
}; | ||
|
||
const optional_params = ['boost', | ||
'operator', | ||
'analyzer', | ||
'cutoff_frequency', | ||
'fuzziness', | ||
'max_expansions', | ||
'prefix_length', | ||
'fuzzy_transpositions', | ||
'minimum_should_match', | ||
'zero_terms_query']; | ||
|
||
optional_params.forEach(function(param) { | ||
if (params && params[param]) { | ||
query.match[property][param] = params[param]; | ||
} | ||
}); | ||
|
||
return query; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters