{"_id":"57646db7c176520e00ea8fea","parentDoc":null,"user":"5633ec9b35355017003ca3f2","__v":4,"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"},"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:37:59.270Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"settings":"","results":{"codes":[]},"auth":"required","params":[],"url":""},"isReference":false,"order":4,"body":"RSolr is a Ruby client for Solr. It is designed to be a simple, low-level wrapper that provides direct access to the Solr API. As such, its use encourages familiarity with the Solr API, which in turn offers simple, direct access to Solr.\n\nThis document provides a basic overview of common RSolr usage. You can find RSolr's comprehensive official documentation at its source repository on GitHub at:\n\n-[ https://github.com/mwmitchell/rsolr]( https://github.com/mwmitchell/rsolr) \n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Installation\"\n}\n[/block]\nRSolr is distributed as a Ruby Gem. To install it using the command-line RubyGems tool, simply run:\n\n```\ngem install rsolr\n```\n\nTo use RSolr with Bundler in your Rails, Sinatra or other Ruby application, you can include the following in your Gemfile:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"gem 'rsolr'\",\n      \"language\": \"ruby\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Basic Usage\"\n}\n[/block]\nWhen using RSolr, you need to specify the URL of the Solr index which you are connecting to. This can be done by passing a `:url` option to the `RSolr.connect` method, which returns a `RSolr::Client` object for you to use in indexing and searching your data.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"rsolr = RSolr.connect :url => 'http://index.websolr.com/solr/a1b2c3d4e5f'\",\n      \"language\": \"ruby\"\n    }\n  ]\n}\n[/block]\nWe will refer back to this `rsolr` object throughout the rest of the documentation.\n\n\n## Indexing & updating your data\n\nThe RSolr client's `add` method accepts a simple Ruby hash, which it will post to Solr as a document to index.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"rsolr.add(:id => 1, :name => \\\"Hello, world\\\", :body => \\\"Two roads diverged in a wood...\\\")\",\n      \"language\": \"ruby\"\n    }\n  ]\n}\n[/block]\nIt can also accept an array of hashes to index:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"rsolr.add([\\n  { :id => 1, :name => \\\"Hello, world\\\", :body => \\\"Two roads diverged in a wood...\\\" },\\n  { :id => 2, :name => \\\"Adventurous\\\", :body => \\\"I shall be telling this with a sigh...\\\" }\\n])\",\n      \"language\": \"ruby\"\n    }\n  ]\n}\n[/block]\nYou can issue a commit using the `commit` method:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"rsolr.commit\",\n      \"language\": \"ruby\"\n    }\n  ]\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"For performance reasons, indexes on Websolr ignore client-issued commits, deferring instead to a pre-configured autoCommit policy.\"\n}\n[/block]\n## Searching your documents\n\nThe RSolr client provides a `select` method which sends requests to the Solr `/select` handler. It accepts a hash of parameters, which it serializes into the query string of its request.\n\nHere is a simple example, using the `q` param to perform a keyword query:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"search = rsolr.select :params => { :q => \\\"keywords\\\" }\",\n      \"language\": \"ruby\"\n    }\n  ]\n}\n[/block]\nNote that in this example, the query is performed according to the query parser settings defined in `solrconfig.xml` and potentially your default query field specified in `schema.xml`. Here is a more explicit search using the [[DisMax query parser|solr-features-dismax-query-parser]] to search our example `name` and `body` fields.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"search = rsolr.select :params => { :q => \\\"keywords\\\", :defType => \\\"dismax\\\", :qf => \\\"name body\\\" }\",\n      \"language\": \"ruby\",\n      \"name\": null\n    }\n  ]\n}\n[/block]\nThe return value of this search is a Ruby hash corresponding to the response from the Solr server. Here is a sample search response:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"{\\n  'responseHeader' => {\\n    'status' => 0,\\n    'QTime' => 2,\\n    'params' => {\\n      'wt' => 'ruby'\\n    }\\n  },\\n  'response' => {\\n    'numFound' => 94727,\\n    'start' => 0,\\n    'docs' => [\\n      {\\n        'id' => 1,\\n        'name' => 'Hello, world',\\n        'body\\\" => 'Two roads diverged in a wood...'\\n      },\\n      # ...\\n    }\\n  }\\n}\",\n      \"language\": \"ruby\"\n    }\n  ]\n}\n[/block]\nBy default, RSolr will evaluate the response into a Ruby hash, which you can use in your application to present the search results.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"search['responseHeader']['QTime'] # => 2\\nsearch['response']['docs'] # => [ { 'id': 1, ... }, ... ]\",\n      \"language\": \"ruby\"\n    }\n  ]\n}\n[/block]\nIn addition to the `searchHeader` and `response` blocks, there may be others — such as `[[spellcheck|solr-features-spellcheck]]` — depending on how you have configured your index.","excerpt":"Learn how to integrate your application with RSolr and Websolr.","slug":"rsolr","type":"basic","title":"RSolr"}

