Java tutorial
package io.yucca.lucene; /* * Copyright 2014 Rob Sessink * * Licensed 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.File; import java.io.IOException; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.MockAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.TermQuery; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.Version; import org.junit.Test; /** * Set understanding 'VM Arguments' when running as unit test from Eclipse * -Dtests.asserts.gracious=true -ea * * @author <a href="mailto:rsessink@tenbrinke.com">Rob Sessink</a> * @version $Id$ */ public class FieldRemoverTestCase extends LuceneTestCase { static { ClassLoader.getSystemClassLoader().setDefaultAssertionStatus(true); } public final static String TESTDIR = "src/test/resources/test-files/"; public static final String title = "Apache Lucene Core"; public static final String content = "Apache LuceneTM is a high-performance, full-featured text search engine library" + " written entirely in Java. It is a technology suitable for nearly any application that" + " requires full-text search, especially cross-platform."; public static final String url = "http://lucene.apache.org/core/"; /** * Create a new random test index directory * * @param directoryName * File path of index directory * @throws IOException */ public void newFSIndex(File directoryPath) throws IOException { Analyzer analyzer = new MockAnalyzer(random()); Directory directory = FSDirectory.open(directoryPath); RandomIndexWriter writer = new RandomIndexWriter(random(), directory, analyzer); Document doc = new Document(); doc.add(newTextField("dc_title", title, Field.Store.YES)); doc.add(newTextField("dc_content", content, Field.Store.YES)); doc.add(newTextField("dc_identifier", url, Field.Store.YES)); writer.addDocument(doc); writer.close(); directory.close(); } @Test public void testFieldRemoval() throws IOException { File testindex = createTempDir("test"); File cleanindex = createTempDir("clean"); newFSIndex(testindex); String[] fields = { "dc_content" }; FieldRemover remover = new FieldRemover(); remover.removeFields(testindex, cleanindex, fields, Version.LATEST); IndexReader reader = IndexHelper.getIndexReader(cleanindex); IndexSearcher searcher = newSearcher(reader); assertEquals(1, searcher.search(new TermQuery(new Term("dc_title", "apache")), 1).totalHits); assertEquals(0, searcher.search(new TermQuery(new Term("dc_content", "apache")), 1).totalHits); reader.close(); } }