`FieldValueQuery`是错误的

时间:2016-01-29 20:02:10

标签: lucene

或者我误解了这堂课的用法。看看下面的代码......(它在Scala中,但应该很容易理解这个想法)

import org.apache.lucene.store._
import org.apache.lucene.document._
import org.apache.lucene.index._
import org.apache.lucene.analysis.core._
import org.apache.lucene.search._

val directory = new RAMDirectory()
val config = new IndexWriterConfig(new WhitespaceAnalyzer())
val writer = new IndexWriter(directory, config)
writer.addDocument({
  val document = new Document()
  document.add(new StringField("foo", "bar", Field.Store.YES))
  document
})
writer.commit()

val searcher = new IndexSearcher(DirectoryReader.open(directory))

{
  val query = new ConstantScoreQuery(new FieldValueQuery("foo"))
  Console.println(searcher.search(query, 1).totalHits)
}

{
  val query = new TermQuery(new Term("foo", "bar"))
  Console.println(searcher.search(query, 1).totalHits)
}

输出是,

[info] 0
[info] 1

这是一个错误还是我遗漏了什么? (我正在使用Lucene 5.4.1)

1 个答案:

答案 0 :(得分:2)

FieldValueQuery检查该字段是否有DocValue,而不是传统的索引/存储字段内容。如果您向文档添加DocValuesField,您应该会看到它获得查询的结果,例如:

val document = new Document()
document.add(new StringField("foo", "bar", Field.Store.YES))
document.add(new SortedDocValuesField("foo", new BytesRef("bar"))
document

查看the DocValues APIthis blog post introducing them,了解有关DocValues的所有信息。