RSolr

Learn how to integrate your application with RSolr and Websolr.

RSolr is a Ruby client for Solr. It is designed to be a simple, low-level wrapper that provides direct access to the Solr API. As such, its use encourages familiarity with the Solr API, which in turn offers simple, direct access to Solr. This document provides a basic overview of common RSolr usage. You can find RSolr's comprehensive official documentation at its source repository on GitHub at: -[ https://github.com/mwmitchell/rsolr]( https://github.com/mwmitchell/rsolr) [block:api-header] { "type": "basic", "title": "Installation" } [/block] RSolr is distributed as a Ruby Gem. To install it using the command-line RubyGems tool, simply run: ``` gem install rsolr ``` To use RSolr with Bundler in your Rails, Sinatra or other Ruby application, you can include the following in your Gemfile: [block:code] { "codes": [ { "code": "gem 'rsolr'", "language": "ruby" } ] } [/block] [block:api-header] { "type": "basic", "title": "Basic Usage" } [/block] When using RSolr, you need to specify the URL of the Solr index which you are connecting to. This can be done by passing a `:url` option to the `RSolr.connect` method, which returns a `RSolr::Client` object for you to use in indexing and searching your data. [block:code] { "codes": [ { "code": "rsolr = RSolr.connect :url => 'http://index.websolr.com/solr/a1b2c3d4e5f'", "language": "ruby" } ] } [/block] We will refer back to this `rsolr` object throughout the rest of the documentation. ## Indexing & updating your data The RSolr client's `add` method accepts a simple Ruby hash, which it will post to Solr as a document to index. [block:code] { "codes": [ { "code": "rsolr.add(:id => 1, :name => \"Hello, world\", :body => \"Two roads diverged in a wood...\")", "language": "ruby" } ] } [/block] It can also accept an array of hashes to index: [block:code] { "codes": [ { "code": "rsolr.add([\n { :id => 1, :name => \"Hello, world\", :body => \"Two roads diverged in a wood...\" },\n { :id => 2, :name => \"Adventurous\", :body => \"I shall be telling this with a sigh...\" }\n])", "language": "ruby" } ] } [/block] You can issue a commit using the `commit` method: [block:code] { "codes": [ { "code": "rsolr.commit", "language": "ruby" } ] } [/block] [block:callout] { "type": "info", "body": "For performance reasons, indexes on Websolr ignore client-issued commits, deferring instead to a pre-configured autoCommit policy." } [/block] ## Searching your documents The RSolr client provides a `select` method which sends requests to the Solr `/select` handler. It accepts a hash of parameters, which it serializes into the query string of its request. Here is a simple example, using the `q` param to perform a keyword query: [block:code] { "codes": [ { "code": "search = rsolr.select :params => { :q => \"keywords\" }", "language": "ruby" } ] } [/block] Note that in this example, the query is performed according to the query parser settings defined in `solrconfig.xml` and potentially your default query field specified in `schema.xml`. Here is a more explicit search using the [[DisMax query parser|solr-features-dismax-query-parser]] to search our example `name` and `body` fields. [block:code] { "codes": [ { "code": "search = rsolr.select :params => { :q => \"keywords\", :defType => \"dismax\", :qf => \"name body\" }", "language": "ruby", "name": null } ] } [/block] The return value of this search is a Ruby hash corresponding to the response from the Solr server. Here is a sample search response: [block:code] { "codes": [ { "code": "{\n 'responseHeader' => {\n 'status' => 0,\n 'QTime' => 2,\n 'params' => {\n 'wt' => 'ruby'\n }\n },\n 'response' => {\n 'numFound' => 94727,\n 'start' => 0,\n 'docs' => [\n {\n 'id' => 1,\n 'name' => 'Hello, world',\n 'body\" => 'Two roads diverged in a wood...'\n },\n # ...\n }\n }\n}", "language": "ruby" } ] } [/block] By default, RSolr will evaluate the response into a Ruby hash, which you can use in your application to present the search results. [block:code] { "codes": [ { "code": "search['responseHeader']['QTime'] # => 2\nsearch['response']['docs'] # => [ { 'id': 1, ... }, ... ]", "language": "ruby" } ] } [/block] In addition to the `searchHeader` and `response` blocks, there may be others — such as `[[spellcheck|solr-features-spellcheck]]` — depending on how you have configured your index.