aboutsummaryrefslogtreecommitdiffstats
path: root/lib/acts_as_xapian/tasks/xapian.rake
blob: c1986ce1e6094510589f302c1edff206bc885210 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require 'rubygems'
require 'rake'
require 'rake/testtask'
require 'active_record'

namespace :xapian do
    # Parameters - specify "flush=true" to save changes to the Xapian database
    # after each model that is updated. This is safer, but slower. Specify
    # "verbose=true" to print model name as it is run.
    desc 'Updates Xapian search index with changes to models since last call'
    task :update_index => :environment do
        ActsAsXapian.update_index(ENV['flush'] ? true : false, ENV['verbose'] ? true : false)
    end

    # Parameters - specify 'models="PublicBody User"' to say which models
    # you index with Xapian.

    # This totally rebuilds the database, so you will want to restart
    # any web server afterwards to make sure it gets the changes,
    # rather than still pointing to the old deleted database. Specify
    # "verbose=true" to print model name as it is run.  By default,
    # all of the terms, values and texts are reindexed.  You can
    # suppress any of these by specifying, for example, "texts=false".
    # You can specify that only certain terms should be updated by
    # specifying their prefix(es) as a string, e.g. "terms=IV" will
    # index the two terms I and V (and "terms=false" will index none,
    # and "terms=true", the default, will index all)


    desc 'Completely rebuilds Xapian search index (must specify all models)'
    task :rebuild_index => :environment do
        def coerce_arg(arg, default)
	    if arg == "false"
	        return false
            elsif arg == "true"
                return true
            elsif arg.nil?
	        return default
            else
                return arg
            end
	end
        raise "specify ALL your models with models=\"ModelName1 ModelName2\" as parameter" if ENV['models'].nil?
        ActsAsXapian.rebuild_index(ENV['models'].split(" ").map{|m| m.constantize}, 
	coerce_arg(ENV['verbose'], false),
        coerce_arg(ENV['terms'], true),
        coerce_arg(ENV['values'], true),
        coerce_arg(ENV['texts'], true))
    end

    # Parameters - are models, query, offset, limit, sort_by_prefix,
    # collapse_by_prefix
    desc 'Run a query, return YAML of results'
    task :query => :environment do
        raise "specify models=\"ModelName1 ModelName2\" as parameter" if ENV['models'].nil?
        raise "specify query=\"your terms\" as parameter" if ENV['query'].nil?
        s = ActsAsXapian::Search.new(ENV['models'].split(" ").map{|m| m.constantize}, 
            ENV['query'],
            :offset => (ENV['offset'] || 0), :limit => (ENV['limit'] || 10),
            :sort_by_prefix => (ENV['sort_by_prefix'] || nil), 
            :collapse_by_prefix => (ENV['collapse_by_prefix'] || nil)
        )
        STDOUT.puts(s.results.to_yaml)
    end
end