Class: Xapit::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/xapit/query.rb

Overview

This class wraps a Xapian::Query for convenience purposes. You will likely not need to use this class unless you are trying to query the Xapian database directly. You may be looking for Xapit::Collection instead.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Query

Returns a new instance of Query.



8
9
10
11
# File 'lib/xapit/query.rb', line 8

def initialize(*args)
  @xapian_query = build_xapian_query(*args)
  @default_options = { :offset => 0, :sort_descending => false }
end

Instance Attribute Details

#default_optionsObject (readonly)

Returns the value of attribute default_options.



6
7
8
# File 'lib/xapit/query.rb', line 6

def default_options
  @default_options
end

#xapian_queryObject (readonly)

Returns the value of attribute xapian_query.



6
7
8
# File 'lib/xapit/query.rb', line 6

def xapian_query
  @xapian_query
end

Instance Method Details

#and_query(*args) ⇒ Object



13
14
15
# File 'lib/xapit/query.rb', line 13

def and_query(*args)
  merge_query(:and, *args)
end

#countObject



44
45
46
47
# File 'lib/xapit/query.rb', line 44

def count
  # a bit of a hack to get more accurate count estimate
  @count ||= matchset(:limit => Config.database.doccount).matches_estimated
end

#matches(options = {}) ⇒ Object



40
41
42
# File 'lib/xapit/query.rb', line 40

def matches(options = {})
  matchset(options).matches
end

#matchset(options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/xapit/query.rb', line 25

def matchset(options = {})
  options.reverse_merge!(default_options)
  enquire = Xapian::Enquire.new(Config.database)
  if options[:sort_by_values]
    sorter = Xapian::MultiValueSorter.new
    options[:sort_by_values].each do |sort_value|
      sorter.add(sort_value, !!options[:sort_descending])
    end
    enquire.set_sort_by_key_then_relevance(sorter)
  end
  enquire.collapse_key = options[:collapse_key] if options[:collapse_key]
  enquire.query = @xapian_query
  enquire.mset(options[:offset], options[:limit])
end

#not_query(*args) ⇒ Object



21
22
23
# File 'lib/xapit/query.rb', line 21

def not_query(*args)
  merge_query(:not, *args)
end

#or_query(*args) ⇒ Object



17
18
19
# File 'lib/xapit/query.rb', line 17

def or_query(*args)
  merge_query(:or, *args)
end