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.



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

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

Instance Attribute Details

#base_queryObject



55
56
57
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 55

def base_query
  @base_query ||= initial_query
end

#extra_queriesObject

Returns the value of attribute extra_queries.



5
6
7
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 5

def extra_queries
  @extra_queries
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

#optionsObject (readonly)

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

#classesObject



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

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

#condition_termsObject



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

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

#current_pageObject



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_identifiersObject



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_termsObject



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_queryObject



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_stringsObject



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_termsObject



79
80
81
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 79

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

#offsetObject



38
39
40
# File 'lib/xapit/query_parsers/abstract_query_parser.rb', line 38

def offset
  per_page*(current_page-1)
end

#per_pageObject



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_queryObject



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

#queryObject



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_optionsObject



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_valuesObject



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_suggestionObject



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