{"_id":"576472b50742400e00c0471a","user":"5633ec9b35355017003ca3f2","__v":10,"category":{"_id":"57646d30c176520e00ea8fe5","__v":0,"version":"56be3388be55991700c3ca10","project":"56be3387be55991700c3ca0d","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-06-17T21:35:44.598Z","from_sync":false,"order":0,"slug":"clients","title":"Client Integration"},"parentDoc":null,"project":"56be3387be55991700c3ca0d","version":{"_id":"56be3388be55991700c3ca10","project":"56be3387be55991700c3ca0d","__v":8,"createdAt":"2016-02-12T19:33:28.313Z","releaseDate":"2016-02-12T19:33:28.313Z","categories":["56be3389be55991700c3ca11","57646709b0a8be1900fcd0d8","5764671c89da831700590782","57646d30c176520e00ea8fe5","5764715d4f867c0e002bc8e3","57698fa2e93bfd190028815c","576c2af16c24681700c902da","5787da96b008c91900aae865"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-06-17T21:59:17.484Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":3,"body":"[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Installation\"\n}\n[/block]\nRails 3 applications use Bundler by default. If you are developing a Rails 2.3 application, please review [Using Bundler with Rails 2.3](http://gembundler.com/rails23.html) to ensure that your application is configured to use Bundler correctly.\n\nOnce you have set up your application to use Bundler, add the `sunspot_rails` gem to your `Gemfile`.\n\n```ruby\ngem 'sunspot_rails', '~> 1.2.1'\n```\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"gem 'sunspot_rails', '~>1.2.1'\",\n      \"language\": \"ruby\",\n      \"name\": null\n    }\n  ]\n}\n[/block]\nRun `bundle install` to install Sunspot, and its dependencies, into your local environment.\n\n## Installing Sunspot with Rails 2.3\n\nAs of this writing, the current release of Sunspot is version 1.2.1. Sunspot provides a Rails plugin as a gem, named `sunspot_rails`.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Configuration\"\n}\n[/block]\nBy default, Sunspot 1.2.1 supports the `WEBSOLR_URL` environment variable used by your Heroku application in production.\n\nIf you would like more fine-grained control over which Solr servers you are using in different environments, you may run `script/generate sunspot` to create a Sunspot configuration file at `config/sunspot.yml`.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Using Sunspot\"\n}\n[/block]\nWith Sunspot you configure your models for searching and indexing using a Ruby DSL. By default, your records are automatically indexed when they are created and updated, and removed from the index when destroyed.\n\n## Indexing Models\n\nHere is a simple example of using Sunspot’s `searchable` block and DSL to configure an ActiveRecord model.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"class Post < ActiveRecord::Base\\n  searchable do\\n    text    :title\\n    text    :body\\n    string  :permalink\\n    integer :category_id\\n    time    :published_at\\n  end\\nend\",\n      \"language\": \"ruby\",\n      \"name\": \"post.rb\"\n    }\n  ]\n}\n[/block]\n\nTo learn more, refer to the following article at the [Sunspot wiki](http://wiki.github.com/outoftime/sunspot):\n* [Setting up classes for search and indexing](http://wiki.github.com/outoftime/sunspot/setting-up-classes-for-search-and-indexing)\n\n## Searching\n\nTo search the model in the above example, you may use something like the following:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \":::at:::search = Post.search { keywords 'hello' }\\n@posts  = @search.results\",\n      \"language\": \"ruby\"\n    }\n  ]\n}\n[/block]\n(If your model already defines a `search` method, you may use the `solr_search` method instead, for which `search` is an alias.)\n\nSunspot exposes the full functionality of Solr. To learn more about searching your models, refer to the following articles at the Sunspot wiki:\n\n* [Full-text search with sunspot](http://wiki.github.com/outoftime/sunspot/fulltext-search)\n* [Sunspot search options](http://wiki.github.com/outoftime/sunspot/working-with-search)\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Rake Tasks\"\n}\n[/block]\nSunspot provides Rake tasks to start and stop a local Solr server for development and testing. In order to use these Rake tasks, add the following line to your application’s `Rakefile`:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"require 'sunspot/rails/tasks'\",\n      \"language\": \"ruby\"\n    }\n  ]\n}\n[/block]\nYou may wish to familiarize yourself with the available tasks by running `rake -T sunspot`.\n\n## Running a local Solr server with Sunspot\n\nTo start and stop a local Solr server for development, run the following rake tasks:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"rake sunspot:solr:start\\nrake sunspot:solr:stop\",\n      \"language\": \"ruby\"\n    }\n  ]\n}\n[/block]\n\n## Re-indexing Data\n\nIf you are adding Websolr to an application with existing data in your development or production environment, you will need to “re-index” your data. Likewise, if you make changes to a model’s `searchable` configuration, or change your index’s configuration at the [Websolr control panel](http://websolr.com/slices), you will need to reindex for your changes to take effect.\n\nIn order to reindex your production data, you may run a command similar to the following from your application’s directory:\n\n```\n$ heroku run rake sunspot:reindex\n```\n\nIf you are indexing a large number of documents, or your models us a lot of memory, you may need to reindex in batches smaller than Sunspot’s default of 50. We recommend starting small and gradually experimenting to find the best results. To reindex with a batch size of 10, use the following:\n\n```\n$ heroku run rake sunspot:reindex[10]\n```\n\nRefer to `rake -T sunspot` to see the usage for the reindex task.","excerpt":"Learn how to integrate your application with Sunspot and Websolr.","slug":"sunspot","type":"basic","title":"Sunspot"}

Sunspot

Learn how to integrate your application with Sunspot and Websolr.

[block:api-header] { "type": "basic", "title": "Installation" } [/block] Rails 3 applications use Bundler by default. If you are developing a Rails 2.3 application, please review [Using Bundler with Rails 2.3](http://gembundler.com/rails23.html) to ensure that your application is configured to use Bundler correctly. Once you have set up your application to use Bundler, add the `sunspot_rails` gem to your `Gemfile`. ```ruby gem 'sunspot_rails', '~> 1.2.1' ``` [block:code] { "codes": [ { "code": "gem 'sunspot_rails', '~>1.2.1'", "language": "ruby", "name": null } ] } [/block] Run `bundle install` to install Sunspot, and its dependencies, into your local environment. ## Installing Sunspot with Rails 2.3 As of this writing, the current release of Sunspot is version 1.2.1. Sunspot provides a Rails plugin as a gem, named `sunspot_rails`. [block:api-header] { "type": "basic", "title": "Configuration" } [/block] By default, Sunspot 1.2.1 supports the `WEBSOLR_URL` environment variable used by your Heroku application in production. If you would like more fine-grained control over which Solr servers you are using in different environments, you may run `script/generate sunspot` to create a Sunspot configuration file at `config/sunspot.yml`. [block:api-header] { "type": "basic", "title": "Using Sunspot" } [/block] With Sunspot you configure your models for searching and indexing using a Ruby DSL. By default, your records are automatically indexed when they are created and updated, and removed from the index when destroyed. ## Indexing Models Here is a simple example of using Sunspot’s `searchable` block and DSL to configure an ActiveRecord model. [block:code] { "codes": [ { "code": "class Post < ActiveRecord::Base\n searchable do\n text :title\n text :body\n string :permalink\n integer :category_id\n time :published_at\n end\nend", "language": "ruby", "name": "post.rb" } ] } [/block] To learn more, refer to the following article at the [Sunspot wiki](http://wiki.github.com/outoftime/sunspot): * [Setting up classes for search and indexing](http://wiki.github.com/outoftime/sunspot/setting-up-classes-for-search-and-indexing) ## Searching To search the model in the above example, you may use something like the following: [block:code] { "codes": [ { "code": "@search = Post.search { keywords 'hello' }\n@posts = @search.results", "language": "ruby" } ] } [/block] (If your model already defines a `search` method, you may use the `solr_search` method instead, for which `search` is an alias.) Sunspot exposes the full functionality of Solr. To learn more about searching your models, refer to the following articles at the Sunspot wiki: * [Full-text search with sunspot](http://wiki.github.com/outoftime/sunspot/fulltext-search) * [Sunspot search options](http://wiki.github.com/outoftime/sunspot/working-with-search) [block:api-header] { "type": "basic", "title": "Rake Tasks" } [/block] Sunspot provides Rake tasks to start and stop a local Solr server for development and testing. In order to use these Rake tasks, add the following line to your application’s `Rakefile`: [block:code] { "codes": [ { "code": "require 'sunspot/rails/tasks'", "language": "ruby" } ] } [/block] You may wish to familiarize yourself with the available tasks by running `rake -T sunspot`. ## Running a local Solr server with Sunspot To start and stop a local Solr server for development, run the following rake tasks: [block:code] { "codes": [ { "code": "rake sunspot:solr:start\nrake sunspot:solr:stop", "language": "ruby" } ] } [/block] ## Re-indexing Data If you are adding Websolr to an application with existing data in your development or production environment, you will need to “re-index” your data. Likewise, if you make changes to a model’s `searchable` configuration, or change your index’s configuration at the [Websolr control panel](http://websolr.com/slices), you will need to reindex for your changes to take effect. In order to reindex your production data, you may run a command similar to the following from your application’s directory: ``` $ heroku run rake sunspot:reindex ``` If you are indexing a large number of documents, or your models us a lot of memory, you may need to reindex in batches smaller than Sunspot’s default of 50. We recommend starting small and gradually experimenting to find the best results. To reindex with a batch size of 10, use the following: ``` $ heroku run rake sunspot:reindex[10] ``` Refer to `rake -T sunspot` to see the usage for the reindex task.