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

📄 searchproxy.java

📁 lucene 是java 的版的搜索引擎公共模块
💻 JAVA
字号:
package com.microvois.luence;
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.*;
import org.apache.lucene.analysis.cn.*;


import java.io.*;
import org.apache.log4j.Logger;
import java.util.ArrayList;


public class searchproxy {
    private static final int MAX_RESULTS_SIZE = 1000;
    private static String indexPath = Config.StrDataDirectroy;
    private static IndexReader reader;
    private static Searcher searcher;
    private static Directory searchDirectory = null;
    private static long indexLastModified;
    protected static Analyzer analyzer = new ChineseAnalyzer();
    private static final Logger log = Logger.getLogger(Config.class);

    public static ArrayList executeQuery(String queryString)
    {    
    	return executeQuery("data", queryString);
    }
    public static ArrayList executeQuery(String fieldname , String queryString)
    {
    	ArrayList al = new ArrayList();
    	try
    	{
    	Searcher searcher = getSearcher();
         if (searcher == null) {
             // Searcher can be null if the index doesn't exist.
        	 log.info("searcher is null");
             return al;
         }
         	QueryParser parser=new QueryParser(fieldname,analyzer);
         	//FieldFilter ffil = new FieldFilter(fieldname,)
         	Query query=parser.parse(queryString);
         	Hits result=searcher.search(query);
         
         	 for(int i=0;i<result.length() && i<=MAX_RESULTS_SIZE;i++)
         	 {
         		Document doc=result.doc(i);
         		documentItem item = new documentItem();
         		item.setStrdata(doc.get("data"));
         		item.setStrtitle(doc.get("title"));
         		item.setUrl(doc.get("url"));
         		al.add(item);
         	 }

    	}catch(Exception se)
    	{
    		se.printStackTrace();
    	}
    	return al;
    	
    }
    
    
    public static ArrayList executeQueryURL(String urlstring)
    {
    	ArrayList al = new ArrayList();
    	try
    	{
    	Searcher searcher = getSearcher();
         if (searcher == null) {
             // Searcher can be null if the index doesn't exist.
        	 log.info("searcher is null");
             return al;
         }
         	//QueryParser parser=new QueryParser("url",analyzer);
         	//Query query=parser.parse(queryString);
     		//FieldFilter ffil = new FieldFilter("url",urlstring);
     		Term tm = new Term("url",urlstring);
     		TermQuery tquery = new TermQuery(tm);     		
         	Hits result=searcher.search(tquery);
         
         	 for(int i=0;i<result.length() && i<=MAX_RESULTS_SIZE;i++)
         	 {
         		Document doc=result.doc(i);
         		documentItem item = new documentItem();
         		item.setStrdata(doc.get("data"));
         		item.setStrtitle(doc.get("title"));
         		item.setUrl(doc.get("url"));
         		al.add(item);
         	 }

    	}catch(Exception se)
    	{
    		se.printStackTrace();
    	}
    	return al;
    	
    }
 
    
    
    private static boolean indexExists(String indexPath) {
        File segments = new File(indexPath + File.separator + "segments.gen");
        return segments.exists();
    }
    private static Searcher getSearcher() throws IOException {
      

        if (searcher == null) {
            //Acquire a lock -- analyzer is a convenient object to do this on.
            synchronized(analyzer) {
                if (searcher == null) {
                    if (indexExists(indexPath)) {
                        searchDirectory = FSDirectory.getDirectory(indexPath);
                        reader = IndexReader.open(searchDirectory);
                        indexLastModified = reader.lastModified(searchDirectory);
                        searcher = new IndexSearcher(reader);
                    }
                    //Otherwise, the index doesn't exist, so return null.
                    else {
                        return null;
                    }
                }
            }
        }
        if (reader.lastModified(searchDirectory) > indexLastModified) {
            synchronized (analyzer) {
                if (reader.lastModified(searchDirectory) > indexLastModified) {
                    if (indexExists(indexPath)) {
                        indexLastModified = reader.lastModified(searchDirectory);
                        //We need to close the indexReader because it has changed.
                        //Re-opening it will make changes visible.
                        reader.close();

                        searchDirectory = FSDirectory.getDirectory(indexPath);
                        reader = IndexReader.open(searchDirectory);
                        searcher = new IndexSearcher(reader);
                    }
                    //Otherwise, the index doesn't exist, so return null.
                    else {
                        return null;
                    }
                }
            }
        }
        return searcher;
    }

    public static synchronized void close() 
    {
    	try
    	{
	    	if(reader != null)
	    	{
	    		reader.close();    		
	    	}
	    	
	    	if(searchDirectory !=null)
	    	{
	    		searchDirectory.close();	    		
	    	}
	    	
    	}catch(Exception see)
    	{
    		see.printStackTrace();
    	}
    	
    }
    public static void main(String args[])
    {
    	ArrayList al = searchproxy.executeQuery("data","百科飞");
		System.out.println ("length="+al.size());    		
	    	
    	for(int i=0;i<al.size();i++)
    	{ 
    		documentItem item = (documentItem)al.get(i);		
    	}
    	
    	
    }
}

⌨️ 快捷键说明

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