huangfox

          韜光隱晦
          隨筆 - 1, 文章 - 8, 評論 - 1, 引用 - 0
          數(shù)據(jù)加載中……

          有關(guān)Lucene的問題(6):Lucene的事務(wù)性【轉(zhuǎn)】

            所謂事務(wù)性,本多指數(shù)據(jù)庫的屬性,包括ACID四個(gè)基本要素:原子性(Atomicity)、一致性(Consistency)、隔離性(Isolation)、持久性(Durability)。

            我們這里主要討論隔離性,Lucene的IndexReader和IndexWriter具有隔離性。

            當(dāng)IndexReader.open打開一個(gè)索引的時(shí)候,相對于給當(dāng)前索引進(jìn)行了一次snapshot,此后的任何修改都不會(huì)被看到。

            僅當(dāng)IndexReader.open打開一個(gè)索引后,才有可能看到從上次打開后對索引的修改。

            當(dāng)IndexWriter沒有調(diào)用Commit的時(shí)候,其修改的內(nèi)容是不能夠被看到的,哪怕IndexReader被重新打開。

            欲使最新的修改被看到,一方面IndexWriter需要commit,一方面IndexReader重新打開。

            下面我們舉幾個(gè)例子來說明上述隔離性:

            (1) 首先做準(zhǔn)備,索引十篇文檔

            File indexDir = new File("TestIsolation/index");
            IndexWriter writer = new IndexWriter(FSDirectory.open(indexDir), new StandardAnalyzer(Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.LIMITED);
            for(int i =0; i < 10; i++){
            indexDocs(writer);
            }
            writer.close();

            (2) 然后再索引十篇文檔,并不commit

            writer = new IndexWriter(FSDirectory.open(indexDir), new StandardAnalyzer(Version.LUCENE_CURRENT), IndexWriter.MaxFieldLength.LIMITED);
            for(int i =0; i < 10; i++){
            indexDocs(writer);
            }

            (3) 打開一個(gè)IndexReader,但是由于IndexWriter沒有commit,所以仍然僅看到十篇文檔。

            IndexReader reader = IndexReader.open(FSDirectory.open(indexDir));
            IndexSearcher searcher = new IndexSearcher(reader);
            TopDocs docs = searcher.search(new TermQuery(new Term("contents","hello")), 50);
            System.out.println(docs.totalHits);

            (4) IndexWriter進(jìn)行提交commit

            writer.commit();

            (5) 不重新打開IndexReader,進(jìn)行搜索,仍然僅看到十篇文檔。

            docs = searcher.search(new TermQuery(new Term("contents","hello")), 50);
            System.out.println(docs.totalHits);

            (6) IndexReader重新打開,則可以看到二十篇文檔。

            reader = IndexReader.open(FSDirectory.open(indexDir));
            searcher = new IndexSearcher(reader);
            docs = searcher.search(new TermQuery(new Term("contents","hello")), 50);
            System.out.println(docs.totalHits);

          posted on 2010-09-25 16:02 fox009 閱讀(161) 評論(0)  編輯  收藏 所屬分類: 搜索引擎技術(shù)

          主站蜘蛛池模板: 锦屏县| 赞皇县| 和平县| 绍兴市| 宝应县| 镶黄旗| 尼玛县| 泊头市| 灵璧县| 古田县| 昌都县| 古浪县| 吕梁市| 普陀区| 灌阳县| 那坡县| 驻马店市| 三门峡市| 林芝县| 什邡市| 梅州市| 平度市| 浮梁县| 兴安盟| 拉萨市| 光泽县| 牡丹江市| 汽车| 营山县| 唐山市| 平南县| 勃利县| 时尚| 安徽省| 陕西省| 大同县| 溧水县| 始兴县| 白河县| 大洼县| 讷河市|