Lucene - 无法使用大写字母查询值

时间:2013-06-28 12:52:26

标签: lucene case

我想索引和搜索较低的关键字。我附上了测试代码,IMHO清楚地表明了我的简单目标。我索引两个单词,一个用大写字母,然后我一个接一个地搜索并打印出来。为此,我创建了Analyzer,只将关键字转换为小写(KeywordAnalyzer不会小写,SimpleAnalyzer会对非字母字符进行拆分。我将此分析器用于IndexWriterQueryParser。但是,出于某种原因,即使我搜索了较低的词语(在示例中为“bye”),我仍然无法使用大写字母返回单词。

计划预期产出:

hello
Bye

实际输出:

hello

有什么问题?

我希望你不介意Scala中的代码。如果不清楚代码的作用,我很乐意帮助您理解。

import org.apache.lucene.store.FSDirectory
import java.io.{Reader, File}
import org.apache.lucene.index._
import org.apache.lucene.document._
import org.apache.lucene.search.IndexSearcher
import org.apache.lucene.queryparser.classic.QueryParser
import org.apache.lucene.analysis.util.CharTokenizer
import org.apache.lucene.analysis.Analyzer
import org.apache.lucene.util.Version
import org.apache.lucene.analysis.Analyzer.TokenStreamComponents

final class LcAnalyzer(lucVer: Version) extends Analyzer {
    def createComponents(fieldName: String, reader: Reader) =
        new TokenStreamComponents(new CharTokenizer(lucVer, reader) {
            def isTokenChar(c: Int) = true
            override def normalize(c: Int) = Character.toLowerCase(c)
        })
}

object LuceneTest {

    val LV = Version.LUCENE_43
    val F = "myf"
    val VALS = Seq("hello", "Bye")

    val indexDir = FSDirectory.open(new File("testindex"))
    val anlz = new LcAnalyzer(LV)

    def main(args: Array[String]) {
        writeData()
        val reader = DirectoryReader.open(indexDir)
        val searcher = new IndexSearcher(reader)
        val p = new QueryParser(LV, F, anlz)

        for (v <- VALS) {
            val hits = searcher.search(p.parse(F + ':' + v), 1).scoreDocs
            for (i <- 0 until hits.length) {
                val doc = searcher.doc(hits(i).doc)
                println(doc.get(F))
            }
        }
    }

    def writeData() {
        val writer = {
            val wc = new IndexWriterConfig(LV, anlz)
            val writer = new IndexWriter(indexDir, wc)
            writer.commit
            writer
        }
        for (v <- VALS) {
            val doc = new Document
            doc.add(new StringField(F, v, Field.Store.YES))
            writer.addDocument(doc)
        }
        writer.commit
        writer.close
    }
}

0 个答案:

没有答案