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

📄 search.java

📁 lucene 是java 的版的搜索引擎公共模块
💻 JAVA
字号:
package com.bitmechanic.spindle;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.search.Query;import org.apache.lucene.search.Hits;import org.apache.lucene.search.Searcher;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.queryParser.QueryParser;import java.util.ArrayList;import java.util.HashMap;import javax.servlet.jsp.PageContext;import com.bitmechanic.listlib.*;/** * Use this class to search indexes created by the Spider class. * * @author James Cooper * @version $Id: Search.java,v 1.5 2002/03/30 20:25:46 pixel Exp $ */public class Search implements ListCreator {     /**     * Command line usage:     *      * <code>java com.bitmechanic.spindle.Search [index dir] [keyword]</code>     *     */    public static void main(String[] args) throws Exception {        String indexPath = args[0];        String queryString = args[1];        Searcher searcher = new IndexSearcher(indexPath);        //Query q = searcher.        QueryParser parser=new QueryParser("body",new StandardAnalyzer());     	Query q=parser.parse(queryString);        //parse(queryString, "body", new StandardAnalyzer());        Hits hits = searcher.search(q);        for (int i = 0; i < hits.length(); i++) {            System.out.println(hits.doc(i).get("url") + "; Title: " +                               hits.doc(i).get("title") + "; Score: " +                                hits.score(i));            System.out.println("Description: " + hits.doc(i).get("desc"));        };        searcher.close();    }    private static final String attrib[] = { "url", "title", "desc" };    /**     * Runs a query against the index in the directory specified.  Returns     * a list of HashMap objects.  The objects have the following keys:     * 'url', 'title', 'desc', 'score'.      *     * @param dir Directory containing the index to search     * @param query Query to run against the index      */    public static ArrayList search(String dir, String query) throws Exception {        ListDesc desc = search(dir, query, 0, Integer.MAX_VALUE);        return desc.list;    }    /**     * Runs a query against the index in the directory specified.  Returns     * a list of HashMap objects.  The objects have the following keys:     * 'url', 'title', 'desc', 'score'.      *     * @param dir Directory containing the index to search     * @param query Query to run against the index      * @param offset Position in the result set to start returning results     * @param max Max number of results to return     */    public static ListDesc search(String dir, String query, int offset,                                   int max) throws Exception {        Searcher searcher = new IndexSearcher(dir);       // Query q = QueryParser.parse(query, "body", new StandardAnalyzer());                                          QueryParser parser=new QueryParser("body",new StandardAnalyzer());     	Query q=parser.parse(query);     	        Hits h = searcher.search(q);        ArrayList list = new ArrayList(h.length());        for (int i = offset; list.size() < max && i < h.length(); i++) {            HashMap map = new HashMap();            for (int x = 0; x < attrib.length; x++) {                map.put(attrib[x], h.doc(i).get(attrib[x]));            }            map.put("score", new Float(h.score(i)));            list.add(map);        }        searcher.close();        ListDesc desc = new ListDesc();        desc.list = list;        desc.count = h.length();        return desc;    }    ////////////////////////////////////////////////////////////////////////    //    // ListCreator implementation    //    ////////////////////////////////////////////////////////////////////////    private String _dir, _query;    public void setDir(String dir) {        _dir = dir;    }    public void setQuery(String query) {        _query = query;    }    /**     * Runs a query against the index in the directory specified.  Returns     * a list of HashMap objects.  The objects have the following keys:     * 'url', 'title', 'desc', 'score'.      *     * @param dir Directory containing the index to search     * @param query Query to run against the index      * @param offset Position in the result set to start returning results     * @param max Max number of results to return     */    public ListContainer execute(PageContext context, int offset, int max)         throws Exception {        ListDesc desc = search(_dir, _query, offset, max);        return new ListContainer(desc.list.iterator(), desc.count);    }}

⌨️ 快捷键说明

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