Created
July 26, 2011 04:52
-
-
Save mcroydon/1106000 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.postneo | |
package index | |
package test | |
import org.apache.lucene.analysis.SimpleAnalyzer | |
import org.apache.lucene.analysis.standard.StandardAnalyzer | |
import org.apache.lucene.document.{Document, Field} | |
import org.apache.lucene.index.{IndexWriter, Term} | |
import org.apache.lucene.search.{IndexSearcher, Query, TermQuery, TopDocs} | |
import org.apache.lucene.store.RAMDirectory | |
/** | |
* A Somewhat literal translation of TheBasics (http://wiki.apache.org/lucene-java/TheBasics) | |
* from Java to Scala | |
*/ | |
object Test extends Application { | |
// Create an in-memory directory | |
val directory = new RAMDirectory | |
// Create a writer | |
val writer = new IndexWriter(directory, new SimpleAnalyzer, true, IndexWriter.MaxFieldLength.UNLIMITED) | |
// Add some documents | |
var doc = new Document | |
doc.add(new Field("tweet", "Airplanes! @ New Century Airport http://gowal.la/c/4BqgN", Field.Store.YES, Field.Index.ANALYZED)) | |
doc.add(new Field("author", "mc", Field.Store.YES, Field.Index.NOT_ANALYZED)) | |
writer.addDocument(doc) | |
println(writer.numDocs + " document(s) indexed.") | |
writer.close | |
// Create a searcher and get some results | |
val searcher = new IndexSearcher(directory) | |
val query = new TermQuery(new Term("tweet", "airport")) | |
val results = searcher.search(query, null, 10) | |
println(results.totalHits + " result(s).") | |
// Present the first (and only) hit | |
val firstHit = searcher.doc(results.scoreDocs(0).doc) | |
println(firstHit.getField("tweet").stringValue) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment