= XapianDb == What's in the box? XapianDb is a ruby gem that combines features of nosql databases and fulltext indexing into one piece. The result: Rich documents and very fast queries. It is based on {Xapian}[http://xapian.org/], an efficient and powerful indexing library. The gem is in very early development and not production ready yet. XapianDb is inspired by {xapian-fu}[https://github.com/johnl/xapian-fu] and {xapit}[https://github.com/ryanb/xapit]. Thank you John and Ryan for your great work. It helped me learning to understand the xapian library and I borrowed an idea or two from you ;-) == Why yet another indexing gem? In the good old days I used {ferret}[https://github.com/dbalmain/ferret] and {acts_as_ferret}[https://github.com/jkraemer/acts_as_ferret] as my fulltext indexing solution and everything was fine. But time moved on and Ferret didn't. So I started to rethink fulltext indexing again. I looked for something that * is under active development * is fast * is lightweight and easy to install / deploy * is framework and database agnostic and works with pure POROS (plain old ruby objects) * is configurable anywhere, not just inside the model classes; I think that index configurations should not be part of the domain model * supports document configuration at the class level, not the database level; each class has its own document structure * integrates with popular Ruby / Rails ORMs like ActiveRecord or Datamapper through a plugin architecture * returns rich document objects that do not necessarily need a database roundtrip to render the search results (but know how to get the underlying object, if needed) * updates the index realtime (no scheduled reindexing jobs) * supports all major features of a full text indexer, namely wildcards!! I tried hard but I couldn't find such a thing so I decided to write it, based on the Xapian library. == Getting started If you want to use xapian_db in a Rails app, you need Rails 3 or newer. === Install Xapian if not already installed To use xapian_db, make sure you have the Xapian library and ruby bindings installed. At the time of this writing, the newest release of Xapian was 1.2.3. You might want to adjust the URLs below to load the most current release of Xapian. The example code works for OSX. On linux you might want to use wget instead of curl. A future release of xapian_db might include the Xapian binaries and make this step obsolete. ==== Install Xapian curl -O http://oligarchy.co.uk/xapian/1.2.3/xapian-core-1.2.3.tar.gz tar xzvf xapian-core-1.2.3.tar.gz cd xapian-core-1.2.3 ./configure --prefix=/usr/local make sudo make install ==== Install ruby bindings for Xapian curl -O http://oligarchy.co.uk/xapian/1.2.2/xapian-bindings-1.2.3.tar.gz tar xzvf xapian-bindings-1.2.3.tar.gz cd xapian-bindings-1.2.3 ./configure --prefix=/usr/local XAPIAN_CONFIG=/usr/local/bin/xapian-config make sudo make install The following steps assume that you are using xapian_db within a Rails app. The gem has an example in the examples folder that shows how you can use xapian_db without Rails. === Configure your databases Without a config file, xapian_db creates the database in the db folder for development and production environments. If you are in the test environment, xapian_db creates an in memory database. It assumes you are using ActiveRecord. You can override these defaults by placing a config file named 'xapian_db.yml' into your config folder. Here's an example: # XapianDb configuration defaults: &defaults adapter: datamapper # Avaliable adapters: :active_record, :datamapper language: de # Global language; can be overridden for specific blueprints development: database: db/xapian_db/development <<: *defaults test: database: ":memory:" # Use an in memory database for tests <<: *defaults production: database: db/xapian_db/production <<: *defaults === Configure an index blueprint In order to get your models indexed, you must configure a document blueprint for each class you want to index: XapianDb::DocumentBlueprint.setup(Person) do |blueprint| blueprint.attribute :name, :weight => 10 blueprint.attribute :first_name end The example above assumes that you have a class Person with the methods name and first_name. Attributes will get indexed and are stored in the documents. You will be able to access the name and the first name in your search results. If you want to index additional data but do not need access to it from a search result, use the index method: blueprint.index :remarks, :weight => 5 If you config a class that has a language property, e.g. class Person attr_reader :language end you can configure the blueprint to use the language of the object when indexing: XapianDb::DocumentBlueprint.setup(Person) do |blueprint| blueprint.language_method :language end The method must return the iso code for the language (:en, :de, ...) as a symbol or a string. Don't worry if you have languages in your database that are not supported by Xapian. If the language is not supported, XapianDb will fall back to the global language configuration or none, if you haven't configured one. You can place this configuration anywhere, e.g. in an initializer. === Update the index xapian_db injects some helper methods into your configured model classes that update the index automatically for you when you create, save or destroy models. If you already have models that should now go into the index, use the method rebuild_xapian_index: Person.rebuild_xapian_index === Query the index A simple query looks like this: results = XapianDb.search("Foo") You can use wildcards and boolean operators: results = XapianDb.search("Fo*" OR "Baz") You can query attributes: results = XapianDb.search("name:Foo") === Process the results XapianDb.search returns a resultset object. You can access the number of hits directly: results.size # Very fast, does not load the resulting documents If you use a persistent database, the resultset may contain a spelling correction: # Assuming you have at least one document containing "mouse" results = XapianDb.search("moose") results.corrected_query # "mouse" To access the found documents, get a page from the resultset: page = result.paginate # Get the first page with 10 documents page = result.paginate(:page => 2, :per_page => 20) # Get the second page page with documents 21-40 Now you can access the documents: doc = page.first puts doc.domain_class # Get the type of the indexed object, e.g. "Person" puts doc.name # We can access the configured attributes person = doc.indexed_object # Access the object behind this doc (lazy loaded) == What to expect from future releases * facet support * will_paginate support * asynchronous index writer based on {resque}[https://github.com/defunkt/resque] for production environments