Created
June 17, 2014 10:59
-
-
Save krishnabhargav/bc40de5630c95d4e5f90 to your computer and use it in GitHub Desktop.
Lucene 4.3 Example
This file contains hidden or 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
import scala.Array.canBuildFrom | |
import org.apache.lucene.analysis.standard.StandardAnalyzer | |
import org.apache.lucene.document.Document | |
import org.apache.lucene.document.Field | |
import org.apache.lucene.document.TextField | |
import org.apache.lucene.index.DirectoryReader | |
import org.apache.lucene.index.IndexWriter | |
import org.apache.lucene.index.IndexWriterConfig | |
import org.apache.lucene.queryparser.flexible.standard.StandardQueryParser | |
import org.apache.lucene.search.IndexSearcher | |
import org.apache.lucene.search.TopScoreDocCollector | |
import org.apache.lucene.store.RAMDirectory | |
import org.apache.lucene.util.Version | |
object LuceneMain { | |
def main(args: Array[String]) { | |
val luceneIndexDir = new RAMDirectory | |
//Indexing content | |
val analyzer = new StandardAnalyzer(Version.LUCENE_43) | |
val indexWriterConfig = new IndexWriterConfig(Version.LUCENE_43, analyzer) | |
val indexWriter = new IndexWriter(luceneIndexDir, indexWriterConfig) | |
val document = new Document() | |
document.add(new TextField("title", "Lucene Architecture Notes", Field.Store.YES)) | |
document.add(new TextField("content", "Lucene Searching", Field.Store.YES)) | |
indexWriter addDocument document | |
indexWriter.close | |
//Time to search | |
val indexReader = DirectoryReader open luceneIndexDir | |
println(s"Total number of documents : ${indexReader.numDocs}") | |
val searcher = new IndexSearcher(indexReader) | |
val qp = new StandardQueryParser() | |
val query = qp parse ("lucene", "title") | |
val resultsCollector = TopScoreDocCollector create (100, true) | |
searcher search (query, resultsCollector) | |
val result = resultsCollector.topDocs().scoreDocs | |
println(s"Found ${result.length} results") | |
for { | |
each <- result | |
doc = searcher.doc(each.doc) | |
} println(s"${doc.get("title")}") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment