|
| 1 | +require 'sinatra/activerecord' |
| 2 | + |
| 3 | +module PJ |
| 4 | + module ExperimentSearch |
| 5 | + COLUMNS = %w[expid sra_id geo_id genome agClass agSubClass clClass clSubClass title attributes].freeze |
| 6 | + |
| 7 | + class << self |
| 8 | + def load_from_json(json_data) |
| 9 | + rows = json_data["data"] |
| 10 | + return if rows.nil? || rows.empty? |
| 11 | + |
| 12 | + db = ActiveRecord::Base.connection |
| 13 | + |
| 14 | + db.execute("DELETE FROM experiments_fts") |
| 15 | + |
| 16 | + # Bulk insert in batches |
| 17 | + rows.each_slice(500) do |batch| |
| 18 | + placeholders = batch.map { "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" }.join(", ") |
| 19 | + values = batch.flat_map do |row| |
| 20 | + COLUMNS.each_with_index.map { |_, i| row[i] || "" } |
| 21 | + end |
| 22 | + |
| 23 | + db.exec_insert( |
| 24 | + "INSERT INTO experiments_fts (#{COLUMNS.join(', ')}) VALUES #{placeholders}", |
| 25 | + "FTS Insert", |
| 26 | + values.each_with_index.map { |v, i| [nil, v] } |
| 27 | + ) |
| 28 | + end |
| 29 | + |
| 30 | + puts "ExperimentSearch: loaded #{rows.size} rows into FTS5 table" |
| 31 | + end |
| 32 | + |
| 33 | + def search(query, genome: nil, limit: 20) |
| 34 | + return { total: 0, returned: 0, experiments: [] } if query.nil? || query.strip.empty? |
| 35 | + |
| 36 | + db = ActiveRecord::Base.connection |
| 37 | + |
| 38 | + # Escape special FTS5 characters and build match expression |
| 39 | + sanitized = fts5_sanitize(query) |
| 40 | + |
| 41 | + where_clause = "experiments_fts MATCH ?" |
| 42 | + bind_values = [sanitized] |
| 43 | + |
| 44 | + if genome && !genome.empty? |
| 45 | + where_clause += " AND genome = ?" |
| 46 | + bind_values << genome |
| 47 | + end |
| 48 | + |
| 49 | + # Count total matches |
| 50 | + count_sql = "SELECT COUNT(*) FROM experiments_fts WHERE #{where_clause}" |
| 51 | + total = db.select_value(count_sql, "FTS Count", bind_values.map { |v| [nil, v] }).to_i |
| 52 | + |
| 53 | + # Fetch ranked results |
| 54 | + select_sql = <<-SQL |
| 55 | + SELECT expid, sra_id, geo_id, genome, agClass, agSubClass, |
| 56 | + clClass, clSubClass, title, attributes, |
| 57 | + rank |
| 58 | + FROM experiments_fts |
| 59 | + WHERE #{where_clause} |
| 60 | + ORDER BY rank |
| 61 | + LIMIT ? |
| 62 | + SQL |
| 63 | + |
| 64 | + rows = db.select_all( |
| 65 | + select_sql, |
| 66 | + "FTS Search", |
| 67 | + (bind_values + [limit]).map { |v| [nil, v] } |
| 68 | + ) |
| 69 | + |
| 70 | + experiments = rows.map do |row| |
| 71 | + { |
| 72 | + expid: row["expid"], |
| 73 | + sra_id: row["sra_id"], |
| 74 | + geo_id: row["geo_id"], |
| 75 | + genome: row["genome"], |
| 76 | + agClass: row["agClass"], |
| 77 | + agSubClass: row["agSubClass"], |
| 78 | + clClass: row["clClass"], |
| 79 | + clSubClass: row["clSubClass"], |
| 80 | + title: row["title"], |
| 81 | + attributes: row["attributes"] |
| 82 | + } |
| 83 | + end |
| 84 | + |
| 85 | + { total: total, returned: experiments.size, experiments: experiments } |
| 86 | + end |
| 87 | + |
| 88 | + private |
| 89 | + |
| 90 | + def fts5_sanitize(query) |
| 91 | + # Remove FTS5 special characters, then wrap each token in quotes for safety |
| 92 | + tokens = query.strip.split(/\s+/).map do |token| |
| 93 | + # Strip special FTS5 operators |
| 94 | + cleaned = token.gsub(/["()*^{}:]/, "") |
| 95 | + next nil if cleaned.empty? |
| 96 | + %("#{cleaned}") |
| 97 | + end.compact |
| 98 | + |
| 99 | + tokens.join(" ") |
| 100 | + end |
| 101 | + end |
| 102 | + end |
| 103 | +end |
0 commit comments