""" def setupfts(): Create the FTS table cursor.execute(''' CREATE VIRTUAL TABLE IF NOT…
- User
""" def setup_fts(): # Create the FTS table cursor.execute(''' CREATE VIRTUAL TABLE IF NOT EXISTS messages_fts USING fts5( id1 UNINDEXED, id2 UNINDEXED, board UNINDEXED, message, subject, tokenize = 'unicode61' ); ''') connection.commit()
# Clear existing data from the FTS table cursor.execute('DELETE FROM messages_fts;') connection.commit() boards = [] cursor.execute("SELECT name FROM sqlite_schema WHERE type ='table' AND name NOT LIKE 'sqlite_%'") rows = cursor.fetchall() for row in rows: json_like_memoryview = row[0] json_like_bytes = bytes(json_like_memoryview) # Convert memoryview to bytes json_like_string = json_like_bytes.decode('utf-8') # Convert bytes to str boards.append(json_like_string) # Populate the FTS table for board in boards: if board != 'fadenWache' and "messages_fts" not in board: # Insert top-level messages and subjects cursor.execute(f''' INSERT INTO messages_fts (id1, board, message, subject) SELECT {board}.id AS id1, '{board}' AS board, json_extract({board}.voll, '$.message') AS message, json_extract({board}.voll, '$.subject') AS subject FROM {board} WHERE json_valid({board}.voll) AND (json_extract({board}.voll, '$.message') IS NOT NULL OR json_extract({board}.voll, '$.subject') IS NOT NULL); ''') # Insert messages and subjects from nested posts cursor.execute(f''' INSERT INTO messages_fts (id1, id2, board, message, subject) SELECT {board}.id AS id1, json_extract(p.value, '$.postId') AS id2, '{board}' AS board, json_extract(p.value, '$.message') AS message, json_extract(p.value, '$.subject') AS subject FROM {board} JOIN json_each({board}.voll, '$.posts') AS p WHERE json_valid({board}.voll) AND (json_extract(p.value, '$.message') IS NOT NULL OR json_extract(p.value, '$.subject') IS NOT NULL); ''') connection.commit() # Set up triggers for each board for board in boards: if board != 'fadenWache': # Drop existing triggers if they exist cursor.executescript(f''' DROP TRIGGER IF EXISTS {board}_after_insert; DROP TRIGGER IF EXISTS {board}_after_update; DROP TRIGGER IF EXISTS {board}_after_delete; ''') # Create triggers (same as in step 3) # ... (Use the trigger creation code provided earlier) connection.commit() print('completed setup_fts')@app.route('/search', methods=['GET']) def doSearch(): #bid=request.form['fbid']
tic = time.perf_counter() search_term=request.args.get('q') cursor=connection.cursor() cursor.execute("SELECT name FROM sqlite_schema WHERE type ='table' AND name NOT LIKE 'sqlite_%'") rows = cursor.fetchall() boards = [] for row in rows: json_like_memoryview = row[0] json_like_bytes = bytes(json_like_memoryview) # Convert memoryview to bytes json_like_string = json_like_bytes.decode('utf-8') # Convert bytes to str boards.append(json_like_string) search_term=" ".join(search_term.split()) if not search_term: search_term=' ' # Use the MATCH operator for efficient search query = ''' SELECT id1, id2, board, message, subject FROM messages_fts WHERE messages_fts MATCH ?; ''' cursor.execute(query, (search_term,)) results = cursor.fetchall() out="" outnum=0 # Process the results for id1, id2, board, message, subject in results: if (outnum<1000): board=bytes(board).decode('utf-8') out+=(f'Board: {board} ') out+=(f'TID: {id1} ') out+=(f'PID: {id2} ') out+=(f'Subject: {re.sub(rf'(<[^>]*>)|({search_term})', grab_search.replace_outside_tags, ((bytes(subject).decode("utf-8"))+' ' if isinstance(subject,memoryview) else "--- "), flags=re.IGNORECASE | re.MULTILINE)}') out+=(f'Message: {re.sub(rf'(<[^>]*>)|({search_term})', grab_search.replace_outside_tags, ('<pre>'+grab_server.escapeMessage(bytes(message).decode("utf-8"),id1,(board))+'</pre>' if isinstance(message,memoryview) else "---"), flags=re.IGNORECASE | re.MULTILINE)}') out+=('<br>') outnum+=1 return '<!DOCTYPE html><head><title>KC-Archive</title><meta name = "viewport" content = "width = device-width"></head>'+grab_server.generatePageFormat(session)+str(len(results))+' results<br><br>'+str(out) """ - Assistant