Commit 9485382a authored by preetwinder's avatar preetwinder

Add full text search as default if key is empty and add tests

parent cb009081
......@@ -90,7 +90,7 @@ case 5: case 8: case 11: case 14: case 16:
this.$ = $$[$0];
break;
case 6:
this.$ = mkComplexQuery('OR', [$$[$0-1], $$[$0]]);
this.$ = mkComplexQuery('AND', [$$[$0-1], $$[$0]]);
break;
case 7:
this.$ = mkComplexQuery('OR', [$$[$0-2], $$[$0]]);
......
......@@ -45,7 +45,7 @@ end
search_text
: and_expression { $$ = $1; }
| and_expression search_text { $$ = mkComplexQuery('OR', [$1, $2]); }
| and_expression search_text { $$ = mkComplexQuery('AND', [$1, $2]); }
| and_expression OR search_text { $$ = mkComplexQuery('OR', [$1, $3]); }
;
......
......@@ -717,7 +717,8 @@
matchMethod = null,
operator = this.operator,
value = null,
key = this.key;
key = this.key,
k;
if (!(regexp_comparaison.test(operator))) {
// `operator` is not correct, we have to change it to "like" or "="
......@@ -736,6 +737,22 @@
key = this._key_schema.key_set[key];
}
// match with all the fields if key is empty
if (key === '') {
matchMethod = this.like;
value = '%' + this.value + '%';
for (k in item) {
if (item.hasOwnProperty(k)) {
if (k !== '__id') {
if (matchMethod(item[k], value) === true) {
return true;
}
}
}
}
return false;
}
if (typeof key === 'object') {
checkKey(key);
object_value = item[key.read_from];
......
......@@ -154,7 +154,8 @@
.exec(doc_list);
})
.then(function (doc_list) {
deepEqual(doc_list, [], 'No document should be kept');
deepEqual(doc_list, [{"identifier": "测试一", "title": "标题"}
], 'Full text query matched document should be returned');
})
.fail(function (error) {
ok(false, error);
......@@ -357,4 +358,103 @@
], 'Documents with newlines are matched');
}).always(start);
});
test('Default operator for complex query', function () {
var doc_list = [
{"identifier": "a", "value": "test1", "time": "2016"},
{"identifier": "a", "value": "test", "time": "2016"},
{"identifier": "c", "value": "test1", "time": "2017"}
];
stop();
expect(1);
jIO.QueryFactory.create('identifier:%a% value:"%test1%" time:%2016%')
.exec(doc_list)
.then(function (doc_list) {
deepEqual(doc_list, [
{"identifier": "a", "value": "test1", "time": "2016"}],
'Document which matches all the fields is matched');
}).always(start);
});
test('Full text query with single word', function () {
var doc_list = [
{"identifier": "a", "value": "test", "time": "2016"},
{"identifier": "b", "value": "test 1", "time": "2017"},
{"identifier": "c", "value": "test 2016", "time": "2017"}
];
stop();
expect(2);
jIO.QueryFactory.create('test').exec(doc_list).
then(function (doc_list) {
deepEqual(doc_list, [
{"identifier": "a", "value": "test", "time": "2016"},
{"identifier": "b", "value": "test 1", "time": "2017"},
{"identifier": "c", "value": "test 2016", "time": "2017"}
], 'Documents which have test in any column are matched');
doc_list = [
{"identifier": "a", "value": "test", "time": "2016"},
{"identifier": "b", "value": "test 1", "time": "2017"},
{"identifier": "c", "value": "test 2016", "time": "2017"}
];
return jIO.QueryFactory.create('2016').exec(doc_list).
then(function (doc_list) {
deepEqual(doc_list, [
{"identifier": "a", "value": "test", "time": "2016"},
{"identifier": "c", "value": "test 2016", "time": "2017"}
], 'Documents which have 2016 in any column are matched');
}).always(start);
});
});
test('Full text query with multiple words', function () {
var doc_list = [
{"identifier": "a", "value": "test post", "time": "2016"},
{"identifier": "b", "value": "test post 1", "time": "2017"},
{"identifier": "c", "value": "test post 2016", "time": "2017"}
];
stop();
expect(2);
jIO.QueryFactory.create('test post').exec(doc_list).
then(function (doc_list) {
deepEqual(doc_list, [
{"identifier": "a", "value": "test post", "time": "2016"},
{"identifier": "b", "value": "test post 1", "time": "2017"},
{"identifier": "c", "value": "test post 2016", "time": "2017"}
], 'Documents which have test post in any column are matched');
doc_list = [
{"identifier": "a", "value": "test post", "time": "2016"},
{"identifier": "b", "value": "test post 1", "time": "2017"},
{"identifier": "c", "value": "test post 2016", "time": "2017"}
];
return jIO.QueryFactory.create('test post 2016').exec(doc_list).
then(function (doc_list) {
deepEqual(doc_list, [
{"identifier": "a", "value": "test post", "time": "2016"},
{"identifier": "c", "value": "test post 2016", "time": "2017"}
], 'Documents which have test post 2016 in any column are matched');
}).always(start);
});
});
// Asterisk wildcard is not supported yet.
/* test('Full text query with asterisk', function () {
var doc_list = [
{"identifier": "abc"},
{"identifier": "ab"}
];
stop();
expect(1);
jIO.QueryFactory.create('a*').exec(doc_list).
then(function (doc_list) {
deepEqual(doc_list, [
{"identifier": "abc"},
{"identifier": "ab"}
], 'Documents which satisfy the asterisk wildcard should be returned')
.always(start);
});
});*/
}));
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment