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

📄 dbquery.java

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

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

import java.util.Iterator;
import java.util.ArrayList;
import java.io.*;
import net.ijsp.news.search.MultiFilter;
import net.ijsp.news.search.FieldFilter;
import net.ijsp.news.search.Query;
import net.ijsp.news.database.*;
import net.ijsp.news.news.*;

import org.apache.lucene.analysis.*;
import org.apache.lucene.analysis.standard.*;
import org.apache.lucene.document.*;
import org.apache.lucene.store.*;
import org.apache.lucene.search.*;
import org.apache.lucene.queryParser.*;
import org.apache.lucene.index.*;

public class DbQuery implements Query {

	private String queryString ;		// 关键字
	private ArrayList results = null;	// 查询结果,新闻文件名
    private static String indexPath = null;
	private static Searcher searcher;
	private static Directory searchDirectory = null;
	private static IndexReader reader;
	private static long indexLastModified;
	private int classID = 0;

    public String getQueryString() {
		return queryString;
	}

    public void setQueryString(String queryString) {
		this.queryString = queryString;
		results = null;
	}

	/**
	* @return 符合条件的记录数
	*/
    public int resultCount() {
		 if (results == null) {
            executeQuery();
        }
		return results.size();
	}

	/**
	* @return 符合条件的新闻
	*/
    public Iterator results() {
		if(results == null) {
			executeQuery();
		}
		return results.iterator();
	}

	/**
	* @return 符合条件的新闻(分页)
	*/
    public Iterator results(int startIndex, int numResults) {
		if(results == null) {
			executeQuery();
		}

		int endIndex = startIndex + numResults - 1 ;
        if (endIndex > results.size() - 1) {
            endIndex = results.size() - 1;
        }
		ArrayList newArrayList = new ArrayList();
		for(int i=startIndex; i<endIndex; i++) {
			newArrayList.add(results.get(i));
		}
		return newArrayList.iterator();
	}

	/**
	* 设置query条件(栏目分类)
	*/
	public void setClassID(int classID) {
		this.classID = classID;
	}


   /**
   * 执行查询,将结果存入数组
   */
    private void executeQuery() {
	    try {
            Searcher searcher = getSearcher();
            if (searcher == null) {
               return;
            }

            org.apache.lucene.search.Query subjectQuery =
                QueryParser.parse(queryString, "subject", DbSearchManager.analyzer);

            org.apache.lucene.search.Query bodyQuery =
                QueryParser.parse(queryString, "body", DbSearchManager.analyzer);

            BooleanQuery comboQuery = new BooleanQuery();
            comboQuery.add(subjectQuery,false,false);
            comboQuery.add(bodyQuery,false,false);

            MultiFilter multiFilter = new MultiFilter(1);
            int filterCount = 0;
	        Hits hits;
			
			// 只对栏目进行search
		    if (classID != 0) {
                multiFilter.add(new FieldFilter("newsClass", String.valueOf(classID)));
                filterCount++;
            }

            if (filterCount > 0) {
                hits = searcher.search(comboQuery, multiFilter);
            } else {
				hits = searcher.search(comboQuery);
            }

			int numResults = hits.length() ;

			results = new ArrayList();
			Factory factory  = Factory.getInstance();
            for (int i=0; i<numResults; i++) {
				News news = factory.getNews();
				news.setTitle(((Document)hits.doc(i)).get("subject"));
				news.setNewsFile(((Document)hits.doc(i)).get("newsPath"));
				results.add(news);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }


  private static Searcher getSearcher() throws IOException {
        if (indexPath == null) {
           LoadProp loadProp = new LoadProp();
		   loadProp = loadProp.prop();
		   indexPath = loadProp.getIndexPath();
        }
        if (searcher == null) {
              synchronized(DbSearchManager.analyzer) {
                if (searcher == null) {
                    if (indexExists(indexPath)) {
                        searchDirectory = FSDirectory.getDirectory(indexPath, false);
                        reader = IndexReader.open(searchDirectory);
                        indexLastModified = reader.lastModified(searchDirectory);
                        searcher = new IndexSearcher(reader);
                    } else {
                        return null;
                    }
                }
            }
        }
     return searcher;
    }

    private static boolean indexExists(String indexPath) {
	    File segments = new File(indexPath + File.separator + "segments");
        return segments.exists();
    }
}

⌨️ 快捷键说明

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