Class: Xapit::AbstractQueryParser

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

Direct Known Subclasses

ClassicQueryParser, SimpleQueryParser

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ AbstractQueryParser

Returns a new instance of AbstractQueryParser.



6
7
8
9
10
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 6

def initialize(*args)
  @options = args.extract_options!
  @member_class = args[0]
  @search_text = args[1].to_s
end

Instance Attribute Details

#base_queryObject



45
46
47
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 45

def base_query
  @base_query ||= initial_query
end

#member_classObject (readonly)

Returns the value of attribute member_class.



3
4
5
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 3

def member_class
  @member_class
end

Instance Method Details

#classesObject



66
67
68
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 66

def classes
  (@options[:classes] || [@member_class]).compact
end

#condition_termsObject



70
71
72
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 70

def condition_terms
  parse_conditions(@options[:conditions])
end

#current_pageObject



20
21
22
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 20

def current_page
  @options[:page] ? @options[:page].to_i : 1
end

#facet_identifiersObject



88
89
90
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 88

def facet_identifiers
  @options[:facets].kind_of?(String) ? @options[:facets].split('-') : (@options[:facets] || [])
end

#facet_termsObject



78
79
80
81
82
83
84
85
86
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 78

def facet_terms
  if @options[:facets]
    facet_identifiers.map do |identifier|
      "F#{identifier}"
    end
  else
    []
  end
end

#initial_queryObject



49
50
51
52
53
54
55
56
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 49

def initial_query
  query = Query.new(initial_query_strings, :or)
  query.default_options[:offset] = offset
  query.default_options[:limit] = per_page
  query.default_options[:sort_by_values] = sort_by_values
  query.default_options[:sort_descending] = @options[:descending]
  query
end

#initial_query_stringsObject



58
59
60
61
62
63
64
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 58

def initial_query_strings
  if classes.empty?
    [""]
  else
    classes.map { |klass| "C#{klass.name}" }
  end
end

#not_condition_termsObject



74
75
76
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 74

def not_condition_terms
  parse_conditions(@options[:not_conditions])
end

#offsetObject



28
29
30
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 28

def offset
  per_page*(current_page-1)
end

#per_pageObject



24
25
26
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 24

def per_page
  @options[:per_page] ? @options[:per_page].to_i : 20
end

#queryObject



12
13
14
15
16
17
18
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 12

def query
  if (@search_text.split + condition_terms + not_condition_terms + facet_terms).empty?
    base_query
  else
    @query ||= base_query.and_query(xapian_query_from_text(@search_text)).and_query(condition_terms + facet_terms).not_query(not_condition_terms)
  end
end

#sort_by_valuesObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 32

def sort_by_values
  if @options[:order] && @member_class
    index = @member_class.xapit_index_blueprint
    if @options[:order].kind_of? Array
      @options[:order].map do |attribute|
        index.position_of_sortable(attribute)
      end
    else
      [index.position_of_sortable(@options[:order])]
    end
  end
end

#spelling_suggestionObject



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 92

def spelling_suggestion
  raise "Spelling has been disabled. Enable spelling in Xapit.setup." unless Config.spelling?
  if [@search_text, *@search_text.scan(/\w+/)].all? { |term| term_suggestion(term).nil? }
    nil
  else
    return term_suggestion(@search_text) unless term_suggestion(@search_text).blank?
    @search_text.downcase.gsub(/\w+/) do |term|
      term_suggestion(term) || term
    end
  end
end

#term_suggestion(term) ⇒ Object



104
105
106
107
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 104

def term_suggestion(term)
  suggestion = Config.database.get_spelling_suggestion(term.downcase)
  suggestion.blank? ? nil : suggestion
end