@ -1,13 +1,22 @@
import Config from './config' ;
import Config from './config' ;
export type SearchIndex = {
fullTexts : {
[ peerID : string ] : string
} / * ,
shortIndexes : {
[ shortStr : string ] : number [ ]
} * /
} ;
class SearchIndexManager {
class SearchIndexManager {
public static badCharsRe = /[`~!@#$%^&*()\-_=+\[\]\\|{}'";:\/?.>,<]+/g ;
public static badCharsRe = /[`~!@#$%^&*()\-_=+\[\]\\|{}'";:\/?.>,<]+/g ;
public static trimRe = /^\s+|\s$/g ;
public static trimRe = /^\s+|\s$/g ;
public createIndex() {
public createIndex ( ) : SearchIndex {
return {
return {
shortIndexes : { } ,
fullText s: { } / * ,
fullTexts : { }
shortIndexe s: { } * /
} ;
} ;
}
}
@ -33,21 +42,19 @@ class SearchIndexManager {
return username && username . toLowerCase ( ) || '' ;
return username && username . toLowerCase ( ) || '' ;
}
}
public indexObject ( id : number , searchText : string , searchIndex : any ) {
public indexObject ( id : number , searchText : string , searchIndex : SearchIndex ) {
if ( searchIndex . fullTexts [ id ] !== undefined ) {
if ( searchIndex . fullTexts . hasOwnProperty ( id ) ) {
return false ;
return false ;
}
}
searchText = this . cleanSearchText ( searchText ) ;
searchText = this . cleanSearchText ( searchText ) ;
if ( ! searchText . length ) {
if ( ! searchText . length ) {
return false ;
return false ;
}
}
const shortIndexes = searchIndex . shortIndexes ;
searchIndex . fullTexts [ id ] = searchText ;
searchIndex . fullTexts [ id ] = searchText ;
/ * c o n s t s h o r t I n d e x e s = s e a r c h I n d e x . s h o r t I n d e x e s ;
searchText . split ( ' ' ) . forEach ( ( searchWord ) = > {
searchText . split ( ' ' ) . forEach ( ( searchWord ) = > {
let len = Math . min ( searchWord . length , 3 ) ,
let len = Math . min ( searchWord . length , 3 ) ,
wordPart , i ;
wordPart , i ;
@ -59,39 +66,52 @@ class SearchIndexManager {
shortIndexes [ wordPart ] . push ( id ) ;
shortIndexes [ wordPart ] . push ( id ) ;
}
}
}
}
} ) ;
} ) ; * /
}
}
public search ( query : string , searchIndex : any ) {
public search ( query : string , searchIndex : SearchIndex ) {
const shortIndexes = searchIndex . shortIndexes ;
const fullTexts = searchIndex . fullTexts ;
const fullTexts = searchIndex . fullTexts ;
//const shortIndexes = searchIndex.shortIndexes;
query = this . cleanSearchText ( query ) ;
query = this . cleanSearchText ( query ) ;
const newFoundObjs : { [ peerID : string ] : true } = { } ;
const queryWords = query . split ( ' ' ) ;
const queryWords = query . split ( ' ' ) ;
let foundObjs : any = false ,
for ( const peerID in fullTexts ) {
newFoundObjs : any , i : number ;
const fullText = fullTexts [ peerID ] ;
let j : number , searchText : string ;
let found : boolean ;
let found = true ;
for ( const word of queryWords ) {
for ( i = 0 ; i < queryWords . length ; i ++ ) {
if ( fullText . indexOf ( word ) === - 1 ) {
newFoundObjs = shortIndexes [ queryWords [ i ] . substr ( 0 , 3 ) ] ;
found = false ;
if ( ! newFoundObjs ) {
break ;
foundObjs = [ ] ;
}
}
if ( found ) {
newFoundObjs [ peerID ] = true ;
}
}
/ * c o n s t q u e r y W o r d s = q u e r y . s p l i t ( ' ' ) ;
let foundArr : number [ ] ;
for ( let i = 0 ; i < queryWords . length ; i ++ ) {
const newFound = shortIndexes [ queryWords [ i ] . substr ( 0 , 3 ) ] ;
if ( ! newFound ) {
foundArr = [ ] ;
break ;
break ;
}
}
if ( foundObjs === false || foundObjs . length > newFoundObjs . length ) {
if ( foundArr === undefined || foundArr . length > newFound . length ) {
foundObjs = newFoundObjs ;
foundArr = newFound ;
}
}
}
}
newFoundObjs = { } ;
for ( let j = 0 ; j < foundArr . length ; j ++ ) {
let found = true ;
for ( j = 0 ; j < foundObjs . length ; j ++ ) {
let searchText = fullTexts [ foundArr [ j ] ] ;
found = true ;
for ( let i = 0 ; i < queryWords . length ; i ++ ) {
searchText = fullTexts [ foundObjs [ j ] ] ;
for ( i = 0 ; i < queryWords . length ; i ++ ) {
if ( searchText . indexOf ( queryWords [ i ] ) == - 1 ) {
if ( searchText . indexOf ( queryWords [ i ] ) == - 1 ) {
found = false ;
found = false ;
break ;
break ;
@ -99,9 +119,9 @@ class SearchIndexManager {
}
}
if ( found ) {
if ( found ) {
newFoundObjs [ foundObjs [ j ] ] = true ;
newFoundObjs [ foundArr [ j ] ] = true ;
}
}
}
} * /
return newFoundObjs ;
return newFoundObjs ;
}
}