You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am creating a Document with an IndexedDB, and the index on it works as expected, I get the data I want on page load (data being in the indexedDB).
My issue is whenever I add new documents to the index, and then use the commit method, it inserts the new data but removes all the existing ones, instead of adding new ones.
Here are the parts of my code where I perform those actions:
functioncreateFlexSearchIndexes(): FlexSearch.Document{// Create the Document indexreturnnewFlexSearch.Document({doc: {id: 'id',index: ['message','type','flatTags','flatAttributes'],store: ['id','eventTimestamp','type','message','flatAttributes','flatTags','receivedTimestamp','attributes','tags','severity',],},tokenize: 'forward',cache: 1000,})}asyncfunctioncreateOrLoadFlexSearchDB(): Promise<{index: FlexSearch.Document}>{console.time('dbLoad')constindex=createFlexSearchIndexes()constdb=newIndexedDB('my-storage')awaitindex.mount(db)console.timeEnd('dbLoad')return{ index }}
Initialization of the flexsearch document:
useEffect(()=>{constinitDB=async()=>{try{if(!indexRef.current){const{ index }=awaitcreateOrLoadFlexSearchDB()indexRef.current=indexconstallDocs=awaitindex.search('test',{query: '',limit: 10000,enrich: true,field: 'message',})setSearchResults(allDocs[0].result.map((doc)=>({id: doc.id,eventTimestamp: doc.doc.eventTimestamp,receivedTimesamp: doc.doc.receivedTimestamp,type: doc.doc.type,message: doc.doc.message,severity: doc.doc.severity,attributes: JSON.parse(doc.doc.attributes),tags: JSON.parse(doc.doc.tags),})))}}catch(error){console.error('Failed to initialize FlexSearch:',error)}}initDB()},[])
and here is the code snippet where I try to add more data:
constsetData=useCallback(async(data)=>{if(!indexRef.current){console.log('FlexSearch not initialized')return}for(constdocofdata){indexRef.current.add(doc.id,{id: doc.id,eventTimestamp: doc.eventTimestamp,type: doc.type,message: doc.message||'',flatAttributes: flattenAttributes(doc.attributesasRecord<string,unknown>),flatTags: flattenTags(doc.tagsasRecord<string,unknown>),receivedTimestamp: doc.receivedTimestamp,attributes: JSON.stringify(doc.attributes||{}),tags: JSON.stringify(doc.tags||{}),severity: doc.severity||9,})}try{awaitindexRef.current.commit(false)setSearchResults((prev)=>[...prev, ...data])}catch(error){console.error('Error committing to index:',error)}},[])
Note that I am setting the param replace_all_contents to false to ensure the the indexedDB content does not get replaced. Let me know if you need more info, thank you for looking into this.
The text was updated successfully, but these errors were encountered:
I am using React.js and Typescript, using the [email protected].
I am creating a Document with an IndexedDB, and the index on it works as expected, I get the data I want on page load (data being in the indexedDB).
My issue is whenever I add new documents to the index, and then use the commit method, it inserts the new data but removes all the existing ones, instead of adding new ones.
Here are the parts of my code where I perform those actions:
Initialization of the flexsearch document:
and here is the code snippet where I try to add more data:
Note that I am setting the param replace_all_contents to false to ensure the the indexedDB content does not get replaced. Let me know if you need more info, thank you for looking into this.
The text was updated successfully, but these errors were encountered: