0

Lucene でドキュメントを更新していますが、いずれかのフィールドで完全な値を検索しても結果が返されません。単語を 1 つだけ検索すると、結果が返されます。

この例は、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 です。クエリを「Den」または「Haag」に変更すると、1 ヒットになります。

ここにすべてのセットアップと依存関係があります。本で示唆されているように、ライターが空白クエリ アナライザーをどのように使用しているかに注意してください。これが問題ですか?

  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
    }
4

1 に答える 1

0

TermQuery の代わりに PhraseQuery を見たいと思うかもしれません。

于 2014-01-21T22:29:01.587 に答える