Class: Xapit::AbstractQueryParser
- Inherits:
-
Object
- Object
- Xapit::AbstractQueryParser
show all
- Defined in:
- lib/xapit/query_parsers/abstract_query_parser.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of AbstractQueryParser.
7
8
9
10
11
12
|
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 7
def initialize(*args)
@options = args.
@member_class = args[0]
@search_text = args[1].to_s
@extra_queries = []
end
|
Instance Attribute Details
#base_query ⇒ Object
55
56
57
|
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 55
def base_query
@base_query ||= initial_query
end
|
Returns the value of attribute extra_queries.
5
6
7
|
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 5
def
@extra_queries
end
|
#member_class ⇒ Object
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
|
#options ⇒ Object
Returns the value of attribute options.
3
4
5
|
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 3
def options
@options
end
|
Instance Method Details
#classes ⇒ Object
71
72
73
|
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 71
def classes
(@options[:classes] || [@member_class]).compact
end
|
#condition_terms ⇒ Object
75
76
77
|
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 75
def condition_terms
parse_conditions(@options[:conditions])
end
|
#current_page ⇒ Object
30
31
32
|
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 30
def current_page
@options[:page] ? @options[:page].to_i : 1
end
|
#facet_identifiers ⇒ Object
93
94
95
|
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 93
def facet_identifiers
@options[:facets].kind_of?(String) ? @options[:facets].split('-') : (@options[:facets] || [])
end
|
#facet_terms ⇒ Object
83
84
85
86
87
88
89
90
91
|
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 83
def facet_terms
if @options[:facets]
facet_identifiers.map do |identifier|
"F#{identifier}"
end
else
[]
end
end
|
#initial_query ⇒ Object
59
60
61
|
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 59
def initial_query
Query.new(initial_query_strings, :or)
end
|
#initial_query_strings ⇒ Object
63
64
65
66
67
68
69
|
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 63
def initial_query_strings
if classes.empty?
[""]
else
classes.map { |klass| "C#{klass.name}" }
end
end
|
#matchset(options = {}) ⇒ Object
114
115
116
|
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 114
def matchset(options = {})
query.matchset(query_options.merge(options))
end
|
#not_condition_terms ⇒ Object
79
80
81
|
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 79
def not_condition_terms
parse_conditions(@options[:not_conditions])
end
|
#offset ⇒ Object
38
39
40
|
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 38
def offset
per_page*(current_page-1)
end
|
#per_page ⇒ Object
34
35
36
|
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 34
def per_page
@options[:per_page] ? @options[:per_page].to_i : 20
end
|
#primary_query ⇒ Object
22
23
24
25
26
27
28
|
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 22
def primary_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
|
#query ⇒ Object
14
15
16
17
18
19
20
|
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 14
def query
if @extra_queries.blank?
primary_query
else
Query.new([primary_query] + @extra_queries, :or)
end
end
|
#query_options ⇒ Object
118
119
120
121
122
123
124
125
|
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 118
def query_options
{
:offset => offset,
:limit => per_page,
:sort_by_values => sort_by_values,
:sort_descending => @options[:descending]
}
end
|
#sort_by_values ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 42
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_suggestion ⇒ Object
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 97
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
109
110
111
112
|
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 109
def term_suggestion(term)
suggestion = Config.database.get_spelling_suggestion(term.downcase)
suggestion.blank? ? nil : suggestion
end
|