Fulltext search in Sunspot uses Solr's dismax handler. The dismax handler is designed for parsing user-entered search phrases, and provides a good balance between functionality and error-proofing. A small subset of the normal boolean query syntax is parsed: in particular, well-matched quotation marks can be used to demarcate phrases, and the + and - operators can be used to require or exclude words respectively. All other Solr boolean syntax is escaped, and non-well-matched quotation marks are ignored. This is fairly consistent with the functionality users are accustomed to with search on the web and elsewhere.
Initiating a fulltext search is as simple as calling the
keywords method inside a search block:
Sunspot.search(Post) do keywords 'great pizza' end
The above search will return all documents where the words "great" and "pizza" appear somewhere in the fields indexed as
text. Note that the
keywords method should not be called multiple times in one search - successive calls will overwrite the previously specified keywords.
By default, fulltext search is performed against the union of all of the text fields configured for all of the classes under search. To specify which fields you'd like to search, pass the
Sunspot.search(Post) do keywords 'great pizza', :fields => [:title, :body] end
To request highlights to be returned with the search, use the
Sunspot.search(Post) do keywords 'great pizza', :highlight => true end
Note that for highlighting to work, the desired fields have to be set up with
:stored => true.
For all possible options, see "the API documentation":http://sunspot.github.com/sunspot/docs/Sunspot/DSL/StandardQuery.html.
Last edited by Jake Varghese,