当前访客身份:游客 [ 登录 | 加入开源中国 ]

代码分享

当前位置:
代码分享 » Scala  » 编程基础
分享到: 
收藏 +0
1
http://solog.co/54/how-to-use-scala-and-lucene-to-create-a-basic-search-application/
标签: Lucene Scala

代码片段(3) [全屏查看所有代码]

1. [代码]创建索引     跳至 [1] [2] [3] [全屏预览]

val analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT)
val directory = new NIOFSDirectory(new java.io.File("tmp/lucene"))
val writer = new IndexWriter(directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED)

val fileLines = io.Source.fromFile("data/tweets.txt").getLines.toList
fileLines foreach { line =>
    writer.addDocument(simpleDoc(line))
}

/** Simple Lucene Document */
private def simpleDoc(text: String) = {
  val doc = new Document()
  doc.add(new Field("tweet", text, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES))
  doc
}

2. [代码]获取最热的关键字     跳至 [1] [2] [3] [全屏预览]

val allTerms = collection.mutable.HashMap[String, Int]()

val reader = IndexReader.open(directory, true)

// create map of popular terms
val terms = reader.terms
while (terms.next) {
  allTerms += terms.term.toString -> terms.docFreq()
}  

// sort map
allTerms.toList sortBy { _._2 } foreach {
  case (key, value) =>
    println(key + ": " + value)
}

3. [代码]搜索     跳至 [1] [2] [3] [全屏预览]

val searcher = new IndexSearcher(directory, true)
val query = new TermQuery(new Term("tweet", q)) 

// perform search, return top 10
val docs = searcher.search(query, 10)
docs.scoreDocs foreach { docId =>
  val d = searcher.doc(docId.doc)
  println(d.get("tweet"))
  println
}


开源中国-程序员在线工具:Git代码托管 API文档大全(120+) JS在线编辑演示 二维码 更多»

发表评论 回到顶部 网友评论(1)

  • 1楼:MUTEX 发表于 2011-06-21 09:23 回复此评论
    代码风格写得基本上还像是Java啊。
开源从代码分享开始 分享代码