Last active
December 24, 2021 18:11
-
-
Save svandragt/f384963a5186c83be08c040b338c7f8c to your computer and use it in GitHub Desktop.
Full text search a single text file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# v2021-12-24.1 | |
require 'sqlite3' | |
dbf = '/tmp/plan.db' | |
File.delete(dbf) if File.exists? dbf | |
db = SQLite3::Database.open dbf | |
db.results_as_hash = true | |
db.execute "CREATE VIRTUAL TABLE IF NOT EXISTS plan USING FTS5(contents,tokenize = porter)" | |
db.execute "INSERT INTO plan (contents) VALUES(?)", File.open(ARGV[0]).read | |
results = db.query "SELECT snippet(plan, 0,'[', ']', '...',32) as result FROM plan(?) ORDER BY rank ", ARGV.slice(1,ARGV.length).join(" ") | |
while r = results.next | |
puts r['result'] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Setup:
$ chmod +x fts.rb