mauriciozaffari/mongoid_search — pull request #27 by ajsharp (2011-10-19 21:25:18+00:00)
Title: Optimize regexification of search tokens

Currently, `Mongoid::Search` turns your search terms into a list of simple, two-way wildcard regular expressions. However, two-way wildcards are very difficult for the query optimizer to handle, so this change only makes it a one-way wild card. Here's an example, wherein the collection being queried only has 100,000 documents -- note the nscanned value.

```ruby
Person.collection.create_index '_keywords'

exp = Person.collection.find(:_keywords => {:$in => [/so/]}).explain
exp["nscanned"] # => 973496, almost 10 times the size of the collection
exp["millis"] # => 945
exp["nscannedObjects"] # => 4014
exp["n"] # => 4013

exp = Person.collection.find(:_keywords => {:$in => [/^so.*/]}).explain
exp["nscanned"] # => 4014, only scans one more document than we need
exp["millis"] # => 6
exp["nscannedObjects"] # => 4013
exp["n"] # => 4013
```