2018-12-09 22:45:50 +02:00
require ( 'app-module-path' ) . addPath ( _ _dirname ) ;
const { time } = require ( 'lib/time-utils.js' ) ;
2019-01-14 21:11:54 +02:00
const { fileContentEqual , setupDatabase , setupDatabaseAndSynchronizer , asyncTest , db , synchronizer , fileApi , sleep , clearDatabase , switchClient , syncTargetId , objectsEqual , checkThrowAsync } = require ( 'test-utils.js' ) ;
2018-12-09 22:45:50 +02:00
const SearchEngine = require ( 'lib/services/SearchEngine' ) ;
const Note = require ( 'lib/models/Note' ) ;
2019-01-14 21:11:54 +02:00
const ItemChange = require ( 'lib/models/ItemChange' ) ;
const Setting = require ( 'lib/models/Setting' ) ;
2018-12-09 22:45:50 +02:00
process . on ( 'unhandledRejection' , ( reason , p ) => {
console . log ( 'Unhandled Rejection at: Promise' , p , 'reason:' , reason ) ;
} ) ;
let engine = null ;
describe ( 'services_SearchEngine' , function ( ) {
beforeEach ( async ( done ) => {
await setupDatabaseAndSynchronizer ( 1 ) ;
await switchClient ( 1 ) ;
2018-12-10 20:54:46 +02:00
2018-12-09 22:45:50 +02:00
engine = new SearchEngine ( ) ;
engine . setDb ( db ( ) ) ;
2018-12-10 20:54:46 +02:00
2018-12-09 22:45:50 +02:00
done ( ) ;
} ) ;
2019-01-14 21:11:54 +02:00
it ( 'should keep the content and FTS table in sync' , asyncTest ( async ( ) => {
2018-12-12 23:40:05 +02:00
let rows , n1 , n2 , n3 ;
n1 = await Note . save ( { title : "a" } ) ;
n2 = await Note . save ( { title : "b" } ) ;
2018-12-29 21:19:18 +02:00
await engine . syncTables ( ) ;
2018-12-12 23:40:05 +02:00
rows = await engine . search ( 'a' ) ;
expect ( rows . length ) . toBe ( 1 ) ;
expect ( rows [ 0 ] . title ) . toBe ( 'a' ) ;
2018-12-10 20:54:46 +02:00
2018-12-12 23:40:05 +02:00
await Note . delete ( n1 . id ) ;
2018-12-29 21:19:18 +02:00
await engine . syncTables ( ) ;
2018-12-12 23:40:05 +02:00
rows = await engine . search ( 'a' ) ;
2018-12-10 20:54:46 +02:00
expect ( rows . length ) . toBe ( 0 ) ;
2018-12-12 23:40:05 +02:00
rows = await engine . search ( 'b' ) ;
expect ( rows [ 0 ] . title ) . toBe ( 'b' ) ;
2018-12-10 20:54:46 +02:00
2018-12-12 23:40:05 +02:00
await Note . save ( { id : n2 . id , title : 'c' } ) ;
2018-12-29 21:19:18 +02:00
await engine . syncTables ( ) ;
2018-12-12 23:40:05 +02:00
rows = await engine . search ( 'b' ) ;
expect ( rows . length ) . toBe ( 0 ) ;
rows = await engine . search ( 'c' ) ;
expect ( rows [ 0 ] . title ) . toBe ( 'c' ) ;
2018-12-10 20:54:46 +02:00
2018-12-12 23:40:05 +02:00
await Note . save ( { id : n2 . id , encryption _applied : 1 } ) ;
2018-12-29 21:19:18 +02:00
await engine . syncTables ( ) ;
2018-12-12 23:40:05 +02:00
rows = await engine . search ( 'c' ) ;
expect ( rows . length ) . toBe ( 0 ) ;
2018-12-10 20:54:46 +02:00
2018-12-12 23:40:05 +02:00
await Note . save ( { id : n2 . id , encryption _applied : 0 } ) ;
2018-12-29 21:19:18 +02:00
await engine . syncTables ( ) ;
2018-12-12 23:40:05 +02:00
rows = await engine . search ( 'c' ) ;
expect ( rows . length ) . toBe ( 1 ) ;
2019-01-14 21:11:54 +02:00
} ) ) ;
2018-12-10 20:54:46 +02:00
2019-01-14 21:11:54 +02:00
it ( 'should, after initial indexing, save the last change ID' , asyncTest ( async ( ) => {
const n1 = await Note . save ( { title : "abcd efgh" } ) ; // 3
const n2 = await Note . save ( { title : "abcd aaaaa abcd abcd" } ) ; // 1
expect ( Setting . value ( 'searchEngine.initialIndexingDone' ) ) . toBe ( false ) ;
await ItemChange . waitForAllSaved ( ) ;
const lastChangeId = await ItemChange . lastChangeId ( ) ;
2018-12-09 22:45:50 +02:00
2019-01-14 21:11:54 +02:00
await engine . syncTables ( ) ;
expect ( Setting . value ( 'searchEngine.lastProcessedChangeId' ) ) . toBe ( lastChangeId ) ;
expect ( Setting . value ( 'searchEngine.initialIndexingDone' ) ) . toBe ( true ) ;
} ) ) ;
it ( 'should order search results by relevance (1)' , asyncTest ( async ( ) => {
2018-12-12 23:40:05 +02:00
const n1 = await Note . save ( { title : "abcd efgh" } ) ; // 3
const n2 = await Note . save ( { title : "abcd aaaaa abcd abcd" } ) ; // 1
const n3 = await Note . save ( { title : "abcd aaaaa bbbb eeee abcd" } ) ; // 2
2018-12-10 20:54:46 +02:00
2018-12-29 21:19:18 +02:00
await engine . syncTables ( ) ;
2018-12-12 23:40:05 +02:00
const rows = await engine . search ( 'abcd' ) ;
2018-12-10 20:54:46 +02:00
2018-12-12 23:40:05 +02:00
expect ( rows [ 0 ] . id ) . toBe ( n2 . id ) ;
expect ( rows [ 1 ] . id ) . toBe ( n3 . id ) ;
expect ( rows [ 2 ] . id ) . toBe ( n1 . id ) ;
2019-01-14 21:11:54 +02:00
} ) ) ;
2018-12-10 20:54:46 +02:00
2019-01-14 21:11:54 +02:00
it ( 'should order search results by relevance (2)' , asyncTest ( async ( ) => {
2018-12-10 20:54:46 +02:00
// 1
const n1 = await Note . save ( { title : "abcd efgh" , body : "XX abcd XX efgh" } ) ;
// 4
const n2 = await Note . save ( { title : "abcd aaaaa bbbb eeee efgh" } ) ;
// 3
const n3 = await Note . save ( { title : "abcd aaaaa efgh" } ) ;
// 2
const n4 = await Note . save ( { title : "blablablabla blabla bla abcd X efgh" } ) ;
// 5
const n5 = await Note . save ( { title : "occurence many times but very abcd spread appart spread appart spread appart spread appart spread appart efgh occurence many times but very abcd spread appart spread appart spread appart spread appart spread appart efgh occurence many times but very abcd spread appart spread appart spread appart spread appart spread appart efgh occurence many times but very abcd spread appart spread appart spread appart spread appart spread appart efgh occurence many times but very abcd spread appart spread appart spread appart spread appart spread appart efgh" } ) ;
2018-12-29 21:19:18 +02:00
await engine . syncTables ( ) ;
2018-12-10 20:54:46 +02:00
const rows = await engine . search ( 'abcd efgh' ) ;
expect ( rows [ 0 ] . id ) . toBe ( n1 . id ) ;
expect ( rows [ 1 ] . id ) . toBe ( n4 . id ) ;
expect ( rows [ 2 ] . id ) . toBe ( n3 . id ) ;
expect ( rows [ 3 ] . id ) . toBe ( n2 . id ) ;
expect ( rows [ 4 ] . id ) . toBe ( n5 . id ) ;
2019-01-14 21:11:54 +02:00
} ) ) ;
2018-12-10 20:54:46 +02:00
2019-02-24 14:00:06 +02:00
it ( 'should order search results by relevance (last updated first)' , asyncTest ( async ( ) => {
let rows ;
const n1 = await Note . save ( { title : "abcd" } ) ;
await sleep ( 0.1 ) ;
const n2 = await Note . save ( { title : "abcd" } ) ;
await sleep ( 0.1 ) ;
const n3 = await Note . save ( { title : "abcd" } ) ;
await sleep ( 0.1 ) ;
await engine . syncTables ( ) ;
rows = await engine . search ( 'abcd' ) ;
expect ( rows [ 0 ] . id ) . toBe ( n3 . id ) ;
expect ( rows [ 1 ] . id ) . toBe ( n2 . id ) ;
expect ( rows [ 2 ] . id ) . toBe ( n1 . id ) ;
await Note . save ( { id : n1 . id , title : "abcd" } ) ;
await engine . syncTables ( ) ;
rows = await engine . search ( 'abcd' ) ;
expect ( rows [ 0 ] . id ) . toBe ( n1 . id ) ;
expect ( rows [ 1 ] . id ) . toBe ( n3 . id ) ;
expect ( rows [ 2 ] . id ) . toBe ( n2 . id ) ;
} ) ) ;
it ( 'should order search results by relevance (completed to-dos last)' , asyncTest ( async ( ) => {
let rows ;
const n1 = await Note . save ( { title : "abcd" , is _todo : 1 } ) ;
await sleep ( 0.1 ) ;
const n2 = await Note . save ( { title : "abcd" , is _todo : 1 } ) ;
await sleep ( 0.1 ) ;
const n3 = await Note . save ( { title : "abcd" , is _todo : 1 } ) ;
await sleep ( 0.1 ) ;
await engine . syncTables ( ) ;
rows = await engine . search ( 'abcd' ) ;
expect ( rows [ 0 ] . id ) . toBe ( n3 . id ) ;
expect ( rows [ 1 ] . id ) . toBe ( n2 . id ) ;
expect ( rows [ 2 ] . id ) . toBe ( n1 . id ) ;
await Note . save ( { id : n3 . id , todo _completed : Date . now ( ) } ) ;
await engine . syncTables ( ) ;
rows = await engine . search ( 'abcd' ) ;
expect ( rows [ 0 ] . id ) . toBe ( n2 . id ) ;
expect ( rows [ 1 ] . id ) . toBe ( n1 . id ) ;
expect ( rows [ 2 ] . id ) . toBe ( n3 . id ) ;
} ) ) ;
2019-01-14 21:11:54 +02:00
it ( 'should supports various query types' , asyncTest ( async ( ) => {
2018-12-10 20:54:46 +02:00
let rows ;
const n1 = await Note . save ( { title : "abcd efgh ijkl" , body : "aaaa bbbb" } ) ;
const n2 = await Note . save ( { title : "iiii efgh bbbb" , body : "aaaa bbbb" } ) ;
2018-12-29 21:19:18 +02:00
const n3 = await Note . save ( { title : "Агентство Рейтер" } ) ;
const n4 = await Note . save ( { title : "Dog" } ) ;
const n5 = await Note . save ( { title : "СООБЩИЛО" } ) ;
await engine . syncTables ( ) ;
2018-12-10 20:54:46 +02:00
rows = await engine . search ( 'abcd ijkl' ) ;
expect ( rows . length ) . toBe ( 1 ) ;
rows = await engine . search ( '"abcd ijkl"' ) ;
expect ( rows . length ) . toBe ( 0 ) ;
rows = await engine . search ( '"abcd efgh"' ) ;
expect ( rows . length ) . toBe ( 1 ) ;
rows = await engine . search ( 'title:abcd' ) ;
expect ( rows . length ) . toBe ( 1 ) ;
rows = await engine . search ( 'title:efgh' ) ;
expect ( rows . length ) . toBe ( 2 ) ;
rows = await engine . search ( 'body:abcd' ) ;
expect ( rows . length ) . toBe ( 0 ) ;
rows = await engine . search ( 'body:bbbb' ) ;
expect ( rows . length ) . toBe ( 2 ) ;
rows = await engine . search ( 'body:bbbb iiii' ) ;
expect ( rows . length ) . toBe ( 1 ) ;
2018-12-09 22:45:50 +02:00
2018-12-29 21:19:18 +02:00
rows = await engine . search ( 'Рейтер' ) ;
expect ( rows . length ) . toBe ( 1 ) ;
2019-01-13 18:05:07 +02:00
rows = await engine . search ( 'рейтер' ) ;
2018-12-29 21:19:18 +02:00
expect ( rows . length ) . toBe ( 1 ) ;
rows = await engine . search ( 'Dog' ) ;
expect ( rows . length ) . toBe ( 1 ) ;
rows = await engine . search ( 'dog' ) ;
expect ( rows . length ) . toBe ( 1 ) ;
rows = await engine . search ( 'сообщило' ) ;
expect ( rows . length ) . toBe ( 1 ) ;
2019-01-14 21:11:54 +02:00
} ) ) ;
2018-12-29 21:19:18 +02:00
2019-01-14 21:11:54 +02:00
it ( 'should support queries with or without accents' , asyncTest ( async ( ) => {
let rows ;
const n1 = await Note . save ( { title : "père noël" } ) ;
await engine . syncTables ( ) ;
expect ( ( await engine . search ( 'père' ) ) . length ) . toBe ( 1 ) ;
expect ( ( await engine . search ( 'pere' ) ) . length ) . toBe ( 1 ) ;
expect ( ( await engine . search ( 'noe*' ) ) . length ) . toBe ( 1 ) ;
expect ( ( await engine . search ( 'noë*' ) ) . length ) . toBe ( 1 ) ;
} ) ) ;
2018-12-09 22:45:50 +02:00
2019-01-14 21:11:54 +02:00
it ( 'should support queries with Chinese characters' , asyncTest ( async ( ) => {
let rows ;
const n1 = await Note . save ( { title : "我是法国人" } ) ;
await engine . syncTables ( ) ;
expect ( ( await engine . search ( '我' ) ) . length ) . toBe ( 1 ) ;
expect ( ( await engine . search ( '法国人' ) ) . length ) . toBe ( 1 ) ;
} ) ) ;
it ( 'should support queries with Japanese characters' , asyncTest ( async ( ) => {
let rows ;
const n1 = await Note . save ( { title : "私は日本語を話すことができません" } ) ;
await engine . syncTables ( ) ;
expect ( ( await engine . search ( '日本' ) ) . length ) . toBe ( 1 ) ;
expect ( ( await engine . search ( 'できません' ) ) . length ) . toBe ( 1 ) ;
} ) ) ;
it ( 'should support queries with Korean characters' , asyncTest ( async ( ) => {
let rows ;
const n1 = await Note . save ( { title : "이것은 한국말이다" } ) ;
await engine . syncTables ( ) ;
expect ( ( await engine . search ( '이것은' ) ) . length ) . toBe ( 1 ) ;
expect ( ( await engine . search ( '말' ) ) . length ) . toBe ( 1 ) ;
} ) ) ;
it ( 'should parse normal query strings' , asyncTest ( async ( ) => {
2018-12-12 23:40:05 +02:00
let rows ;
const testCases = [
[ 'abcd efgh' , { _ : [ 'abcd' , 'efgh' ] } ] ,
[ 'abcd efgh' , { _ : [ 'abcd' , 'efgh' ] } ] ,
[ 'title:abcd efgh' , { _ : [ 'efgh' ] , title : [ 'abcd' ] } ] ,
[ 'title:abcd' , { title : [ 'abcd' ] } ] ,
[ '"abcd efgh"' , { _ : [ 'abcd efgh' ] } ] ,
] ;
for ( let i = 0 ; i < testCases . length ; i ++ ) {
const t = testCases [ i ] ;
const input = t [ 0 ] ;
const expected = t [ 1 ] ;
const actual = engine . parseQuery ( input ) ;
2019-01-31 11:48:48 +02:00
const _Values = actual . terms . _ ? actual . terms . _ . map ( v => v . value ) : undefined ;
const titleValues = actual . terms . title ? actual . terms . title . map ( v => v . value ) : undefined ;
const bodyValues = actual . terms . body ? actual . terms . body . map ( v => v . value ) : undefined ;
2018-12-12 23:40:05 +02:00
2019-01-31 11:48:48 +02:00
expect ( JSON . stringify ( _Values ) ) . toBe ( JSON . stringify ( expected . _ ) , 'Test case (_) ' + i ) ;
expect ( JSON . stringify ( titleValues ) ) . toBe ( JSON . stringify ( expected . title ) , 'Test case (title) ' + i ) ;
expect ( JSON . stringify ( bodyValues ) ) . toBe ( JSON . stringify ( expected . body ) , 'Test case (body) ' + i ) ;
2018-12-12 23:40:05 +02:00
}
2019-01-14 21:11:54 +02:00
} ) ) ;
2018-12-12 23:40:05 +02:00
2019-01-31 11:48:48 +02:00
// it('should parse query strings with wildcards', asyncTest(async () => {
// let rows;
// const testCases = [
// ['do*', ['do', 'dog', 'domino'], [] ],
// // "*" is a wildcard only when used at the end (to search for documents with the specified prefix)
// // If it's at the beginning, it's ignored, if it's in the middle, it's treated as a litteral "*".
// ['*an*', ['an', 'anneau'], ['piano', 'plan'] ],
// ['no*no', ['no*no'], ['nonono'] ],
// ];
// for (let i = 0; i < testCases.length; i++) {
// const t = testCases[i];
// const input = t[0];
// const shouldMatch = t[1];
// const shouldNotMatch = t[2];
// const regex = new RegExp(engine.parseQuery(input).terms._[0].value, 'gmi');
// for (let j = 0; j < shouldMatch.length; j++) {
// const r = shouldMatch[j].match(regex);
// expect(!!r).toBe(true, '"' + input + '" should match "' + shouldMatch[j] + '"');
// }
// }
// expect(engine.parseQuery('*').termCount).toBe(0);
// }));
2019-01-31 10:35:41 +02:00
it ( 'should handle queries with special characters' , asyncTest ( async ( ) => {
let rows ;
const testCases = [
[ 'did-not-match' , 'did-not-match' ] ,
[ 'does match' , 'does match' ] ,
] ;
for ( let i = 0 ; i < testCases . length ; i ++ ) {
const t = testCases [ i ] ;
const content = t [ 0 ] ;
const query = t [ 1 ] ;
const n = await Note . save ( { title : content } ) ;
await engine . syncTables ( ) ;
rows = await engine . search ( query ) ;
expect ( rows . length ) . toBe ( 1 ) ;
await Note . delete ( n . id ) ;
}
} ) ) ;
2018-12-09 22:45:50 +02:00
} ) ;