Search with type coercion
Closed this issue · 4 comments
mikecole commented
Am I correct that the properties have to be the same type to match?
For example, given:
var list = [{name:"John",age:25},{name:"Jill",age:30}];
This would match:
{name:"John",age:30}
This would not match:
{name:"John",age:'30'}
deitch commented
Correct @mikecole . Here is an example. See how the matches are performed.
> search = require('searchjs')
{ setDefaults: [Function: setDefaults],
resetDefaults: [Function: resetDefaults],
singleMatch: [Function: singleMatch],
matchArray: [Function: matchArray],
matchObject: [Function: matchObject] }
> ary = [{name:"string",age:"30"},{name:"number",age:30}]
[ { name: 'string', age: '30' }, { name: 'number', age: 30 } ]
> search.matchArray(ary,{age:30})
[ { name: 'number', age: 30 } ]
> search.matchArray(ary,{age:"30"})
[ { name: 'string', age: '30' } ]
mikecole commented
Any way to perform the search without strict equality?
deitch commented
Hmm... how about:
> ary = [{name:"string",age:"30"},{name:"number",age:30}]
> search.matchArray(ary,{terms:[{age:"30"},{age:30}], _join:"OR"})
[ { name: 'string', age: '30' }, { name: 'number', age: 30 } ]
mikecole commented
I can work with this, I think. My search term is a free-form text field so I can check the datatype and if it can parse to int I'll add both terms. Thanks!