Created
March 27, 2012 11:45
-
-
Save d11wtq/2215235 to your computer and use it in GitHub Desktop.
Brainstorming Sphinx 2 gem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# Using an index defined in code for model abstraction. | |
## | |
class PostsIndex | |
include Oedipus::RTIndex | |
connection 'sphinx.site.com:3312' | |
index_name :posts_rt | |
record do | |
attribute :id, Integer | |
attribute :title, String | |
attribute :body, String | |
attribute :post_count, Integer | |
end | |
end | |
# reading | |
posts = PostsIndex.search( | |
"bobcats", | |
:post_count => 5..10, | |
:order => {:post_count => :desc}, | |
:limit => 30, | |
:offset => 120 | |
) | |
# => [<PostIndex::Record>, ...] (a recordset that knows about full counts) | |
# faceted search (assumes inheritance of main query) | |
posts = PostsIndex.faceted_search( | |
:query => ["bobcats", { :post_count => 5...10, :order => { :post_count => :desc } }], | |
:facets => { | |
:popular => { :post_count => 10...15 }, | |
:videos => "video", | |
:popular_videos => ["video", { :post_count => 10...Oedipus::SPHINX_INT_MAX }] | |
} | |
) | |
# => { :results => [<PostIndex::Record>, ...], :facets => { :popular => [<PostIndex::Record>, ...], ... } } | |
# multi search | |
posts = PostsIndex.faceted_search( | |
:bobcats => ["bobcat", { :post_count => 10...15 }], | |
:videos => "video", | |
:zebras => ["zebra", { :post_count => 10...15 }] | |
) | |
# => { :bobcats => [<PostIndex::Record>, ...], :videos => [<PostIndex::Record>, ...], ... } | |
# inserting | |
post = PostsIndex.create_record( ... ) | |
# updating | |
PostsIndex.update_record(7, :foo => "bar") | |
post.update(:foo => "bar") | |
# deleting | |
PostsIndex.delete_record(7) | |
post.delete | |
# -------------------------------------------------------------------------- | |
## | |
# Using the raw connection. | |
## | |
conn = Oedipus.connect('localhost:3312') | |
# reading | |
posts = conn[:posts_rt].search( | |
# ... same | |
) | |
# => { :total => 142, :count => 30, :records => [<Hash>, ...]} | |
# faceted search | |
posts = conn[:posts_rt].faceted_search( | |
# ... same | |
) | |
# => { :results => { ... }, :facets => { ... } } | |
# multi search | |
posts = conn[:posts_rt].multi_search( | |
# ... same | |
) | |
# => { :bobcats => { ... }, :zebras => { ... } } | |
# inserting | |
conn[:posts_rt].insert_record(:foo => "bar") | |
# => { :id => 7, :foo => "bar", ... } | |
# updating | |
conn[:posts_rt].update_record(7, :foo => "bar") | |
# => { :id => 7, :foo => "bar", ... } | |
# deleting | |
conn[:posts_rt].delete_record(7) | |
# => true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment