Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

branch: master

This branch is even with TheSkunkworx:master

README.rdoc

SolrMapper

A Ruby Object Document Mapper for the Apache Foundation's Solr search platform

Installing

gem install solr_mapper

Examples

Model

require 'solr_mapper'
include SolrMapper

class Stuff
  include SolrDocument
  bind_service_url 'http://localhost:8080/solr/stuff'
  limit_page_size 25

  belongs_to :thing
end

class Widget
  include SolrDocument
  bind_service_url 'http://localhost:8080/solr/widget'
  limit_page_size 15

  belongs_to :thing
end

class Thing
  include SolrDocument

  # base urls can also be defined by rails environments
  bind_service_url({
    :development => 'http://somehost/solr_thing',
    :test => 'http://localhost:8080/solr_thing',
    :production => 'http://livebox/solr/thing'
  })

  limit_page_size 10

  has_many :stuffs
  has_one :widget
end

Auto Generated IDs

as of 0.1.9 you can cause a document to create a UUID id
for new objects with: 

class Stuff
  include SolrDocument

  # tells solr_mapper to generate the id
  auto_generate_id

  bind_service_url 'http://localhost:8080/solr/stuff'
end

Reading

thing = Thing.find(1)

puts thing.widget.name

thing.stuffs.each do |stuff|
  puts stuff.name
end

Querying

# freetext-style search
searched_things = Thing.query('sample')

# a more structured query with a hash
queried_things = Thing.query({:upc_code => '1234567'})

# direct solr search
searched_things = Thing.query('name:sample')

Writing

thing = Thing.new({:_id => 2, :content => 'sample content'})
thing.save()

Writing Related Objects

thing = Thing.new({:_id => 2, :content => 'sample content'})
thing.stuffs << Stuff.new({:_id => 10, :content => 'sample stuff'})

Paged Queries

# will_paginate page returned.  :rows overrides the @per_page setting
# with a search string
page = Thing.paginate('*:*', :rows => 5)
puts page.total_pages
puts page.count  

# with a structured hash search
page = Thing.paginate({:name => 'Sample Name'}, {:rows => 5})
puts page.total_pages
puts page.count

Deleting

# destroys thing from index
Thing.find(1).destroy

stuff = Stuffs.find(3)
# deletes reference and destroys stuff from index
Thing.find(2).stuffs.destroy(stuff)

Copyright

Copyright 2010 The Skunkworx.

Something went wrong with that request. Please try again.