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

📄 fieldfilter.java

📁 一个用jsp技术实现的新闻发布系统
💻 JAVA
字号:
package net.ijsp.news.search;

/**
*  搜索
*  @author: ccjsmile
*  Company: http://www.ijsp.net
*  Copyright: Copyright (c) 2003
*  @version 1.0 beta
*/

import java.util.BitSet;
import java.io.IOException;

import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermDocs;
import org.apache.lucene.index.IndexReader;

public class FieldFilter extends org.apache.lucene.search.Filter {

    private Term [] searchTerms;

    public FieldFilter(String field, String [] values) {
        searchTerms = new Term[values.length];
        for (int i=0; i<values.length; i++) {
            searchTerms[i] = new Term(field, values[i]);
        }
    }

    public FieldFilter(String field, String value) {
        this(field, new String[] { value });
    }

    public BitSet bits(IndexReader reader) throws IOException {
        //Create a new BitSet with a capacity equal to the size of the index.
        BitSet bits = new BitSet(reader.maxDoc());
        //Match all search terms.
        for (int i=0; i < searchTerms.length; i++) {
            //Get an enumeration of all the documents that match the specified
            //field value.
            TermDocs matchingDocs = reader.termDocs(searchTerms[i]);
            try {
                if (matchingDocs != null) {
                    while(matchingDocs.next()) {
                        bits.set(matchingDocs.doc());
                    }
                }
            }
            finally {
                if (matchingDocs != null) {
                    matchingDocs.close();
                }
            }
        }
        return bits;
    }
}

⌨️ 快捷键说明

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