{"__v":11,"_id":"576461860742400e00c046e4","category":{"__v":0,"_id":"57698fa2e93bfd190028815c","project":"56be3387be55991700c3ca0d","version":"56be3388be55991700c3ca10","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-06-21T19:04:02.961Z","from_sync":false,"order":1,"slug":"integrations","title":"Guides"},"parentDoc":null,"project":"56be3387be55991700c3ca0d","user":"5633ec9b35355017003ca3f2","version":{"__v":8,"_id":"56be3388be55991700c3ca10","project":"56be3387be55991700c3ca0d","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-17T20:45:58.829Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":3,"body":"This is a quick start guide for getting started with the latest version of the Sunspot Solr client for Ruby. By the end of this guide, you should have working full-text search in your development environment, powered by a locally running Solr instance.\n\n## Installing Sunspot\n\nFirst, install the `sunspot_rails` gem in your `Gemfile`:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"gem 'sunspot_rails'\",\n      \"language\": \"ruby\",\n      \"name\": \"Gemfile\"\n    }\n  ]\n}\n[/block]\nNext, install the gem into your bundle by running `bundle install` on the command line.\n\n    $ bundle install\n\n## Starting Solr\n\nNext, you can start a local instance of Solr by using one of Sunspot's provided rake task.\n\n    $ rake sunspot:solr:start\n\nThis rake task also creates a `solr/conf` directory, which contains default configuration files for your local Solr instance, as well as a `solr/data` directory for the Solr index itself.\n\nYou should ignore the `solr/data` directory by adding the following to your `.gitignore` file:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"solr/data\",\n      \"language\": \"text\",\n      \"name\": \".gitignore\"\n    }\n  ]\n}\n[/block]\nWith a local instance of Solr up and running, it's time to configure your models to be searchable.\n\n## Indexing your models\n\nSunspot provides the `searchable` class methods for you to configure how your models are to be indexed. This lets you specify the fields you would like to have indexed, and the Solr data type you would like them to be indexed as.\n\nFor a simple example, consider a Post model with a `title` field and a `content` field. We will index these fields as Solr `text` fields.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"class Post < ActiveRecord::Base\\n  searchable do\\n    text :title\\n    text :content\\n  end\\nend\",\n      \"language\": \"ruby\"\n    }\n  ]\n}\n[/block]\nFor more information about data types, take a look at our guide to [[Solr data types]].\n\nWhenever you make changes to how your models are indexed, you will need to rebuild your index for any existing data in your database. To do so, run Sunspot's \"reindex\" rake task:\n\n    $ rake sunspot:reindex\n\nWith that, your data should now be indexed and ready to search.\n\n## Searching your models\n\nSunspot provides a Ruby-based DSL for generating sophisticated search queries. This method is called `solr_search` which is also aliased to `search` if your class does not already respond to that method. The `solr_search` method accepts a block, and returns a search result object that wraps the response from Solr.\n\nHere's a simple search against the Post class that we indexed above, running from the index action of the `PostsController`:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"class PostsController < ApplicationController\\n  def index\\n    :::at:::search = Post.search do\\n      keywords params[:query]\\n    end\\n    @posts = @search.results\\n  end\\nend\",\n      \"language\": \"ruby\"\n    }\n  ]\n}\n[/block]\n\n- The `keywords` method accepts a query string and performs a keyword-based search against your `text` fields.\n- The `solr_search` method returns a search object which wraps the entire response from Solr, including values for the query time, total number of results found, and so on.\n- The `@search` object provides a `results` method that fetches the full ActiveRecord objects that correspond to the results returned by Solr.\n\nFinally, showing your search results can be as simple as rendering that `@posts` variable like any other collection of ActiveRecord objects. Sunspot also supports WillPaginate.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<%= render @posts %>\",\n      \"language\": \"html\"\n    }\n  ]\n}\n[/block]\n### That's it!\n\nYou should now have a working search locally.\n\nFor more information, check out our [[Sunspot]] guides page.","excerpt":"Get up and running with Solr using the Sunspot Solr client for Ruby.","slug":"sunspot-solr-client-for-ruby","type":"basic","title":"Sunspot Solr client for Ruby"}

Sunspot Solr client for Ruby

Get up and running with Solr using the Sunspot Solr client for Ruby.

This is a quick start guide for getting started with the latest version of the Sunspot Solr client for Ruby. By the end of this guide, you should have working full-text search in your development environment, powered by a locally running Solr instance. ## Installing Sunspot First, install the `sunspot_rails` gem in your `Gemfile`: [block:code] { "codes": [ { "code": "gem 'sunspot_rails'", "language": "ruby", "name": "Gemfile" } ] } [/block] Next, install the gem into your bundle by running `bundle install` on the command line. $ bundle install ## Starting Solr Next, you can start a local instance of Solr by using one of Sunspot's provided rake task. $ rake sunspot:solr:start This rake task also creates a `solr/conf` directory, which contains default configuration files for your local Solr instance, as well as a `solr/data` directory for the Solr index itself. You should ignore the `solr/data` directory by adding the following to your `.gitignore` file: [block:code] { "codes": [ { "code": "solr/data", "language": "text", "name": ".gitignore" } ] } [/block] With a local instance of Solr up and running, it's time to configure your models to be searchable. ## Indexing your models Sunspot provides the `searchable` class methods for you to configure how your models are to be indexed. This lets you specify the fields you would like to have indexed, and the Solr data type you would like them to be indexed as. For a simple example, consider a Post model with a `title` field and a `content` field. We will index these fields as Solr `text` fields. [block:code] { "codes": [ { "code": "class Post < ActiveRecord::Base\n searchable do\n text :title\n text :content\n end\nend", "language": "ruby" } ] } [/block] For more information about data types, take a look at our guide to [[Solr data types]]. Whenever you make changes to how your models are indexed, you will need to rebuild your index for any existing data in your database. To do so, run Sunspot's "reindex" rake task: $ rake sunspot:reindex With that, your data should now be indexed and ready to search. ## Searching your models Sunspot provides a Ruby-based DSL for generating sophisticated search queries. This method is called `solr_search` which is also aliased to `search` if your class does not already respond to that method. The `solr_search` method accepts a block, and returns a search result object that wraps the response from Solr. Here's a simple search against the Post class that we indexed above, running from the index action of the `PostsController`: [block:code] { "codes": [ { "code": "class PostsController < ApplicationController\n def index\n @search = Post.search do\n keywords params[:query]\n end\n @posts = @search.results\n end\nend", "language": "ruby" } ] } [/block] - The `keywords` method accepts a query string and performs a keyword-based search against your `text` fields. - The `solr_search` method returns a search object which wraps the entire response from Solr, including values for the query time, total number of results found, and so on. - The `@search` object provides a `results` method that fetches the full ActiveRecord objects that correspond to the results returned by Solr. Finally, showing your search results can be as simple as rendering that `@posts` variable like any other collection of ActiveRecord objects. Sunspot also supports WillPaginate. [block:code] { "codes": [ { "code": "<%= render @posts %>", "language": "html" } ] } [/block] ### That's it! You should now have a working search locally. For more information, check out our [[Sunspot]] guides page.