2013-07-08 90 views
0

我在Lucene中更新文檔,但是當我在其中一個字段中搜索完整值時,沒有結果返回。如果我只搜索一個詞,那麼我會得到一個結果。Lucene白色空間分析器忽略短語?

這個例子來自Lucene in Action 2nd Edition一書的第2章,我使用的是Lucene 3 Java庫。

這裏的主要邏輯

"Document fields show new value when updated, and not old value" in { 
     getHitCount("city", "Amsterdam") must equal(1) 

     val update = new Document 
     update add new Field("id", "1", Field.Store.YES, Field.Index.NOT_ANALYZED) 
     update add new Field("country", "Netherlands", Field.Store.YES, Field.Index.NO) 
     update add new Field("contents", "Den Haag has a lot of museums", Field.Store.NO, Field.Index.ANALYZED) 
     update add new Field("city", "Den Haag", Field.Store.YES, Field.Index.ANALYZED) 

     wr updateDocument(new Term("id", "1"), update) 
     wr close 

     getHitCount("city", "Amsterdam") must equal(0) 
     getHitCount("city", "Den Haag") must equal(1) 
    } 

這是最後一行在上面的失敗 - 命中次數爲0。如果我更改查詢要麼「小室」或「海牙」然後我得到一重擊。

以下是所有設置和依賴關係。請注意作者如何使用空白查詢分析器,正如本書所示。這是問題嗎?

override def beforeEach{ 
     dir = new RAMDirectory 

     val wri = writer 
     for (i <- 0 to ids.length - 1) { 
      val doc = new Document 
      doc add new Field("id", ids(i), Field.Store.YES, Field.Index.NOT_ANALYZED) 
      doc add new Field("country", unindexed(i), Field.Store.YES, Field.Index.NO) 
      doc add new Field("contents", unstored(i), Field.Store.NO, Field.Index.ANALYZED) 
      doc add new Field("city", text(i), Field.Store.YES, Field.Index.ANALYZED) 
      wri addDocument doc 
     } 
     wri close 

     wr = writer 
    } 

var dir: RAMDirectory = _ 
    def writer = new IndexWriter(dir, new WhitespaceAnalyzer, IndexWriter.MaxFieldLength.UNLIMITED) 
    var wr: IndexWriter = _ 

def getHitCount(field: String, q: String): Int = { 
     val searcher = new IndexSearcher(dir) 
     val query = new TermQuery(new Term(field, q)) 
     val hitCount = searcher.search(query, 1).totalHits 
     searcher.close() 
     hitCount 
    } 

回答

0

您可能想看看PhraseQuery而不是TermQuery。