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

📄 threadsearcher.java

📁 一个功能较为完善的论坛
💻 JAVA
字号:
/*
 * XP Forum
 *
 * Copyright (c) 2002-2003 RedSoft Group.  All rights reserved.
 *
 */
package org.redsoft.forum.search;

import java.io.IOException;
import java.util.Collection;
import java.util.Hashtable;
import java.io.StringReader;

import org.apache.lucene.document.Document;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cn.ChineseAnalyzer;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Hits;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.queryParser.ParseException;

import org.redsoft.forum.ForumConstants;
/**
 * Search thread
 * To use ThreadSearcher:
 *   ThreadSeracher.getInstance().findByTitle("title");
 *
 * Query string syntax
 *   "xp"
 *   "xp AND java"
 *   "xp java" : same as "xp OR java"
 *   "xp OR java"
 *   "jav*"
 *   "面向对象"
 *   
 *
 * @author cinc
 * @version $Id: ThreadSearcher.java,v 1.1.1.1 2003/07/08 08:25:16 cinc Exp $
 */
public class ThreadSearcher{
    /**
     * The solo instance of <code>ThreadSearcher</code>
     */
    private static ThreadSearcher instance = new ThreadSearcher();
    private String indexDir = null;

    /**
     * Private constructor, so can't be new from outside this class
     */
    private ThreadSearcher(){
    }

    /**
     * Init
     *
     * @param   indexDir    the index directory
     */
    public void init( String indexDir ){
        if ( this.indexDir == null ){
            this.indexDir = indexDir;
            System.out.println ("ThreadSearcher inited at directory: " + indexDir);
        }
    }

    /**
     * Get the solo instance of <code>ThreadSearcher</code>
     */
    public static ThreadSearcher getInstance(){
        return instance;
    }

    /**
     * Return threads that match the title
     */
    public Collection findByTitle( String criteria ) throws ParseException, IOException{
        System.out.println("\nSearch title for " + criteria);
        return find( "title", criteria );
    }

    /**
     * Return threads that match the content
     */
    public Collection findByContent( String criteria ) throws ParseException, IOException{
        System.out.println("\nSearch content for " + criteria);
        return find( "content", criteria );
    }

    /**
     * Find threads
     *
     * @param   field       the field the query is performed
     * @param   criteria    the query string
     * @return              the threads match the condition
     */
    public Collection find( String field, String criteria ) throws ParseException, IOException{
        Searcher searcher = new IndexSearcher( indexDir );
        Analyzer analyzer = new ChineseAnalyzer();
  	    Query query = QueryParser.parse(criteria, field, analyzer);
      	Hits hits = searcher.search(query);
       	//System.out.println(hits.length() + " total matching documents");
        Hashtable results = new Hashtable();
        for (int i=0; i<hits.length(); i++){
            Document doc = hits.doc(i);
            //System.out.println(doc.get("title") + " " + doc.get("content"));
            Thread thread = new Thread( doc.get("title"),
                                        doc.get("content"),
                                        doc.get("author"),
                                        doc.get("timestamp"),
                                        doc.get("category"),
                                        doc.get("categoryID"),
                                        doc.get("parentID")
                );
            // avoid duplicate threads
            if (!results.containsKey(thread.getParentID()) ){
                results.put( thread.getParentID(), thread );
            }
        }
       	System.out.println(results.size() + " total matching documents");
        return results.values();
    }

    /**
     * Program entry point, for test purpose
     */
    public static void main(String args[]){
        try{
            String indexDir = args[0] + ForumConstants.INDEX_DIR;
            ThreadSearcher searcher = ThreadSearcher.getInstance();
            searcher.init( indexDir );
            Collection results = searcher.findByTitle( "地" );
            System.out.println ( results.size() );
        }catch (Exception e){
			e.printStackTrace();
        }
    }
}

⌨️ 快捷键说明

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