⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testfilterindexreader.java

📁 索引aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
💻 JAVA
字号:
package org.apache.lucene.index;/** * Copyright 2004 The Apache Software Foundation * * 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 junit.framework.TestCase;import junit.framework.TestSuite;import junit.textui.TestRunner;import junit.framework.TestResult;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.Searcher;import org.apache.lucene.search.Hits;import org.apache.lucene.search.TermQuery;import org.apache.lucene.store.Directory;import org.apache.lucene.store.RAMDirectory;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.analysis.WhitespaceAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import java.util.Collection;import java.io.IOException;public class TestFilterIndexReader extends TestCase {  private static class TestReader extends FilterIndexReader {     /** Filter that only permits terms containing 'e'.*/    private static class TestTermEnum extends FilterTermEnum {      public TestTermEnum(TermEnum termEnum)        throws IOException {        super(termEnum);      }      /** Scan for terms containing the letter 'e'.*/      public boolean next() throws IOException {        while (in.next()) {          if (in.term().text().indexOf('e') != -1)            return true;        }        return false;      }    }        /** Filter that only returns odd numbered documents. */    private static class TestTermPositions extends FilterTermPositions {      public TestTermPositions(TermPositions in)        throws IOException {        super(in);      }      /** Scan for odd numbered documents. */      public boolean next() throws IOException {        while (in.next()) {          if ((in.doc() % 2) == 1)            return true;        }        return false;      }    }        public TestReader(IndexReader reader) {      super(reader);    }    /** Filter terms with TestTermEnum. */    public TermEnum terms() throws IOException {      return new TestTermEnum(in.terms());    }    /** Filter positions with TestTermPositions. */    public TermPositions termPositions() throws IOException {      return new TestTermPositions(in.termPositions());    }  }  /** Main for running test case by itself. */  public static void main(String args[]) {    TestRunner.run (new TestSuite(TestIndexReader.class));  }      /**   * Tests the IndexReader.getFieldNames implementation   * @throws Exception on error   */  public void testFilterIndexReader() throws Exception {    RAMDirectory directory = new RAMDirectory();    IndexWriter writer =      new IndexWriter(directory, new WhitespaceAnalyzer(), true);    Document d1 = new Document();    d1.add(Field.Text("default","one two"));    writer.addDocument(d1);    Document d2 = new Document();    d2.add(Field.Text("default","one three"));    writer.addDocument(d2);    Document d3 = new Document();    d3.add(Field.Text("default","two four"));    writer.addDocument(d3);    writer.close();    IndexReader reader = new TestReader(IndexReader.open(directory));    TermEnum terms = reader.terms();    while (terms.next()) {      assertTrue(terms.term().text().indexOf('e') != -1);    }    terms.close();        TermPositions positions = reader.termPositions(new Term("default", "one"));    while (positions.next()) {      assertTrue((positions.doc() % 2) == 1);    }    reader.close();  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -