Module: Zena::Use::Search::NodeClassMethods
- Included in:
- Node
- Defined in:
- lib/zena/use/search.rb
Instance Method Summary collapse
-
#match_query(query, options = {}) ⇒ Object
Return a hash to do a fulltext query.
-
#search_index(params, options = {}) ⇒ Object
Execute an index search using query builder.
- #search_records(query, options = {}) ⇒ Object
-
#search_text(query, options = {}) ⇒ Object
Execute a fulltext search using default fulltext support from the database (MyISAM on MySQL).
Instance Method Details
#match_query(query, options = {}) ⇒ Object
Return a hash to do a fulltext query.
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 |
# File 'lib/zena/use/search.rb', line 6 def match_query(query, = {}) node = .delete(:node) if query == '.' && node return .merge( :conditions => ["parent_id = ?",node[:id]], :order => 'zip ASC' ) elsif !query.blank? match = sanitize_sql(["vs.idx_text_high LIKE ?", "%#{query.gsub('*','%')}%"]) select = "nodes.*" case Zena::Db.adapter when 'postgresql' select = "DISTINCT ON (nodes.zip) #{select}" group = nil else group = 'nodes.id' end return .merge( :select => select, :joins => "INNER JOIN versions AS vs ON vs.node_id = nodes.id AND vs.status >= #{Zena::Status::Pub}", :conditions => match, :group => group, :order => "zip DESC") # new items first else # error return .merge(:conditions => '0') end end |
#search_index(params, options = {}) ⇒ Object
Execute an index search using query builder. Either provide a full query with ‘qb’ or ‘key’=‘value’ parameters.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/zena/use/search.rb', line 53 def search_index(params, = {}) count = (params.delete(:_find) || :all).to_sym node = .delete(:node) || current_site.root_node default = .delete(:default) unless query = params[:qb] query_args = [] params.each do |key, value| query_args << "#{key} = #{Zena::Db.quote(value)}" end query = "nodes where #{query_args.join(' and ')} in site" end res = node.find(count, query, .merge(:errors => true, :rubyless_helper => self, :default => default)) if res.kind_of?(::QueryBuilder::Error) raise ::QueryBuilder::Error.new("Error parsing query #{query.inspect} (#{res.})") else return res end end |
#search_records(query, options = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/zena/use/search.rb', line 36 def search_records(query, = {}) if per_page = .delete(:per_page) page = .delete(:page).to_i page = 1 if page < 1 search_records(query, .merge(:offset => (page - 1) * per_page, :limit => per_page)) else # Removed pagination clause or no pagination if query.kind_of?(Hash) search_index(query, ) else # TODO: should we parse :_find (all, first, count) here ? search_text(query, ) end end end |
#search_text(query, options = {}) ⇒ Object
Execute a fulltext search using default fulltext support from the database (MyISAM on MySQL).
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/zena/use/search.rb', line 78 def search_text(query, = {}) [:order] ||= 'zip desc' if offset = [:offset] limit = [:limit] || 20 Node.find(:all, match_query(query).merge(:offset => offset, :limit => limit, :order => [:order])) else # :default argument not used here .delete(:default) Node.find(:all, match_query(query, )) end end |