Convert Figma logo to code with AI

pat logothinking-sphinx

Sphinx/Manticore plugin for ActiveRecord/Rails

1,627
468
1,627
10

Top Related Projects

Intelligent search made easy

Elasticsearch integrations for ActiveModel/Record and Ruby on Rails

pg_search builds ActiveRecord named scopes that take advantage of PostgreSQL’s full text search

5,648

Object-based searching.

6,271

Authorization Gem for Ruby on Rails.

Quick Overview

Thinking Sphinx is a Ruby library that integrates the Sphinx search engine with ActiveRecord and Rails applications. It provides a simple and intuitive way to add full-text search capabilities to Ruby on Rails projects, offering both real-time and delta indexing options.

Pros

  • Easy integration with Rails and ActiveRecord
  • Supports both real-time and delta indexing
  • Offers flexible query options and faceted search
  • Provides a clean DSL for index and search configuration

Cons

  • Requires separate installation and configuration of Sphinx search engine
  • Can be complex to set up and optimize for large datasets
  • Limited support for non-ActiveRecord models
  • May have performance issues with very large indexes

Code Examples

  1. Defining an index:
class Article < ActiveRecord::Base
  searchkick

  def search_data
    {
      title: title,
      content: content,
      author: author.name,
      published_at: published_at
    }
  end
end
  1. Performing a basic search:
Article.search("ruby on rails")
  1. Advanced search with filters and facets:
Article.search "ruby on rails",
  where: { published_at: { gte: 1.month.ago } },
  facets: [:author],
  page: 2,
  per_page: 20

Getting Started

  1. Add to your Gemfile:
gem 'thinking-sphinx'
  1. Install the gem:
bundle install
  1. Generate the Thinking Sphinx configuration:
rails generate thinking_sphinx:install
  1. Define an index in your model:
class Article < ActiveRecord::Base
  define_index do
    indexes title
    indexes content
    indexes author.name, as: :author
    has published_at
  end
end
  1. Generate and build the index:
rake ts:index
  1. Start the Sphinx searchd daemon:
rake ts:start

Now you can use Thinking Sphinx in your Rails application to perform searches and manage indexes.

Competitor Comparisons

Intelligent search made easy

Pros of Searchkick

  • Easier to set up and use, with a more intuitive API
  • Better support for advanced features like geospatial search and faceted search
  • More actively maintained with frequent updates and improvements

Cons of Searchkick

  • Limited to Elasticsearch as the search backend
  • May have a steeper learning curve for complex queries
  • Potentially higher resource usage due to its feature-rich nature

Code Comparison

Searchkick:

class Product < ApplicationRecord
  searchkick
end

Product.search("apples", where: {in_stock: true})

Thinking Sphinx:

class Product < ApplicationRecord
  define_index do
    indexes name, sortable: true
    has in_stock
  end
end

Product.search "apples", with: {in_stock: true}

Both libraries aim to simplify full-text search in Ruby applications, but they differ in their approach and feature set. Searchkick focuses on providing a user-friendly interface with powerful features out of the box, while Thinking Sphinx offers more flexibility in terms of search backends and configuration options. The choice between the two depends on specific project requirements, such as the desired search engine, complexity of queries, and the level of control needed over the search functionality.

Elasticsearch integrations for ActiveModel/Record and Ruby on Rails

Pros of Elasticsearch Rails

  • More flexible and powerful querying capabilities
  • Better support for complex, distributed search scenarios
  • Seamless integration with the Elasticsearch ecosystem

Cons of Elasticsearch Rails

  • Steeper learning curve and more complex setup
  • Requires separate Elasticsearch server maintenance
  • Potentially higher resource usage for large-scale applications

Code Comparison

Thinking Sphinx:

class Article < ActiveRecord::Base
  define_index do
    indexes title, sortable: true
    indexes content
    has author_id, created_at, updated_at
  end
end

Elasticsearch Rails:

class Article < ActiveRecord::Base
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks

  settings index: { number_of_shards: 1 } do
    mappings dynamic: 'false' do
      indexes :title, analyzer: 'english'
      indexes :content, analyzer: 'english'
      indexes :author_id, type: 'integer'
      indexes :created_at, type: 'date'
      indexes :updated_at, type: 'date'
    end
  end
end

The code comparison shows that Elasticsearch Rails requires more configuration but offers greater control over indexing and mapping. Thinking Sphinx provides a simpler setup with its DSL, making it easier to get started with basic search functionality.

