| CODENOTIFIER | HelpYou are not signed inSign in |
Project: acts_as_ferret
Revision: 346
Author: jk
Date: 20 May 2008 04:54:48
Diff at Trac: http://projects.jkraemer.net/acts_as_ferret/changeset/346
Changes:allow per query options, i.e. for specifying a special analyzer to be used for this query
Files:| ... | ...@@ -45,11 +45,13 @@ | |
| 45 | 45 | |
| 46 | 46 | desc "Stop the Ferret DRb server" |
| 47 | 47 | task :stop, :roles => :app do |
| 48 | run "cd #{current_path}; script/ferret_server -e #{rails_env} stop" | |
| 48 | rails_env = fetch(:rails_env, 'production') | |
| 49 | run "cd #{current_path}; script/ferret_server -e #{rails_env} stop || true" | |
| 49 | 50 | end |
| 50 | 51 | |
| 51 | 52 | desc "Start the Ferret DRb server" |
| 52 | 53 | task :start, :roles => :app do |
| 54 | rails_env = fetch(:rails_env, 'production') | |
| 53 | 55 | run "cd #{current_path}; script/ferret_server -e #{rails_env} start" |
| 54 | 56 | end |
| 55 | 57 | |
| ... | ...@@ -65,6 +67,7 @@ | |
| 65 | 67 | desc "Rebuild the Ferret index. See aaf_recipes.rb for instructions." |
| 66 | 68 | task :rebuild, :roles => :app do |
| 67 | 69 | rake = fetch(:rake, 'rake') |
| 70 | rails_env = fetch(:rails_env, 'production') | |
| 68 | 71 | indexes = fetch(:ferret_indexes, nil) |
| 69 | 72 | if indexes and indexes.any? |
| 70 | 73 | run "cd #{current_path}; RAILS_ENV=#{rails_env} INDEXES='#{indexes.join(' ')}' #{rake} ferret:rebuild" |
| ... | ...@@ -82,7 +82,7 @@ | |
| 82 | 82 | result = [] |
| 83 | 83 | stored_fields = determine_stored_fields options |
| 84 | 84 | |
| 85 | q = process_query(query) | |
| 85 | q = process_query(query, options) | |
| 86 | 86 | q = scope_query_to_models q, options[:models] #if shared? |
| 87 | 87 | logger.debug "query: #{query}\n-->#{q}" |
| 88 | 88 | s = searcher |
| ... | ...@@ -60,12 +60,24 @@ | |
| 60 | 60 | end |
| 61 | 61 | |
| 62 | 62 | # Parses the given query string into a Ferret Query object. |
| 63 | def process_query(query) | |
| 64 | # work around ferret bug in #process_query (doesn't ensure the | |
| 65 | # reader is open) | |
| 63 | def process_query(query, options = {}) | |
| 64 | return query unless String === query | |
| 66 | 65 | ferret_index.synchronize do |
| 67 | ferret_index.send(:ensure_reader_open) | |
| 68 | original_query = ferret_index.process_query(query) | |
| 66 | if options[:analyzer] | |
| 67 | # use per-query analyzer if present | |
| 68 | qp = Ferret::QueryParser.new ferret_index.instance_variable_get('@options').merge(options) | |
| 69 | reader = ferret_index.reader | |
| 70 | qp.fields = | |
| 71 | reader.fields unless options[:all_fields] || options[:fields] | |
| 72 | qp.tokenized_fields = | |
| 73 | reader.tokenized_fields unless options[:tokenized_fields] | |
| 74 | return qp.parse query | |
| 75 | else | |
| 76 | # work around ferret bug in #process_query (doesn't ensure the | |
| 77 | # reader is open) | |
| 78 | ferret_index.send(:ensure_reader_open) | |
| 79 | return ferret_index.process_query(query) | |
| 80 | end | |
| 69 | 81 | end |
| 70 | 82 | end |
| 71 | 83 |
| ... | ...@@ -57,13 +57,13 @@ | |
| 57 | 57 | end |
| 58 | 58 | |
| 59 | 59 | def search(query, options={}) |
| 60 | query = process_query(query) | |
| 60 | query = process_query(query, options) | |
| 61 | 61 | logger.debug "parsed query: #{query.to_s}" |
| 62 | 62 | searcher.search(query, options) |
| 63 | 63 | end |
| 64 | 64 | |
| 65 | 65 | def search_each(query, options = {}, &block) |
| 66 | query = process_query(query) | |
| 66 | query = process_query(query, options) | |
| 67 | 67 | searcher.search_each(query, options, &block) |
| 68 | 68 | end |
| 69 | 69 | |
| ... | ...@@ -92,7 +92,7 @@ | |
| 92 | 92 | @query_parser ||= Ferret::QueryParser.new(@options) |
| 93 | 93 | end |
| 94 | 94 | |
| 95 | def process_query(query) | |
| 95 | def process_query(query, options = {}) | |
| 96 | 96 | query = query_parser.parse(query) if query.is_a?(String) |
| 97 | 97 | return query |
| 98 | 98 | end |