1
- import { thaw } from '$lib/scripts/thaw' ;
2
- import type {
3
- QueryGenerator ,
4
- SearchOptions ,
5
- SearchQuery
6
- } from '$lib/search/domain/interfaces/data-interfaces' ;
7
- import type { Message } from '$lib/utils/worker-messenger/message' ;
8
- import { Messenger , type MessageIO } from '$lib/utils/worker-messenger/messenger' ;
9
- import { ProskommaSearchRepositoryImpl } from './data/repositories/pk-search-repository-impl' ;
10
- import { ProksommaWorkerSearchRepository } from './data/repositories/pk-worker-search-repository' ;
11
- import { ScriptureRepositoryImpl } from './data/repositories/scripture-repository-impl' ;
12
- import type { ScriptureRepository } from './domain/interfaces/data-interfaces' ;
13
- import { SearchQueryInternal } from './domain/search-query-internal' ;
14
- import { SearchSessionInternal } from './domain/search-session-internal' ;
15
- import initSqlJs from 'sql.js' ;
1
+ import type { SearchOptions } from '$lib/search/domain/interfaces/data-interfaces' ;
16
2
import { initializeDatabase } from '$lib/data/stores/lexicon' ;
17
3
18
- // const messageIO: MessageIO = {
19
- // postMessage: function (message: Message): void {
20
- // self.postMessage(message);
21
- // },
22
-
23
- // setOnMessage: function (handler: (event: MessageEvent<Message>) => void): void {
24
- // self.onmessage = handler;
25
- // }
26
- // };
27
-
28
- // const messenger = new Messenger(messageIO);
29
-
30
4
// Search the dictionary based on the phrase and options
31
5
export async function searchDictionary ( phrase : string , options : SearchOptions ) {
6
+
7
+ const searchWords = phrase . split ( ' ' ) ;
8
+
32
9
const column = options . accentsAndTones ? 'word' : 'word_no_accents' ;
33
10
34
- const searchPattern = options . wholeWords ? `${ phrase } ` : `%${ phrase } %` ;
35
-
36
11
let db = await initializeDatabase ( { fetch } )
37
- let results = db . exec ( `SELECT locations FROM search_words WHERE ${ column } LIKE ?` , [ searchPattern ] ) ;
38
-
39
- if ( ! results . length || ! results [ 0 ] . values . length ) {
12
+ let results ;
13
+ const dynamicQuery = searchWords
14
+ . map ( ( ) => `${ column } LIKE ?` )
15
+ . join ( ' OR ' ) ;
16
+ const dynamicParams = searchWords . map ( word => options . wholeWords ? word : `%${ word } %` ) ;
17
+ results = db . exec ( `SELECT locations FROM search_words WHERE ${ dynamicQuery } ` , dynamicParams ) ;
18
+ console . log ( 'results:' , results ) ;
19
+
20
+ if ( ! results ?. length || ! results [ 0 ] ?. values ?. length ) {
40
21
return [ ] ;
41
22
}
42
23
43
24
// Extract and process locations from the query result
44
- let locations = results [ 0 ] . values [ 0 ] [ 0 ] . split ( ' ' ) . map ( loc => {
45
- const [ id , weight ] = loc . split ( '(' ) . map ( v => v . replace ( ')' , '' ) ) ;
46
- return { id : parseInt ( id , 10 ) , weight : parseInt ( weight , 10 ) } ;
25
+ let locations = results [ 0 ] . values . flatMap ( value =>
26
+ value [ 0 ] . split ( ' ' ) . map ( loc => {
27
+ const [ id , weight ] = loc . split ( '(' ) . map ( v => v . replace ( ')' , '' ) ) ;
28
+ return { id : parseInt ( id , 10 ) , weight : parseInt ( weight , 10 ) } ;
29
+ } )
30
+ ) ;
31
+
32
+ // Remove duplicates by creating a Map with unique IDs
33
+ const uniqueLocationsMap = new Map ( ) ;
34
+ locations . forEach ( location => {
35
+ if ( ! uniqueLocationsMap . has ( location . id ) || uniqueLocationsMap . get ( location . id ) . weight < location . weight ) {
36
+ uniqueLocationsMap . set ( location . id , location ) ;
37
+ }
47
38
} ) ;
39
+ locations = Array . from ( uniqueLocationsMap . values ( ) ) ;
48
40
49
41
locations = ( locations . sort ( ( a , b ) => b . weight - a . weight ) ) ;
50
42
const ids = locations . map ( location => location . id ) ;
51
43
return ids ;
52
- }
53
-
54
- // async function makeQuery(phrase: string, options: SearchOptions): Promise<SearchQuery> {
55
- // const dictionaryResults = await searchDictionary(phrase, options);
56
- // const scriptureRepo = makeScriptureRepository();
57
- // const scriptureResults = await scriptureRepo.queryVerses(phrase, options);
58
- // return new SearchQueryInternal(phrase, options, scriptureRepo);
59
- // }
60
-
61
- // const session = new SearchSessionInternal(makeQuery);
62
- // messenger.setInboundHandler(session.getRequestHandler());
44
+ }
0 commit comments