pg_search builds ActiveRecord named scopes that take advantage of PostgreSQL’s full text search

Pros of pg_search

  • Focused specifically on PostgreSQL, leveraging its powerful full-text search capabilities
  • Simpler setup and configuration for PostgreSQL users
  • Supports multi-search functionality out of the box

Cons of pg_search

  • Limited to PostgreSQL databases only
  • Less extensive documentation compared to Thinking Sphinx
  • Fewer advanced features for complex search scenarios

Code Comparison

pg_search:

class Article < ApplicationRecord
  include PgSearch::Model
  pg_search_scope :search_by_title, against: :title
end

Article.search_by_title("Ruby on Rails")

Thinking Sphinx:

class Article < ApplicationRecord
  define_index do
    indexes title
  end
end

Article.search "Ruby on Rails"

Both libraries provide straightforward ways to implement search functionality, but pg_search's syntax is more PostgreSQL-specific, while Thinking Sphinx offers a more database-agnostic approach. pg_search is ideal for PostgreSQL-based projects seeking simple full-text search, while Thinking Sphinx offers broader database support and more advanced features for complex search requirements.

5,648

Object-based searching.

Pros of Ransack

  • More flexible and versatile for complex querying across multiple models
  • Easier integration with Rails form helpers for dynamic search forms
  • Supports a wider range of databases and ORMs beyond just ActiveRecord

Cons of Ransack

  • May have a steeper learning curve for advanced features
  • Can be slower for large datasets compared to Sphinx's optimized search
  • Lacks some of the advanced text search capabilities that Thinking Sphinx offers

Code Comparison

Ransack:

@q = Person.ransack(params[:q])
@people = @q.result(distinct: true)

Thinking Sphinx:

@people = Person.search params[:query], 
  :with => {:gender => params[:gender]}

Key Differences

Ransack is primarily focused on building complex database queries and search forms, while Thinking Sphinx specializes in full-text search using the Sphinx search engine. Ransack is more suitable for general-purpose querying and filtering, whereas Thinking Sphinx excels in scenarios requiring advanced text search capabilities and high performance on large datasets.

Ransack integrates seamlessly with Rails and ActiveRecord, making it easier to implement search functionality without additional infrastructure. Thinking Sphinx, on the other hand, requires setting up and maintaining a separate Sphinx search daemon, which can provide better performance for text-heavy searches but adds complexity to the deployment process.

6,271

Authorization Gem for Ruby on Rails.

Pros of CanCan

  • Simpler and more intuitive authorization system for Rails applications
  • Easier to set up and configure for basic authorization needs
  • More widely adopted in the Rails community, with extensive documentation and support

Cons of CanCan

  • Less flexible for complex search and indexing requirements
  • Limited to authorization, while Thinking Sphinx offers full-text search capabilities
  • May require additional gems or plugins for advanced features

Code Comparison

CanCan example:

class Ability
  include CanCan::Ability

  def initialize(user)
    can :read, Post
    can :manage, Post, user_id: user.id
  end
end

Thinking Sphinx example:

class Article < ActiveRecord::Base
  define_index do
    indexes title, content
    has author_id, created_at, updated_at
  end
end

Summary

CanCan is focused on authorization in Rails applications, offering a straightforward approach to defining and managing user permissions. It's easier to set up for basic needs but may lack flexibility for complex scenarios.

Thinking Sphinx, on the other hand, is a full-text search solution that integrates with Rails. It provides powerful search and indexing capabilities but requires more setup and configuration.

Choose CanCan for simple authorization needs, and Thinking Sphinx for advanced search functionality in your Rails application.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

h1. Thinking Sphinx

Thinking Sphinx is a library for connecting ActiveRecord to the Sphinx full-text search tool, and integrates closely with Rails (but also works with other Ruby web frameworks). The current release is v5.6.0.

h2. Upgrading

Please refer to "the changelog":https://github.com/pat/thinking-sphinx/blob/develop/CHANGELOG.markdown and "release notes":https://github.com/pat/thinking-sphinx/releases for any changes you need to make when upgrading. The release notes in particular are quite good at covering breaking changes and more details for new features.

