Skip to content

Instantly share code, notes, and snippets.

@francois
Created August 21, 2009 21:25
Show Gist options
  • Select an option

  • Save francois/172440 to your computer and use it in GitHub Desktop.

Select an option

Save francois/172440 to your computer and use it in GitHub Desktop.
require "test/unit/assertions"
include Test::Unit::Assertions
# JRuby code to have a lot less to type later
require "java"
require "lucene-core.jar"
import "org.apache.lucene.analysis.standard.StandardAnalyzer"
import "org.apache.lucene.store.RAMDirectory"
import "org.apache.lucene.index.IndexWriter"
import "org.apache.lucene.document.Document"
import "org.apache.lucene.document.Field"
import "org.apache.lucene.search.IndexSearcher"
import "org.apache.lucene.queryParser.QueryParser"
# Here's the overview, but rewritten in JRuby
# Original URL: http://lucene.apache.org/java/2_4_1/api/overview-summary.html#overview_description
analyzer = StandardAnalyzer.new
directory = RAMDirectory.new
iwriter = IndexWriter.new(directory, analyzer, true)
iwriter.setMaxFieldLength(25000);
doc = Document.new
doc.add(Field.new("fieldname", "This is the text to be indexed", Field::Store::YES, Field::Index::TOKENIZED))
iwriter.addDocument(doc)
iwriter.optimize
iwriter.close
isearcher = IndexSearcher.new(directory)
parser = QueryParser.new("fieldname", analyzer)
query = parser.parse("text")
hits = isearcher.search(query)
assert_equal 1, hits.length
(0...hits.length).each do |i|
hitDoc = hits.doc(i)
assert_equal "This is the text to be indexed", hitDoc.get("fieldname")
end
isearcher.close();
directory.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment