class XapianFu::QueryParser
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.
Attributes
The database that this query is agains, used for setting up fields, stemming, stopping and spelling.
The default operation when combining search terms. Defaults to :and
The stemming strategy to use when generating terms from a query. Defaults to :some
Public Class Methods
# File lib/xapian_fu/query_parser.rb 65 def initialize(options = { }) 66 @options = { 67 :stemming_strategy => :some, 68 :default_op => :and 69 }.merge(options) 70 self.stemming_strategy = @options[:stemming_strategy] 71 self.default_op = @options[:default_op] 72 self.database = @options[:database] 73 end
Public Instance Methods
Return the query string with any spelling corrections made
# File lib/xapian_fu/query_parser.rb 89 def corrected_query 90 query_parser.get_corrected_query_string 91 end
An array of field names that will be recognised in this query
# File lib/xapian_fu/query_parser.rb 204 def fields 205 if @options[:fields].is_a? Array 206 @options[:fields] 207 elsif database.is_a? XapianFu::XapianDb 208 database.fields 209 else 210 [] 211 end 212 end
Return an array of symbols representing the flags set for this query parser
# File lib/xapian_fu/query_parser.rb 152 def flags 153 if @flags 154 @flags 155 else 156 valid_flags = [:boolean, :boolean_anycase, :wildcards, :lovehate, :spelling, :pure_not, :synonyms, :phrase] 157 @flags = valid_flags.delete_if { |vf| not @options[vf] } 158 end 159 end
Parse the given query and return a Xapian::Query object Accepts either a string or a special query
# File lib/xapian_fu/query_parser.rb 77 def parse_query(q) 78 case q 79 when :all 80 Xapian::Query.new("") 81 when :nothing 82 Xapian::Query.new() 83 else 84 query_parser.parse_query(q, xapian_flags) 85 end 86 end
The current Xapian::QueryParser object
# File lib/xapian_fu/query_parser.rb 94 def query_parser 95 if @query_parser 96 @query_parser 97 else 98 qp = Xapian::QueryParser.new 99 qp.database = xapian_database if xapian_database 100 qp.stopper = database.stopper if database && database.stopper 101 qp.stemmer = database.stemmer if database && database.stemmer 102 qp.default_op = xapian_default_op 103 qp.stemming_strategy = xapian_stemming_strategy 104 105 fields.each do |name, type| 106 next if database && database.boolean_fields.include?(name) 107 qp.add_prefix(name.to_s.downcase, "X" + name.to_s.upcase) 108 end 109 110 database.boolean_fields.each do |name| 111 qp.add_boolean_prefix(name.to_s.downcase, "X#{name.to_s.upcase}") 112 end if database 113 114 database.sortable_fields.each do |field, opts| 115 prefix, string = nil 116 117 if opts[:range_postfix] 118 prefix = false 119 string = opts[:range_postfix] 120 else 121 prefix = true 122 string = opts[:range_prefix] || "#{field.to_s.downcase}:" 123 end 124 125 qp.add_valuerangeprocessor(Xapian::NumberValueRangeProcessor.new( 126 XapianDocValueAccessor.value_key(field), 127 string, 128 prefix 129 )) 130 end if database && @options.fetch(:ranges, true) 131 132 @query_parser = qp 133 end 134 end
Return the available Xapian::Database for use in the query parser
# File lib/xapian_fu/query_parser.rb 193 def xapian_database 194 if database.is_a? XapianFu::XapianDb 195 database.ro 196 elsif database.is_a? Xapian::Database 197 database 198 else 199 nil 200 end 201 end
Return a Xapian::Query constant for this query parser's default operation
# File lib/xapian_fu/query_parser.rb 178 def xapian_default_op 179 case default_op 180 when :and_maybe 181 Xapian::Query::OP_AND_MAYBE 182 when :or 183 Xapian::Query::OP_OR 184 when :phrase 185 Xapian::Query::OP_PHRASE 186 when :and 187 Xapian::Query::OP_AND 188 end 189 end
Return a Xapian::QueryParser flag mask representing the flags set for this query parser
# File lib/xapian_fu/query_parser.rb 163 def xapian_flags 164 qflags = 0 165 qflags |= Xapian::QueryParser::FLAG_BOOLEAN if flags.include?(:boolean) 166 qflags |= Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE if flags.include?(:boolean_anycase) 167 qflags |= Xapian::QueryParser::FLAG_WILDCARD if flags.include?(:wildcards) 168 qflags |= Xapian::QueryParser::FLAG_LOVEHATE if flags.include?(:lovehate) 169 qflags |= Xapian::QueryParser::FLAG_SPELLING_CORRECTION if flags.include?(:spelling) 170 qflags |= Xapian::QueryParser::FLAG_PURE_NOT if flags.include?(:pure_not) 171 qflags |= Xapian::QueryParser::FLAG_AUTO_SYNONYMS if flags.include?(:synonyms) 172 qflags |= Xapian::QueryParser::FLAG_PHRASE if flags.include?(:phrase) 173 qflags 174 end
The Xapian::QueryParser constant for this parsers stemming strategy
# File lib/xapian_fu/query_parser.rb 137 def xapian_stemming_strategy 138 case stemming_strategy 139 when :all 140 Xapian::QueryParser::STEM_ALL 141 when :some 142 Xapian::QueryParser::STEM_SOME 143 when :none 144 when false 145 when nil 146 Xapian::QueryParser::STEM_NONE 147 end 148 end