The documentation also has more details on what's involved for upgrading from "v4 to v5":https://freelancing-gods.com/thinking-sphinx/v5/upgrading.html, "v3 to v4":https://freelancing-gods.com/thinking-sphinx/v4/upgrading.html, and "v1/v2 to v3":https://freelancing-gods.com/thinking-sphinx/v3/upgrading.html.

h2. Installation

It's a gem, so install it like you would any other gem. You will also need to specify the mysql2 gem if you're using MRI, or jdbc-mysql if you're using JRuby:

gem 'mysql2',          '~> 0.4',    :platform => :ruby
gem 'jdbc-mysql',      '~> 5.1.35', :platform => :jruby
gem 'thinking-sphinx', '~> 5.5'

The MySQL gems mentioned are required for connecting to Sphinx, so please include it even when you're using PostgreSQL for your database.

You'll also need to install Sphinx - this is covered in "the extended documentation":https://freelancing-gods.com/thinking-sphinx/installing_sphinx.html.

h2. Usage

Begin by reading the "quick-start guide":https://freelancing-gods.com/thinking-sphinx/quickstart.html, and beyond that, "the documentation":https://freelancing-gods.com/thinking-sphinx/ should serve you pretty well.

h2. Requirements

The current release of Thinking Sphinx works with the following versions of its dependencies:

|. Library |. Minimum |_. Tested Against | | Ruby | v2.4 | v2.4, v2.5, v2.6, v2.7, v3.0, v3.1, v3.2 | | Sphinx | v2.2.11 | v2.2.11, v3.4.1 | | Manticore | v2.8 | v4.0, v6.0 | | ActiveRecord | v4.2 | v4.2..v7.0 |

It might work with older versions of Ruby, but it's highly recommended to update to a supported release.

It should also work with JRuby, but the test environment for that in CI has been unreliable, hence that's not actively tested against at the moment.

h3. Sphinx or Manticore

If you're using Sphinx, v2.2.11 is recommended even though it's quite old, as it works well with PostgreSQL databases (but if you're using MySQL - or real-time indices - then v3.3.1 should also be fine).

If you're opting for Manticore instead, v2.8 or newer works, but v4 or newer is recommended as that's what is actively tested against. The v4.2 and 5.0 releases had bugs with facet searching, but that's been fixed in Manticore v6.0.

h3. Rails and ActiveRecord

Currently Thinking Sphinx is built to support Rails/ActiveRecord 4.2 or newer. If you're using Sinatra and ActiveRecord instead of Rails, that's fine - just make sure you add the @:require => 'thinking_sphinx/sinatra'@ option when listing @thinking-sphinx@ in your Gemfile.

If you want ActiveRecord 3.2-4.1 support, then refer to the 4.x releases of Thinking Sphinx. Or, for ActiveRecord 3.1 support, then refer to the 3.0.x releases. Anything older than that, then you're stuck with Thinking Sphinx v2.x (for Rails/ActiveRecord 3.0) or v1.x (Rails 2.3). Please note that these older versions are no longer actively supported.

h3. Ruby

You'll need either the standard Ruby (v2.4 or newer) or JRuby (9.1 or newer).

h3. Database Versions

MySQL 5.x and Postgres 8.4 or better are supported.

h2. Contributing

Please note that this project has a "Contributor Code of Conduct":http://contributor-covenant.org/version/1/0/0/. By participating in this project you agree to abide by its terms.

To contribute, clone this repository and have a good look through the specs - you'll notice the distinction between acceptance tests that actually use Sphinx and go through the full stack, and unit tests (everything else) which use liberal test doubles to ensure they're only testing the behaviour of the class in question. I've found this leads to far better code design.

All development is done on the @develop@ branch; please base any pull requests off of that branch. Please write the tests and then the code to get them passing, and send through a pull request.

In order to run the tests, you'll need to create a database named @thinking_sphinx@:

# Either fire up a MySQL console:
mysql -u root
# OR a PostgreSQL console:
psql
# In that console, create the database:
CREATE DATABASE thinking_sphinx;

You can then run the unit tests with @rake spec:unit@, the acceptance tests with @rake spec:acceptance@, or all of the tests with just @rake@. To run these with PostgreSQL, you'll need to set the @DATABASE@ environment variable accordingly:

DATABASE=postgresql rake

h2. Licence

Copyright (c) 2007-2024, Thinking Sphinx is developed and maintained by Pat Allan, and is released under the open MIT Licence. Many thanks to "all who have contributed patches":https://github.com/pat/thinking-sphinx/contributors.