Class: XapianFu::QueryParser

Inherits:
Object
  • Object
show all
Defined in:
lib/xapian_fu/query_parser.rb

Overview

The XapianFu::QueryParser is responsible for building useful Xapian::QueryParser objects.

The :fields option specifies the fields allowed in the query. Settings :fields => [:name, :city] would allow searches such as "name:john city:Leeds" (assuming those fields were in the document when it was added to the database.) This options takes an array of symbols or strings representing the field names.

The :database option specifies the XapianFu::Database, necessary for calculating spelling corrections. The database's stemmer, stopper and field list will also be used.

The :default_op option specifies the search operator to be used when not specified. It takes the operations :or, :phrase, :and and :and_maybe. The default is :and. So for example, with the :or operation, a query "dog cat rabbit" will be parsed as "dog AND cat AND rabbit".

The :stemming_strategy option specifies how terms in the query should be stemmed. It accepts :some, :all or :none. The default is :some which is best for most situations. See the Xapian documentation for more details.

The :boolean option enables or disables boolean queries. Set to true or false.

The :boolean_anycase option enables or disables case-insensitive boolean queries. Set to true or false.

The :wildcards option enables or disables the use of wildcard terms in queries, such as "york*". Set to true or false.

The :lovehate option enables or disables the use of +/- operators in queries, such as "+mickey -mouse". Set to true or false.

The :spelling option enables or disables spelling correction on queries. Set to true or false. Requires the :database option.

The :pure_not option enables or disables the use of queries that only exclude terms, such as "NOT apples". Set to true or false.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = { }) ⇒ QueryParser



65
66
67
68
69
70
71
72
73
# File 'lib/xapian_fu/query_parser.rb', line 65

def initialize(options = { })
  @options = {
    :stemming_strategy => :some,
    :default_op => :and
  }.merge(options)
  self.stemming_strategy = @options[:stemming_strategy]
  self.default_op = @options[:default_op]
  self.database = @options[:database]
end

Instance Attribute Details

#databaseObject

The database that this query is agains, used for setting up fields, stemming, stopping and spelling.



63
64
65
# File 'lib/xapian_fu/query_parser.rb', line 63

def database
  @database
end

#default_opObject

The default operation when combining search terms. Defaults to :and



59
60
61
# File 'lib/xapian_fu/query_parser.rb', line 59

def default_op
  @default_op
end

#stemming_strategyObject

The stemming strategy to use when generating terms from a query. Defaults to :some



55
56
57
# File 'lib/xapian_fu/query_parser.rb', line 55

def stemming_strategy
  @stemming_strategy
end

Instance Method Details

#corrected_queryObject

Return the query string with any spelling corrections made



89
90
91
# File 'lib/xapian_fu/query_parser.rb', line 89

def corrected_query
  query_parser.get_corrected_query_string
end

#fieldsObject

An array of field names that will be recognised in this query



204
205
206
207
208
209
210
211
212
# File 'lib/xapian_fu/query_parser.rb', line 204

def fields
  if @options[:fields].is_a? Array
    @options[:fields]
  elsif database.is_a? XapianFu::XapianDb
    database.fields
  else
    []
  end
end

#flagsObject

Return an array of symbols representing the flags set for this query parser



152
153
154
155
156
157
158
159
# File 'lib/xapian_fu/query_parser.rb', line 152

def flags
  if @flags
    @flags
  else
    valid_flags = [:boolean, :boolean_anycase, :wildcards, :lovehate, :spelling, :pure_not, :synonyms, :phrase]
    @flags = valid_flags.delete_if { |vf| not @options[vf] }
  end
end

#parse_query(q) ⇒ Object

Parse the given query and return a Xapian::Query object Accepts either a string or a special query



77
78
79
80
81
82
83
84
85
86
# File 'lib/xapian_fu/query_parser.rb', line 77

def parse_query(q)
  case q
  when :all
    Xapian::Query.new("")
  when :nothing
    Xapian::Query.new()
  else
    query_parser.parse_query(q, xapian_flags)
  end
end

#query_parserObject

The current Xapian::QueryParser object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/xapian_fu/query_parser.rb', line 94

def query_parser
  if @query_parser
    @query_parser
  else
    qp = Xapian::QueryParser.new
    qp.database = xapian_database if xapian_database
    qp.stopper = database.stopper if database && database.stopper
    qp.stemmer = database.stemmer if database && database.stemmer
    qp.default_op = xapian_default_op
    qp.stemming_strategy = xapian_stemming_strategy

    fields.each do |name, type|
      next if database && database.boolean_fields.include?(name)
      qp.add_prefix(name.to_s.downcase, "X" + name.to_s.upcase)
    end

    database.boolean_fields.each do |name|
      qp.add_boolean_prefix(name.to_s.downcase, "X#{name.to_s.upcase}")
    end if database

    database.sortable_fields.each do |field, opts|
      prefix, string = nil

      if opts[:range_postfix]
        prefix = false
        string = opts[:range_postfix]
      else
        prefix = true
        string = opts[:range_prefix] || "#{field.to_s.downcase}:"
      end

      qp.add_valuerangeprocessor(Xapian::NumberValueRangeProcessor.new(
        XapianDocValueAccessor.value_key(field),
        string,
        prefix
      ))
    end if database && @options.fetch(:ranges, true)

    @query_parser = qp
  end
end

#xapian_databaseObject

Return the available Xapian::Database for use in the query parser



193
194
195
196
197
198
199
200
201
# File 'lib/xapian_fu/query_parser.rb', line 193

def xapian_database
  if database.is_a? XapianFu::XapianDb
    database.ro
  elsif database.is_a? Xapian::Database
    database
  else
    nil
  end
end

#xapian_default_opObject

Return a Xapian::Query constant for this query parser's default operation



178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/xapian_fu/query_parser.rb', line 178

def xapian_default_op
  case default_op
  when :and_maybe
    Xapian::Query::OP_AND_MAYBE
  when :or
    Xapian::Query::OP_OR
  when :phrase
    Xapian::Query::OP_PHRASE
  when :and
    Xapian::Query::OP_AND
  end
end

#xapian_flagsObject

Return a Xapian::QueryParser flag mask representing the flags set for this query parser



163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/xapian_fu/query_parser.rb', line 163

def xapian_flags
  qflags = 0
  qflags |= Xapian::QueryParser::FLAG_BOOLEAN if flags.include?(:boolean)
  qflags |= Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE if flags.include?(:boolean_anycase)
  qflags |= Xapian::QueryParser::FLAG_WILDCARD if flags.include?(:wildcards)
  qflags |= Xapian::QueryParser::FLAG_LOVEHATE if flags.include?(:lovehate)
  qflags |= Xapian::QueryParser::FLAG_SPELLING_CORRECTION if flags.include?(:spelling)
  qflags |= Xapian::QueryParser::FLAG_PURE_NOT if flags.include?(:pure_not)
  qflags |= Xapian::QueryParser::FLAG_AUTO_SYNONYMS if flags.include?(:synonyms)
  qflags |= Xapian::QueryParser::FLAG_PHRASE if flags.include?(:phrase)
  qflags
end

#xapian_stemming_strategyObject

The Xapian::QueryParser constant for this parsers stemming strategy



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/xapian_fu/query_parser.rb', line 137

def xapian_stemming_strategy
  case stemming_strategy
  when :all
    Xapian::QueryParser::STEM_ALL
  when :some
    Xapian::QueryParser::STEM_SOME
  when :none
  when false
  when nil
    Xapian::QueryParser::STEM_NONE
  end
end