/xapian-erlang-bindings

Xapian binding for Erlang (GSOC2012 project)

Primary LanguageErlang

Xapian binding for Erlang

License: MIT, GPL2 or higher (Xapian is still under GPL only.)

Author: Uvarov Michael (arcusfelis@gmail.com)

Xapian is an Open Source Search Engine Library, written in C++. Xapian is a highly adaptable toolkit which allows developers to easily add advanced indexing and search facilities to their own applications.

Xapian library

Install Xapian library itself. emerge dev-libs/xapian in Gentoo Linux.

Installation

I use rebar for building.

Try as a stand-alone Erlang application:

git clone git://github.com/arcusfelis/xapian-erlang-bindings.git xapian
cd xapian 
./rebar get-deps compile
./start-dev.sh

Add as a dependency to rebar.config:

Google hash map (optional)

You can use google sparse hash for storing resources' ids.

In the Debian and Ubuntu repositories, this is packaged as libsparsehash-dev.

The C++-preprocessor macro GOOGLE_HASH_MAP enables using google hash map as a hash map.

emerge dev-cpp/sparsehash in Gentoo Linux.

Using

This application uses records, defined in the file include/xapian.hrl. To include it use:

Tests

Next command runs tests:

$ ./rebar eunit skip_deps=true

A pool of readers

Readers use the Poolboy application. There is only one writer for each database, so there is no writer pool. You can use a named process and a supervisor instead:

If you try to run this code from the console, then next command will be useful:

It loads information about records into the console.

A pool is supervised by xapian_sup. That is why calling the xapian_pool:open function does not link the parent process with the new process.

As with xapian_drv:transaction, you can checkout a few pools.

If an error occurs, an exception will be thrown and workers will be returned into the pool.

Multi-database support

You can use this code for opening two databases from the directories "DB1" and "DB2".

Only read-only databases can be used.

There are two fields meaning a document's id: docid and multi_docid. They are equal if only one database is used.

Otherwise, the first field contains a document id (can be repeated) and multi_docid is a unique idintifier, which is calculated from docid and db_number.

db_number is the number of the document's database counting from 1.

db_name field contains pseudonyms of the databases. Information from name field of #x_database{} record will be used for this. This field is undefined by default.

Here is a full multi-database example:

Resources

A resource is a C++ object, which can be passed and stored inside an Erlang VM. Each server can have its own set of resources. Resources from other servers cannot be used or controlled. Resources are not automatically garbidge-collected, but if a control process (server) dies, all its resources are released.

Use the release_resource(Server, Resource) function call to free a resource which is no longer needed.

A second call of this function with the same arguments will cause an error:

Using a port

Ports cannot crash the Erlang VM. The port program will be compiled by rebar.

For running a single server in port mode use:

For running all servers in port mode use:

Testing a port

$ erl -pa ./.eunit/ ./../xapian/ebin ./deps/?*/ebin

Document forms

  • Document Constructor (CD)
  • Extracted Document (ED)
  • Document Id (ID)
  • Document Resource (RD)

Conversations:

  • ID to RD: xapian_server:document(S, ID) -> RD
  • CD to RD: xapian_server:document(S, CD) -> RD
  • DC to EC: xapian_server:document_info(S, DC, Meta) -> EC
  • ID to EC: xapian_server:read_document(S, ID, Meta) -> EC

Helpers

Stand-alone Stemmer

"Z" is a prefix. It means that this term is stemmed.

Code examples

Documentation