{"id":19339411,"url":"https://github.com/appfolio/sunspot","last_synced_at":"2025-07-19T04:35:43.880Z","repository":{"id":30022179,"uuid":"33571021","full_name":"appfolio/sunspot","owner":"appfolio","description":null,"archived":false,"fork":false,"pushed_at":"2022-02-04T18:31:53.000Z","size":17939,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-02-24T08:25:29.258Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/appfolio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-04-07T22:14:14.000Z","updated_at":"2022-02-04T18:31:55.000Z","dependencies_parsed_at":"2022-09-07T08:50:56.736Z","dependency_job_id":null,"html_url":"https://github.com/appfolio/sunspot","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/appfolio/sunspot","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/appfolio%2Fsunspot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/appfolio%2Fsunspot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/appfolio%2Fsunspot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/appfolio%2Fsunspot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/appfolio","download_url":"https://codeload.github.com/appfolio/sunspot/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/appfolio%2Fsunspot/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265889138,"owners_count":23844539,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-10T03:22:01.286Z","updated_at":"2025-07-19T04:35:43.855Z","avatar_url":"https://github.com/appfolio.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Sunspot\n\n[![Build Status](http://travis-ci.org/sunspot/sunspot.png)](http://travis-ci.org/sunspot/sunspot)\n\nSunspot is a Ruby library for expressive, powerful interaction with the Solr\nsearch engine. Sunspot is built on top of the RSolr library, which\nprovides a low-level interface for Solr interaction; Sunspot provides a simple,\nintuitive, expressive DSL backed by powerful features for indexing objects and\nsearching for them.\n\nSunspot is designed to be easily plugged in to any ORM, or even non-database-backed\nobjects such as the filesystem.\n\nThis README provides a high level overview; class-by-class and\nmethod-by-method documentation is available in the [API\nreference](http://sunspot.github.com/sunspot/docs/).\n\n## Quickstart with Rails 3\n\nAdd to Gemfile:\n\n```ruby\ngem 'sunspot_rails'\ngem 'sunspot_solr' # optional pre-packaged Solr distribution for use in development\n```\n\nBundle it!\n\n```bash\nbundle install\n```\n\nGenerate a default configuration file:\n\n```bash\nrails generate sunspot_rails:install\n```\n\nIf `sunspot_solr` was installed, start the packaged Solr distribution\nwith:\n\n```bash\nbundle exec rake sunspot:solr:start # or sunspot:solr:run to start in foreground\n```\n\n## Setting Up Objects\n\nAdd a `searchable` block to the objects you wish to index.\n\n```ruby\nclass Post \u003c ActiveRecord::Base\n  searchable do\n    text :title, :body\n    text :comments do\n      comments.map { |comment| comment.body }\n    end\n\n    boolean :featured\n    integer :blog_id\n    integer :author_id\n    integer :category_ids, :multiple =\u003e true\n    double  :average_rating\n    time    :published_at\n    time    :expired_at\n\n    string  :sort_title do\n      title.downcase.gsub(/^(an?|the)/, '')\n    end\n  end\nend\n```\n\n`text` fields will be full-text searchable. Other fields (e.g.,\n`integer` and `string`) can be used to scope queries.\n\n## Searching Objects\n\n```ruby\nPost.search do\n  fulltext 'best pizza'\n\n  with :blog_id, 1\n  with(:published_at).less_than Time.now\n  order_by :published_at, :desc\n  paginate :page =\u003e 2, :per_page =\u003e 15\n  facet :category_ids, :author_id\nend\n```\n\n## Search In Depth\n\nGiven an object `Post` setup in earlier steps ...\n\n### Full Text\n\n```ruby\n# All posts with a `text` field (:title, :body, or :comments) containing 'pizza'\nPost.search { fulltext 'pizza' }\n\n# Posts with pizza, scored higher if pizza appears in the title\nPost.search do\n  fulltext 'pizza' do\n    boost_fields :title =\u003e 2.0\n  end\nend\n\n# Posts with pizza, scored higher if featured\nPost.search do\n  fulltext 'pizza' do\n    boost(2.0) { with(:featured, true) }\n  end\nend\n\n# Posts with pizza *only* in the title\nPost.search do\n  fulltext 'pizza' do\n    fields(:title)\n  end\nend\n\n# Posts with pizza in the title (boosted) or in the body (not boosted)\nPost.search do\n  fulltext 'pizza' do\n    fields(:body, :title =\u003e 2.0)\n  end\nend\n```\n\n#### Phrases\n\nSolr allows searching for phrases: search terms that are close together.\n\nIn the default query parser used by Sunspot (dismax), phrase searches\nare represented as a double quoted group of words.\n\n```ruby\n# Posts with the exact phrase \"great pizza\"\nPost.search do\n  fulltext '\"great pizza\"'\nend\n```\n\nIf specified, **query_phrase_slop** sets the number of words that may\nappear between the words in a phrase.\n\n```ruby\n# One word can appear between the words in the phrase, so \"great big pizza\"\n# also matches, in addition to \"great pizza\"\nPost.search do\n  fulltext '\"great pizza\"' do\n    query_phrase_slop 1\n  end\nend\n```\n\n##### Phrase Boosts\n\nPhrase boosts add boost to terms that appear in close proximity;\nthe terms do not *have* to appear in a phrase, but if they do, the\ndocument will score more highly.\n\n```ruby\n# Matches documents with great and pizza, and scores documents more\n# highly if the terms appear in a phrase in the title field\nPost.search do\n  fulltext 'great pizza' do\n    phrase_fields :title =\u003e 2.0\n  end\nend\n\n# Matches documents with great and pizza, and scores documents more\n# highly if the terms appear in a phrase (or with one word between them)\n# in the title field\nPost.search do\n  fulltext 'great pizza' do\n    phrase_fields :title =\u003e 2.0\n    phrase_slop   1\n  end\nend\n```\n\n### Scoping (Scalar Fields)\n\nFields not defined as `text` (e.g., `integer`, `boolean`, `time`,\netc...) can be used to scope (restrict) queries before full-text\nmatching is performed.\n\n#### Positive Restrictions\n\n```ruby\n# Posts with a blog_id of 1\nPost.search do\n  with(:blog_id, 1)\nend\n\n# Posts with an average rating between 3.0 and 5.0\nPost.search do\n  with(:average_rating, 3.0..5.0)\nend\n\n# Posts with a category of 1, 3, or 5\nPost.search do\n  with(:category_ids, [1, 3, 5])\nend\n\n# Posts published since a week ago\nPost.search do\n  with(:published_at).greater_than(1.week.ago)\nend\n```\n\n#### Negative Restrictions\n\n```ruby\n# Posts not in category 1 or 3\nPost.search do\n  without(:category_ids, [1, 3])\nend\n\n# All examples in \"positive\" also work negated using `without`\n```\n\n#### Disjunctions and Conjunctions\n\n```ruby\n# Posts that do not have an expired time or have not yet expired\nPost.search do\n  any_of do\n    with(:expired_at).greater_than(Time.now)\n    with(:expired_at, nil)\n  end\nend\n```\n\n```ruby\n# Posts with blog_id 1 and author_id 2\nPost.search do\n  all_of do\n    with(:blog_id, 1)\n    with(:author_id, 2)\n  end\nend\n```\n\nDisjunctions and conjunctions may be nested\n\n```ruby\nPost.search do\n  any_of do\n    with(:blog_id, 1)\n    all_of do\n      with(:blog_id, 2)\n      with(:category_ids, 3)\n    end\n  end\nend\n```\n\n#### Combined with Full-Text\n\nScopes/restrictions can be combined with full-text searching. The\nscope/restriction pares down the objects that are searched for the\nfull-text term.\n\n```ruby\n# Posts with blog_id 1 and 'pizza' in the title\nPost.search do\n  with(:blog_id, 1)\n  fulltext(\"pizza\")\nend\n```\n\n### Pagination\n\n**All results from Solr are paginated**\n\nThe results array that is returned has methods mixed in that allow it to\noperate seamlessly with common pagination libraries like will\\_paginate\nand kaminari.\n\nBy default, Sunspot requests the first 30 results from Solr.\n\n```ruby\nsearch = Post.search do\n  fulltext \"pizza\"\nend\n\n# Imagine there are 60 *total* results (at 30 results/page, that is two pages)\nresults = search.results # =\u003e Array with 30 Post elements\n\nsearch.total           # =\u003e 60\n\nresults.total_pages    # =\u003e 2\nresults.first_page?    # =\u003e true\nresults.last_page?     # =\u003e false\nresults.previous_page  # =\u003e nil\nresults.next_page      # =\u003e 2\nresults.out_of_bounds? # =\u003e false\nresults.offset         # =\u003e 0\n```\n\nTo retrieve the next page of results, recreate the search and use the\n`paginate` method.\n\n```ruby\nsearch = Post.search do\n  fulltext \"pizza\"\n  paginate :page =\u003e 2\nend\n\n# Again, imagine there are 60 total results; this is the second page\nresults = search.results # =\u003e Array with 30 Post elements\n\nsearch.total           # =\u003e 60\n\nresults.total_pages    # =\u003e 2\nresults.first_page?    # =\u003e false\nresults.last_page?     # =\u003e true\nresults.previous_page  # =\u003e 1\nresults.next_page      # =\u003e nil\nresults.out_of_bounds? # =\u003e false\nresults.offset         # =\u003e 30\n```\n\nA custom number of results per page can be specified with the\n`:per_page` option to `paginate`:\n\n```ruby\nsearch = Post.search do\n  fulltext \"pizza\"\n  paginate :page =\u003e 1, :per_page =\u003e 50\nend\n```\n\n### Faceting\n\nFaceting is a feature of Solr that determines the number of documents\nthat match a given search *and* an additional criterion. This allows you\nto build powerful drill-down interfaces for search.\n\nEach facet returns zero or more rows, each of which represents a\nparticular criterion conjoined with the actual query being performed.\nFor **field facets**, each row represents a particular value for a given\nfield. For **query facets**, each row represents an arbitrary scope; the\nfacet itself is just a means of logically grouping the scopes.\n\n#### Field Facets\n\n```ruby\n# Posts that match 'pizza' returning counts for each :author_id\nsearch = Post.search do\n  fulltext \"pizza\"\n  facet :author_id\nend\n\nsearch.facet(:author_id).rows.each do |facet|\n  puts \"Author #{facet.value} has #{facet.count} pizza posts!\"\nend\n```\n\n#### Query Facets\n\n```ruby\n# Posts faceted by ranges of average ratings\nPost.search do\n  facet(:average_rating) do\n    row(1.0..2.0) do\n      with(:average_rating, 1.0..2.0)\n    end\n    row(2.0..3.0) do\n      with(:average_rating, 2.0..3.0)\n    end\n    row(3.0..4.0) do\n      with(:average_rating, 3.0..4.0)\n    end\n    row(4.0..5.0) do\n      with(:average_rating, 4.0..5.0)\n    end\n  end\nend\n\n# e.g.,\n# Number of posts with rating withing 1.0..2.0: 2\n# Number of posts with rating withing 2.0..3.0: 1\nsearch.facet(:average_rating).rows.each do |facet|\n  puts \"Number of posts with rating withing #{facet.value}: #{facet.count}\"\nend\n```\n\n### Ordering\n\nBy default, Sunspot orders results by \"score\": the Solr-determined\nrelevancy metric. Sorting can be customized with the `order_by` method:\n\n```ruby\n# Order by average rating, descending\nPost.search do\n  fulltext(\"pizza\")\n  order_by(:average_rating, :desc)\nend\n\n# Order by relevancy score and in the case of a tie, average rating\nPost.search do\n  fulltext(\"pizza\")\n\n  order_by(:score, :desc)\n  order_by(:average_rating, :desc)\nend\n\n# Randomized ordering\nPost.search do\n  fulltext(\"pizza\")\n  order_by(:random)\nend\n```\n\n### Geospatial\n\nTODO\n\n### Highlighting\n\nHighlighting allows you to display snippets of the part of the document\nthat matched the query.\n\nThe fields you wish to highlight must be **stored**.\n\n```ruby\nclass Post \u003c ActiveRecord::Base\n  searchable do\n    # ...\n    text :body, :stored =\u003e true\n  end\nend\n```\n\nHighlighting matches on the `body` field, for instance, can be acheived\nlike:\n\n```ruby\nsearch = Post.search do\n  fulltext \"pizza\" do\n    highlight :body\n  end\nend\n\n# Will output something similar to:\n# Post #1\n#   I really love *pizza*\n#   *Pizza* is my favorite thing\n# Post #2\n#   Pepperoni *pizza* is delicious\nsearch.hits.each do |hit|\n  puts \"Post ##{hit.primary_key}\"\n\n  hit.highlights(:body).each do |highlight|\n    puts \"  \" + highlight.format { |word| \"*#{word}*\" }\n  end\nend\n```\n\n### Functions\n\nTODO\n\n### More Like This\n\nTODO\n\n## Indexing In Depth\n\nTODO\n\n### Index-Time Boosts\n\nTo specify that a field should be boosted in relation to other fields for\nall queries, you can specify the boost at index time:\n\n```ruby\nclass Post \u003c ActiveRecord::Base\n  searchable do\n    text :title, :boost =\u003e 5.0\n    text :body\n  end\nend\n```\n\n### Stored Fields\n\nStored fields keep an original (untokenized/unanalyzed) version of their\ncontents in Solr.\n\nStored fields allow data to be retrieved without also hitting the\nunderlying database (usually an SQL server). They are also required for\nhighlighting and more like this queries.\n\nStored fields come at some performance cost in the Solr index, so use\nthem wisely.\n\n```ruby\nclass Post \u003c ActiveRecord::Base\n  searchable do\n    text :body, :stored =\u003e true\n  end\nend\n\n# Retrieving stored contents without hitting the database\nPost.search.hits.each do |hit|\n  puts hit.stored(:body)\nend\n```\n\n## Hits vs. Results\n\nSunspot simply stores the type and primary key of objects in Solr.\nWhen results are retrieved, those primary keys are used to load the\nactual object (usually from an SQL database).\n\n```ruby\n# Using #results pulls in the records from the object-relational\n# mapper (e.g., ActiveRecord + a SQL server)\nPost.search.results.each do |result|\n  puts result.body\nend\n```\n\nTo access information about the results without querying the underlying\ndatabase, use `hits`:\n\n```ruby\n# Using #hits gives back all information requested from Solr, but does\n# not load the object from the object-relational mapper\nPost.search.hits.each do |hit|\n  puts hit.stored(:body)\nend\n```\n\nIf you need both the result (ORM-loaded object) and `Hit` (e.g., for\nfaceting, highlighting, etc...), you can use the convenience method\n`each_hit_with_result`:\n\n```ruby\nPost.search.each_hit_with_result do |hit, result|\n  # ...\nend\n```\n\n## Reindexing Objects\n\nIf you are using Rails, objects are automatically indexed to Solr as a\npart of the `save` callbacks.\n\nIf you make a change to the object's \"schema\" (code in the `searchable` block),\nyou must reindex all objects so the changes are reflected in Solr:\n\n```bash\nbundle exec rake sunspot:solr:reindex\n\n# or, to be specific to a certain model with a certain batch size:\nbundle exec rake sunspot:solr:reindex[500,Post] # some shells will require escaping [ with \\[ and ] with \\]\n```\n\n## Use Without Rails\n\nTODO\n\n## Manually Adjusting Solr Parameters\n\nTo add or modify parameters sent to Solr, use `adjust_solr_params`:\n\n```ruby\nPost.search do\n  adjust_solr_params do |params|\n    params[:q] += \" AND something_s:more\"\n  end\nend\n```\n\n## Session Proxies\n\nTODO\n\n## Type Reference\n\nTODO\n\n## Development\n\n### Running Tests\n\n#### sunspot\n\nInstall the required gem dependencies:\n\n```bash\ncd /path/to/sunspot/sunspot\nbundle install\n```\n\nStart a Solr instance on port 8983:\n\n```bash\nbundle exec sunspot-solr start -p 8983\n# or `bundle exec sunspot-solr run -p 8983` to run in foreground\n```\n\nRun the tests:\n\n```bash\nbundle exec rake spec\n```\n\nIf desired, stop the Solr instance:\n\n```bash\nbundle exec sunspot-solr stop\n```\n\n#### sunspot\\_rails\n\nInstall the gem dependencies for `sunspot`:\n\n```bash\ncd /path/to/sunspot/sunspot\nbundle install\n```\n\nStart a Solr instance on port 8983:\n\n```bash\nbundle exec sunspot-solr start -p 8983\n# or `bundle exec sunspot-solr run -p 8983` to run in foreground\n```\n\nNavigate to the `sunspot_rails` directory:\n\n```bash\ncd ../sunspot_rails\n```\n\nRun the tests:\n\n```bash\nrake spec # all Rails versions\nrake spec RAILS=3.1.1 # specific Rails version only\n```\n\nIf desired, stop the Solr instance:\n\n```bash\ncd ../sunspot\nbundle exec sunspot-solr stop\n```\n\n### Generating Documentation\n\nInstall the `yard` and `redcarpet` gems:\n\n```bash\n$ gem install yard redcarpet\n```\n\nUninstall the `rdiscount` gem, if installed:\n\n```bash\n$ gem uninstall rdiscount\n```\n\nGenerate the documentation from topmost directory:\n\n```bash\n$ yardoc -o docs */lib/**/*.rb - README.md\n```\n\n## Tutorials and Articles\n\n* [Full Text Searching with Solr and Sunspot](http://collectiveidea.com/blog/archives/2011/03/08/full-text-searching-with-solr-and-sunspot/) (Collective Idea)\n* [Full-text search in Rails with Sunspot](http://tech.favoritemedium.com/2010/01/full-text-search-in-rails-with-sunspot.html) (Tropical Software Observations)\n* [Sunspot Full-text Search for Rails/Ruby](http://therailworld.com/posts/23-Sunspot-Full-text-Search-for-Rails-Ruby) (The Rail World)\n* [A Few Sunspot Tips](http://blog.trydionel.com/2009/11/19/a-few-sunspot-tips/) (spiral_code)\n* [Sunspot: A Solr-Powered Search Engine for Ruby](http://www.linux-mag.com/id/7341) (Linux Magazine)\n* [Sunspot Showed Me the Light](http://bennyfreshness.com/2010/05/sunspot-helped-me-see-the-light/) (ben koonse)\n* [RubyGems.org — A case study in upgrading to full-text search](http://blog.websolr.com/post/3505903537/rubygems-search-upgrade-1) (Websolr)\n* [How to Implement Spatial Search with Sunspot and Solr](http://codequest.eu/articles/how-to-implement-spatial-search-with-sunspot-and-solr) (Code Quest)\n* [Sunspot 1.2 with Spatial Solr Plugin 2.0](http://joelmats.wordpress.com/2011/02/23/getting-sunspot-1-2-with-spatial-solr-plugin-2-0-to-work/) (joelmats)\n* [rails3 + heroku + sunspot : madness](http://anhaminha.tumblr.com/post/632682537/rails3-heroku-sunspot-madness) (anhaminha)\n* [How to get full text search working with Sunspot](http://cookbook.hobocentral.net/recipes/57-how-to-get-full-text-search) (Hobo Cookbook)\n* [Full text search with Sunspot in Rails](http://hemju.com/2011/01/04/full-text-search-with-sunspot-in-rail/) (hemju)\n* [Using Sunspot for Free-Text Search with Redis](http://masonoise.wordpress.com/2010/02/06/using-sunspot-for-free-text-search-with-redis/) (While I Pondered...)\n* [Fuzzy searching in SOLR with Sunspot](http://www.pipetodevnull.com/past/2010/8/5/fuzzy_searching_in_solr_with_sunspot/) (pipe :to =\u003e /dev/null)\n* [Default scope with Sunspot](http://www.cloudspace.com/blog/2010/01/15/default-scope-with-sunspot/) (Cloudspace)\n* [Index External Models with Sunspot/Solr](http://www.medihack.org/2011/03/19/index-external-models-with-sunspotsolr/) (Medihack)\n* [Chef recipe for Sunspot in production](http://gist.github.com/336403)\n* [Testing with Sunspot and Cucumber](http://collectiveidea.com/blog/archives/2011/05/25/testing-with-sunspot-and-cucumber/) (Collective Idea)\n* [Cucumber and Sunspot](http://opensoul.org/2010/4/7/cucumber-and-sunspot) (opensoul.org)\n* [Testing Sunspot with Cucumber](http://blog.trydionel.com/2010/02/06/testing-sunspot-with-cucumber/) (spiral_code)\n* [Running cucumber features with sunspot_rails](http://blog.kabisa.nl/2010/02/03/running-cucumber-features-with-sunspot_rails) (Kabisa Blog)\n* [Testing Sunspot with Test::Unit](http://timcowlishaw.co.uk/post/3179661158/testing-sunspot-with-test-unit) (Type Slowly)\n* [How To Use Twitter Lists to Determine Influence](http://www.untitledstartup.com/2010/01/how-to-use-twitter-lists-to-determine-influence/) (Untitled Startup)\n* [Sunspot Quickstart](http://wiki.websolr.com/index.php/Sunspot_Quickstart) (WebSolr)\n* [Solr, and Sunspot](http://www.kuahyeow.com/2009/08/solr-and-sunspot.html) (YT!)\n* [The Saga of the Switch](http://mrb.github.com/2010/04/08/the-saga-of-the-switch.html) (mrb -- includes comparison of Sunspot and Ultrasphinx)\n\n## License\n\nSunspot is distributed under the MIT License, copyright (c) 2008-2009 Mat Brown\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fappfolio%2Fsunspot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fappfolio%2Fsunspot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fappfolio%2Fsunspot/lists"}