Switch branches/tags
release404401_rc1 release4401test_t1 release4401_t1 release4401_release_point release4401_rc4 release4401_rc3 release4401_rc2 release4401_rc1 release491_t15 release491_t14 release491_t13 release491_t12 release491_t11 release491_t10 release491_t9 release491_t8 release491_t7 release491_t6 release491_t5 release491_t4 release491_t3 release491_t2 release491_t1 release451_t1 release410_t1 release410_rc6 release410_rc5 release410_rc4 release410_rc3 release410_rc2 release410_rc1 release49_t5 release49_t4 release49_t3 release49_t2 release49_t1 release49_release_point release49_rc4 release49_rc3 release49_rc2 release49_rc1 release48_t3 release48_t2 release48_t1 release48_release_point release48_rc1 release47_t3 release47_t2 release47_t1 release47_release_point release47_rc5 release47_rc4 release47_rc3 release47_rc2 release47_rc1 release46_release_point release46_rc4 release46_rc3 release46_rc2 release46_rc1 release45_t4 release45_t3 release45_t2 release45_t1 release45_rc1 release44b_release_point release44b_rc2 release44b_rc1 release44a_rc1 release6.3.0_t2 release6.3.0_t1 release6.2.3_t1 release6.2.3_release_point release6.2.3_rc2 release6.2.3_rc1 release6.2.2_t2 release6.2.2_t1 release6.2.2_release_point release6.2.2_rc3 release6.2.2_rc2 release6.2.2_rc1 release6.2.1_t5 release6.2.1_t4 release6.2.1_t3 release6.2.1_t2 release6.2.1_t1 release6.2.1_release_point release6.2.1_rc1 release6.2.0_t1 release6.2.0_rc4 release6.2.0_rc3 release6.2.0_rc2 release6.2.0_rc1 release6.1.6_release_point release6.1.6_rc5 release6.1.6_rc4 release6.1.6_rc3 release6.1.6_rc2 release6.1.6_rc1 release6.1.5_t1
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
140 lines (91 sloc) 4.36 KB

This is a Solr binding for Allegro CL. Solr is an open-source freetext indexing/searching platform from the Apache Lucene project. See the following URL for its details.

This package allows Allegro CL applications to communicate with a running Solr server, add and delete documents, and run queries to retrieve indexed records.

The package comes with a solr.asd file. To use it just load :solr.

(push #p"path/to/solr/source/directory" asdf:*central-registry*)
(asdf:load-system :solr)

Accessing the database

The Solr server should be running. To access the server, you need to create an instance of solr with the endpoint url. For example, if the server is running on localhost:8983, you can say:

(defvar *solr* (make-instance 'solr :uri "http://localhost:8983/solr"))

This action itself doesn't actually connect to the database, but the instance solr can be passed to other solr APIs to access to the database.

Adding documents

To add a document, you can use solr-add and solr-add* API.

(solr-add *solr* '((:id . 123) (:name . "foobar") (:author . "xyzzy")))

(solr-add* *solr* list-of-records)

This adds the document with id=123, name="foobar" and author="xyzzy". The document record is semantically an unordered collection of named fields; you can pass an alist or a hashtable as a record. Field names are represented by keywords. Field values are mapped as follows:

Lisp numbers => Solr numbers
Lisp date-time object => Solr Datetime
Lisp strings => Solr text
Lisp t and nil => Solr boolean

Non-empty Lisp lists can be used to represent set of values.

By default, solr-add and solr-add* do not commit the change. solr-commit commits pending changes:

(solr-commit *solr*)

Or, you can discard uncommitted changes by solr-rollback:

(solr-rollback *solr*)

For convenience, solr-add and solr-add* accept a keyword argument commit, that automatically commits the change before returning:

(solr-add* *solr* list-of-records :commit t)

If you're adding large amount of documents, it is a good idea to send a bunch of documents together before committing using solr-add*, because it is much faster than adding documents one by one.

Occasionally you may want to call solr-optimize to optimize indices for faster query performance:

(solr-optimize *solr*)

Deleting documents

You can delete documents by listing document ids, or specifying queries:

(solr-delete *solr* :ids '(1 13 17))

(solr-delete *solr* :queries '("author:Shiro"))

The deletion takes effects after committing. solr-delete accepts the commit keyword argument for autocommit.

Querying documents

solr-query does the query, and takes large number of keyword arguments to customize the query. See the Solr documentation for the full set of features. Here's an example of solr-query call:

(solr-query *solr* :query "author:Shiro"
            :fields "id,name,author"
            :param-alist '((:rows . 100)))

It returns LXML, a S-expression representaton of XML. It's up to the caller to extract necessary information from the returned LXML, but we provide a few convenience procedures for some basic extraction.

(solr-result->response-count lxml)

This returns three values: the total number of matching documents, the starting record number, and the number of documents included in the response. Note that Solr does pagenation by default--if you don't pass the :rows keyword it will only return the first 10 matching records. To retrieve other documents you need to pass the :start keyword to solr-query.

Information on matching documents is in :doc XML elements.

(solr-result->doc-nodes lxml)

This procedure extracts and returns the list of doc elements, on which you can map to dig further information.

The extracted doc elements are still LXML. The following procedure further converts the field values to Lisp objects according to the LXML attributes:

(solr-result->doc-alist lxml)

Error handling

When the Solr server returns an error (e.g. invalid query format), a condition solr-error is raised. It contains the Solr response status code, response headers and response body. The response body is in LXML.

When the API failed to communicate with Solr server (e.g. the server isn't running), a socket-error condition is raised.