📄 testindexwriterdelete.java
字号:
package org.apache.lucene.index;/** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */import java.io.IOException;import java.util.Arrays;import junit.framework.TestCase;import org.apache.lucene.analysis.WhitespaceAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.search.Hits;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.TermQuery;import org.apache.lucene.store.Directory;import org.apache.lucene.store.MockRAMDirectory;import org.apache.lucene.store.RAMDirectory;public class TestIndexWriterDelete extends TestCase { // test the simple case public void testSimpleCase() throws IOException { String[] keywords = { "1", "2" }; String[] unindexed = { "Netherlands", "Italy" }; String[] unstored = { "Amsterdam has lots of bridges", "Venice has lots of canals" }; String[] text = { "Amsterdam", "Venice" }; for(int pass=0;pass<2;pass++) { boolean autoCommit = (0==pass); Directory dir = new RAMDirectory(); IndexWriter modifier = new IndexWriter(dir, autoCommit, new WhitespaceAnalyzer(), true); modifier.setUseCompoundFile(true); modifier.setMaxBufferedDeleteTerms(1); for (int i = 0; i < keywords.length; i++) { Document doc = new Document(); doc.add(new Field("id", keywords[i], Field.Store.YES, Field.Index.UN_TOKENIZED)); 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.TOKENIZED)); doc .add(new Field("city", text[i], Field.Store.YES, Field.Index.TOKENIZED)); modifier.addDocument(doc); } modifier.optimize(); if (!autoCommit) { modifier.close(); } Term term = new Term("city", "Amsterdam"); int hitCount = getHitCount(dir, term); assertEquals(1, hitCount); if (!autoCommit) { modifier = new IndexWriter(dir, autoCommit, new WhitespaceAnalyzer()); modifier.setUseCompoundFile(true); } modifier.deleteDocuments(term); if (!autoCommit) { modifier.close(); } hitCount = getHitCount(dir, term); assertEquals(0, hitCount); if (autoCommit) { modifier.close(); } dir.close(); } } // test when delete terms only apply to disk segments public void testNonRAMDelete() throws IOException { for(int pass=0;pass<2;pass++) { boolean autoCommit = (0==pass); Directory dir = new RAMDirectory(); IndexWriter modifier = new IndexWriter(dir, autoCommit, new WhitespaceAnalyzer(), true); modifier.setMaxBufferedDocs(2); modifier.setMaxBufferedDeleteTerms(2); int id = 0; int value = 100; for (int i = 0; i < 7; i++) { addDoc(modifier, ++id, value); } modifier.flush(); assertEquals(0, modifier.getRamSegmentCount()); assertTrue(0 < modifier.getSegmentCount()); if (!autoCommit) { modifier.close(); } IndexReader reader = IndexReader.open(dir); assertEquals(7, reader.numDocs()); reader.close(); if (!autoCommit) { modifier = new IndexWriter(dir, autoCommit, new WhitespaceAnalyzer()); modifier.setMaxBufferedDocs(2); modifier.setMaxBufferedDeleteTerms(2); } modifier.deleteDocuments(new Term("value", String.valueOf(value))); modifier.deleteDocuments(new Term("value", String.valueOf(value))); if (!autoCommit) { modifier.close(); } reader = IndexReader.open(dir); assertEquals(0, reader.numDocs()); reader.close(); if (autoCommit) { modifier.close(); } dir.close(); } } // test when delete terms only apply to ram segments public void testRAMDeletes() throws IOException { for(int pass=0;pass<2;pass++) { boolean autoCommit = (0==pass); Directory dir = new RAMDirectory(); IndexWriter modifier = new IndexWriter(dir, autoCommit, new WhitespaceAnalyzer(), true); modifier.setMaxBufferedDocs(4); modifier.setMaxBufferedDeleteTerms(4); int id = 0; int value = 100; addDoc(modifier, ++id, value); modifier.deleteDocuments(new Term("value", String.valueOf(value))); addDoc(modifier, ++id, value); modifier.deleteDocuments(new Term("value", String.valueOf(value))); assertEquals(2, modifier.getNumBufferedDeleteTerms()); assertEquals(1, modifier.getBufferedDeleteTermsSize()); addDoc(modifier, ++id, value); assertEquals(0, modifier.getSegmentCount()); modifier.flush(); if (!autoCommit) { modifier.close(); } IndexReader reader = IndexReader.open(dir); assertEquals(1, reader.numDocs()); int hitCount = getHitCount(dir, new Term("id", String.valueOf(id))); assertEquals(1, hitCount); reader.close(); if (autoCommit) { modifier.close(); } dir.close(); } } // test when delete terms apply to both disk and ram segments public void testBothDeletes() throws IOException { for(int pass=0;pass<2;pass++) { boolean autoCommit = (0==pass); Directory dir = new RAMDirectory(); IndexWriter modifier = new IndexWriter(dir, autoCommit, new WhitespaceAnalyzer(), true); modifier.setMaxBufferedDocs(100); modifier.setMaxBufferedDeleteTerms(100); int id = 0; int value = 100; for (int i = 0; i < 5; i++) { addDoc(modifier, ++id, value); } value = 200; for (int i = 0; i < 5; i++) { addDoc(modifier, ++id, value); } modifier.flush(); for (int i = 0; i < 5; i++) { addDoc(modifier, ++id, value); } modifier.deleteDocuments(new Term("value", String.valueOf(value))); modifier.flush(); if (!autoCommit) { modifier.close(); } IndexReader reader = IndexReader.open(dir); assertEquals(5, reader.numDocs()); if (autoCommit) { modifier.close(); } } } // test that batched delete terms are flushed together public void testBatchDeletes() throws IOException { for(int pass=0;pass<2;pass++) { boolean autoCommit = (0==pass); Directory dir = new RAMDirectory(); IndexWriter modifier = new IndexWriter(dir, autoCommit, new WhitespaceAnalyzer(), true); modifier.setMaxBufferedDocs(2); modifier.setMaxBufferedDeleteTerms(2); int id = 0; int value = 100; for (int i = 0; i < 7; i++) { addDoc(modifier, ++id, value); } modifier.flush(); if (!autoCommit) { modifier.close(); } IndexReader reader = IndexReader.open(dir); assertEquals(7, reader.numDocs()); reader.close(); if (!autoCommit) { modifier = new IndexWriter(dir, autoCommit, new WhitespaceAnalyzer()); modifier.setMaxBufferedDocs(2); modifier.setMaxBufferedDeleteTerms(2); } id = 0; modifier.deleteDocuments(new Term("id", String.valueOf(++id))); modifier.deleteDocuments(new Term("id", String.valueOf(++id))); if (!autoCommit) { modifier.close(); } reader = IndexReader.open(dir); assertEquals(5, reader.numDocs()); reader.close();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -