xapian-crystal
Xapian bindings for crystal
Installation
- install xapian
- Compile and install https://github.com/endlessm/xapian-glib
- Add the following to your application's
shard.yml
:
dependencies:
xapian:
github: repomaa/xapian-crystal
Usage
Indexing
require "xapian"
db = Xapian::WritableDatabase.create_or_open("path/to/db")
term_generator = Xapian::TermGenerator.new(db, language: "english")
# Index a document
# Most of the following steps are optional
doc = Xapian::Document.new # create a new document
doc.data = some_data.to_json # add some data to display in results
date = Time.now.to_unix
doc.values[0] = date # add a value for sorting
# Add terms for document
term_generator.set_document(doc)
term_generator.index_text(title, "S") # use predefined title prefix
term_generator.index_text(author, "A") # use predefined author prefix
term_generator.index_text(body) # don't use prefix for body
# Add the document to the database
db.add_document(doc)
# Repeat with as many documents as you like
# ...
# Commit changes
db.commit
Searching
require "xapian"
db = Xapian::Database.new("path/to/db")
query_parser = Xapian::QueryParser.new(db, language: "english")
# Set up the same prefixes as with the term generator
query_parser.add_prefix("title", "S")
query_parser.add_prefix("author", "A")
query = query_parser.parse("author:'Cormac McCarthy'")
enquire = Xapian::Enquire.new(db)
enquire.sort_by_value(0) # sort by date
enquire.results.each do |id, document|
puts({id, document.data})
end
Contributing
- Fork it ( https://github.com/repomaa/xapian/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
Contributors
- repomaa Joakim Repomaa - creator, maintainer