• This repository has been archived on 12/Dec/2021
  • Stars
    star
    139
  • Rank 262,954 (Top 6 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 15 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

High level Ruby library for interacting with Xapian, a full text search engine.

Unmaintained

The Xapit gem is no longer maintained. Feel free to fork this project.

Xapit

Xapit (pronounced “zap it”) is a Ruby gem for doing full text searching through Xapian. This project was recently rewritten and is not yet ready for production. Please try it out and let me know if there are any issues.

Install

First, install Xapian with the Ruby bindings. The easiest way is through Homebrew.

brew install xapian --ruby

Note: this is tied to the current version of Ruby you have installed. If you use RVM be certain you’re in the correct version. See Installing Xapian for other methods.

Next add Xapit to your Gemfile and run the bundle command.

gem "xapit", "~> 0.3"

Then if you are in a Rails application, run the installation generator.

rails g xapit:install

This creates a configuration file and a rackup file for production use.

Index

To make a model searchable you must define an index through the xapit method. Here is an example of the four methods you can call there.

class Article < ActiveRecord::Base
  xapit do
    text :name, :content
    field :category_id
    sortable :id, :created_at
    facet :author_name, "Author"
  end
end

This indexes the model to be searched in a variety of ways (shown below). See the Indexing wiki page for details.

Currently only Active Record is supported, but expect support for other libraries soon.

The index will automatically be updated when records are added or removed. You can regenerate the index manually to fill it with any existing records.

rake xapit:index

Use the search class method to perform a full text search on the index. This returns a Xapit scope where additional scoping methods can be called similar to Active Record scopes.

# simple full text search
@articles = Article.search("phone")

# full text search with basic boolean matching
@articles = Article.search("phone OR fax NOT email")

# pagination works with kaminari and will_paginate
@articles = Article.search("phone").page(10).per_page(20)

# search based on a specific field
@articles = Article.search("phone").where(:category_id => params[:category_id])

# search for multiple negative conditions (doesn't match 3, 5, or 8)
@articles = Article.search("phone").not_where(:category_id => [3, 5, 8])

# search for range of conditions by number
@articles = Article.search.where(:released_at => 2.years.ago..Time.now)

# order based on sortable fields, sorting defaults to most relevant
@articles = Article.search("phone").order(:created_at, :desc)

Simply iterate through the returned set to display the results.

<% for article in @articles %>
  <%= article.name %>
<% end %>

See the Searching wiki page for more details.

Spelling

Spelling suggestions are available when there is a simlarly indexed term.

<% if @articles.spelling_suggestion %>
  Did you mean <%= link_to @articles.spelling_suggestion, :overwrite_params => { :keywords => @articles.spelling_suggestion } %>?
<% end %>

Note: the spelling feature is disabled by default in the test environment because it uses an in-memory database.

Facets

Facets allow you to further filter the result set based on certain attributes.

<% for facet in @articles.facets %>
  <%= facet.name %>
  <% for option in facet.options %>
    <%= link_to option.name, :overwrite_params => { :facets => option } %>
    (<%= option.count %>)
  <% end %>
<% end %>

The facet option is passed in through the URL which you can add to the search.

Article.search("phone").with_facets(params[:facets])

You can also list the applied facets along with a remove link.

<% for option in @articles.applied_facet_options %>
  <%= option.name %>
  <%= link_to "remove", :overwrite_params => { :facets => option } %>
<% end %>

Testing

To use Xapit in the test environment, you will need to reload the configuration before each test. For example, you can do this in RSpec:

config.before(:each) do
  Xapit.reload
end

Xapit is also disabled by default in the test environment so you can enable it on a per-test basis. This way it doesn’t slow down tests which don’t need Xapit search.

Xapit.enable
# create records and test searching functionality

If you have existing records in the database you want to search (maybe loaded by fixtures) you can index those.

Xapit.index(Article, Comment) # or whatever models you need

By default, the test environment keeps the Xapian database in memory which makes it much faster and easier to reset. The problem is that the spelling suggestion feature does not work with in-memory databases, so it is disabled in the test environment by default. All of this can be customized in the configuration file.

Production

The default Xapit setup works well in development because there is only one instance of the Rails app running. However in production you will need to move Xapit to a separate process so all of the instances can communicate to it. To do this, start up the rackup file provided. You will likely want to put this behind a server such as Passenger.

rackup xapit.ru

Now when you start up your Rails app in production it will use this server. You can configure the server URL in the configuration file.

Bug Reports

First check out the troubleshooting section when you have problems. If you have found a bug or have a feature to request, please add it to the GitHub issue tracker if it is not there already. Feel free to treat it as a mailing list and discuss new ideas or bring up any confusion you may have.

More Repositories

1

cancan

Authorization Gem for Ruby on Rails.
Ruby
6,265
star
2

ruby-warrior

Game written in Ruby for learning Ruby.
Ruby
3,830
star
3

letter_opener

Preview mail in the browser instead of sending.
Ruby
3,686
star
4

dotfiles

config files for zsh, bash, completions, gem, git, irb, rails
Shell
2,312
star
5

nifty-generators

A collection of useful Rails generator scripts.
Ruby
1,984
star
6

nested_form

Rails plugin to conveniently handle multiple models in a single form.
Ruby
1,792
star
7

private_pub

Handle pub/sub messaging through private channels in Rails using Faye.
Ruby
864
star
8

railscasts-episodes

NOT MAINTAINED. See README.
Ruby
846
star
9

railscasts

railscasts.com in open source (outdated).
Ruby
760
star
10

populator

Mass populate an Active Record database.
Ruby
393
star
11

complex-form-examples

Various ways to handle multi-model forms in Rails.
Ruby
304
star
12

trusted-params

Rails plugin for overriding attr_accessible protection.
Ruby
149
star
13

mustard

Simple "must" expectations for tests and specs in Ruby.
Ruby
144
star
14

govsgo

Rails 3 app for playing the board game Go online.
Ruby
140
star
15

rails-templates

Template scripts for creating new rails applications.
Ruby
134
star
16

cocoa-web-app-example

A Cocoa application to demonstrate the interaction between Objective-C and JavaScript in a WebView.
Objective-C
96
star
17

importex

Import an Excel file using Ruby.
Ruby
90
star
18

uniquify

Generate a unique, random token for Active Record.
Ruby
87
star
19

textmate-themes

My TextMate themes (includes Railscasts theme)
69
star
20

acts-as-list

NOT MAINTAINED. Gem version of acts_as_list Rails plugin.
Ruby
65
star
21

abingo

Fork of A/Bingo plugin for Rails.
Ruby
55
star
22

railscasts-scripts

Scripts used internally when producing RailsCasts
Ruby
52
star
23

scope-builder

Build up named scopes conditionally.
Ruby
51
star
24

rmov

Ruby wrapper for the QuickTime C API.
C
48
star
25

render-caching

Cache render calls in Rails controllers.
Ruby
45
star
26

enlighten

Interactive ruby debugger in the browser.
Ruby
41
star
27

static_actions

Rails plugin to quickly make named routes for non-RESTful actions.
Ruby
39
star
28

searchify

Rails plugin to add extra searching functionality to models.
Ruby
37
star
29

selenium-on-rails

This repo is no longer maintained, see the official repository by paytonrules.
JavaScript
34
star
30

ryan-on-rails.tmbundle

Some TextMate snippets I use when working with Ruby and Rails.
26
star
31

dailystamp

Source code for my Rails Rumble 2009 submission
Ruby
23
star
32

url_formatter

Format and validate a URL in Active Record. Example gem for RailsCasts.
Ruby
18
star
33

association-freezer

Freeze a belongs_to association in Active Record.
Ruby
17
star
34

admiteer

Rails Rumble 2007 project by Jack Canty, Kelli Shaver, and Ryan Bates
17
star
35

todo-list.tmbundle

A simple TextMate bundle to manage a todo lists.
14
star
36

myideadrawer

Rails Rumble 2008 entry by Ryan Bates and Kelli Shaver
Ruby
13
star
37

advent-2022

Advent of Code in Elixir
Elixir
12
star
38

blog-screencast

Example blog application built in the offical 15 minute Rails screencast.
Ruby
12
star
39

xapit-sync

Rails plugin to automatically reload a Xapian database when models change.
Ruby
12
star
40

ryan-bates.tmbundle

Miscellaneous commands and snippets I use in TextMate.
11
star
41

maestro

Piano exercise game written in MacRuby.
Ruby
11
star
42

vscode-railscasts-theme

RailsCasts Theme for VS Code
7
star
43

ryanb.github.io

Personal site for Ryan Bates
5
star
44

vscode-erb-syntax

ERB Syntax for VS Code
5
star
45

xapit-server

Rack server for interacting with a Xapian database remotely through Xapit.
Ruby
4
star
46

swapper

Ruby script for swapping two elements on a line (to be used in text editors).
3
star
47

bookmarklets

JavaScript
1
star
48

wallaby-rails-7-1-2

Example Rails 7.1.2 app with Wallaby
Ruby
1
star