Class: Xapit::Server::Query

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

Instance Method Summary collapse

Constructor Details

#initialize(clauses) ⇒ Query

Returns a new instance of Query.



4
5
6
7
# File 'lib/xapit/server/query.rb', line 4

def initialize(clauses)
  @clauses = clauses
  @xapian_query = nil
end

Instance Method Details

#applied_facet_optionsObject



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/xapit/server/query.rb', line 57

def applied_facet_options
  facet_options = []
  @clauses.each do |clause|
    if clause[:with_facets]
      clause[:with_facets].each do |identifier|
        facet_options << facet_option(identifier)
      end
    end
  end
  facet_options
end

#dataObject



85
86
87
# File 'lib/xapit/server/query.rb', line 85

def data
  {:records => records, :facets => facets, :applied_facet_options => applied_facet_options, :total => total}
end

#facet_option(identifier) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/xapit/server/query.rb', line 69

def facet_option(identifier)
  match = self.class.new([{:in_classes => ["FacetOption"]}, {:where => {:id => identifier}}]).matches.first
  if match.nil?
    raise "Unable to find facet option for #{identifier}."
  else
    name, value = match.document.data.split('|||')
    {:id => identifier, :name => name, :value => value}
  end
end

#facetsObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/xapit/server/query.rb', line 35

def facets
  facets = {}
  enquire = Xapian::Enquire.new(Xapit.database.xapian_database)
  enquire.query = xapian_query
  spies = facet_spies
  spies.values.each do |spy|
    enquire.add_matchspy(spy)
  end
  enquire.mset(0, 200)
  spies.each do |attribute, spy|
    values = {}
    spy.values.map do |spy_value|
      spy_value.term.split("\3").each do |term| # used to support multiple facet values
        values[term] ||= 0
        values[term] += spy_value.termfreq.to_i
      end
    end
    facets[attribute] = values.map { |value, count| {:value => value, :count => count} }
  end
  facets
end

#matchesObject



9
10
11
12
13
14
# File 'lib/xapit/server/query.rb', line 9

def matches
  enquire = Xapian::Enquire.new(Xapit.database.xapian_database)
  enquire.query = xapian_query
  enquire.set_sort_by_key_then_relevance(sorter, false) if sorter
  enquire.mset((page.to_i-1)*per_page.to_i, per_page.to_i).matches
end

#recordsObject



16
17
18
19
20
21
# File 'lib/xapit/server/query.rb', line 16

def records
  matches.map do |match|
    class_name, id = match.document.data.split('-')
    {:class => class_name, :id => id, :relevance => match.percent}
  end
end

#spelling_suggestionObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/xapit/server/query.rb', line 23

def spelling_suggestion
  text = @clauses.map { |clause| clause[:search] }.compact.join(" ")
  if [text, *text.scan(/\w+/)].all? { |term| term_suggestion(term).nil? }
    nil
  else
    return term_suggestion(text) unless term_suggestion(text).to_s.empty?
    text.downcase.gsub(/\w+/) do |term|
      term_suggestion(term) || term
    end
  end
end

#totalObject



79
80
81
82
83
# File 'lib/xapit/server/query.rb', line 79

def total
  enquire = Xapian::Enquire.new(Xapit.database.xapian_database)
  enquire.query = xapian_query
  enquire.mset(0, Xapit.database.xapian_database.doccount).matches_estimated
end