我想索引和搜索小写关键字。我附上了测试代码,恕我直言清楚地展示了我的简单目标。我索引了两个单词,一个用大写字母,然后我一一搜索并打印回来。为此,我创建了Analyzer
它只是将关键字转换为小写(KeywordAnalyzer
不小写并SimpleAnalyzer
拆分非字母字符)。我将此分析器用于IndexWriter
和QueryParser
。但是,由于某种原因,即使我搜索小写单词(示例中的“再见”),我也无法返回带有大写字母的单词。
程序预期输出:
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
}
}