Skip to content
Browse files

adding wildcard matching behavior to conditions

  • Loading branch information...
1 parent 4bbd535 commit c878f8a2f1e37033cf465eb434ec4cf2a0722067 @ryanb committed Jun 26, 2009
Showing with 26 additions and 0 deletions.
  1. +5 −0 CHANGELOG
  2. +10 −0 features/finding.feature
  3. +11 −0 lib/xapit/query_parsers/abstract_query_parser.rb
View
5 CHANGELOG
@@ -1,3 +1,8 @@
+* adding wildcard matching when asterisk exists at end of condition
+
+ :conditions => { :name => "foo*" }
+
+
*0.2.4* (June 23rd, 2009)
* only create/update record in index if it matches xapit find conditions
View
10 features/finding.feature
@@ -73,3 +73,13 @@ Scenario: Query Range of Integer
| Jack | 24 |
When I query "age" between 8 and 15
Then I should find records named "John, Jane"
+
+@focus
+Scenario: Query Partial Match
+ Given the following indexed records
+ | name | sirname |
+ | John | Jacobson |
+ | Jane | Niel |
+ | Jack | Striker |
+ When I query "name" matching "Ja*"
+ Then I should find records named "Jane, Jack"
View
11 lib/xapit/query_parsers/abstract_query_parser.rb
@@ -126,6 +126,8 @@ def condition_term(name, value)
if value.kind_of?(Range) && @member_class
position = @member_class.xapit_index_blueprint.position_of_field(name)
Xapian::Query.new(Xapian::Query::OP_VALUE_RANGE, position, Xapit.serialize_value(value.begin), Xapit.serialize_value(value.end))
+ elsif value.to_s.ends_with? "*"
+ wildcard_query(value, "X#{name}-")
else
if value.kind_of? Time
value = value.to_i
@@ -135,5 +137,14 @@ def condition_term(name, value)
"X#{name}-#{value.to_s.downcase}"
end
end
+
+ # Expands the wildcard in the term (just at the end) and returns a query
+ # which will match any term that starts with the given term.
+ def wildcard_query(term, prefix = "")
+ partial_term = term.sub(/\*$/, '') # remove asterisk at end if it exists
+ parser = Xapian::QueryParser.new
+ parser.database = Xapit::Config.database
+ parser.parse_query(partial_term, Xapian::QueryParser::FLAG_PARTIAL, prefix)
+ end
end
end

0 comments on commit c878f8a

Please sign in to comment.
Something went wrong with that request. Please try again.