Module: Zena::Use::Search::NodeClassMethods

Included in:
Node
Defined in:
lib/zena/use/search.rb

Instance Method Summary collapse

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, options = {})
  node = options.delete(:node)
  if query == '.' && node
    return options.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 options.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 options.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, options = {})
  count   = (params.delete(:_find) || :all).to_sym
  node    = options.delete(:node) || current_site.root_node
  default = options.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, options.merge(:errors => true, :rubyless_helper => self, :default => default))

  if res.kind_of?(::QueryBuilder::Error)
    raise ::QueryBuilder::Error.new("Error parsing query #{query.inspect} (#{res.message})")
  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, options = {})
  if per_page = options.delete(:per_page)
    page = options.delete(:page).to_i
    page = 1 if page < 1
    search_records(query, options.merge(:offset => (page - 1) * per_page, :limit => per_page))
  else
    # Removed pagination clause or no pagination
    if query.kind_of?(Hash)
      search_index(query, options)
    else
      # TODO: should we parse :_find (all, first, count) here ?
      search_text(query, options)
    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, options = {})
  options[:order] ||= 'zip desc'
  if offset = options[:offset]
    limit = options[:limit] || 20
    Node.find(:all, match_query(query).merge(:offset => offset, :limit => limit, :order => options[:order]))
  else
    # :default argument not used here
    options.delete(:default)
    Node.find(:all, match_query(query, options))
  end
end