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

📄 searcherimpl.java

📁 开源项目simpleoa-0.3.zip是最新版本
💻 JAVA
字号:
package com.ejsun.entapps.core.search.lucene;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;

import com.ejsun.entapps.core.search.SearchUtil;
import com.ejsun.entapps.core.search.Searcher;

/**
 * @author	Quake Wang
 * @since	2004-5-21
 * @version $Revision: 1.5 $
 * 
 **/
public class SearcherImpl implements Searcher {
    private static final Log log = LogFactory.getLog(SearcherImpl.class);
    private Analyzer analyzer;
    private String[] defaultFields;
	private IndexStore indexStore;
	
	public SearcherImpl () {
		indexStore = new IndexStore();
	}
	
    public List search(String query) {
        return search(defaultFields, query);
    }

    public List search(String[] searchFields, String query) {
        List result = new ArrayList();
        if(query == null || query.trim().length() < 1) return result;
		IndexSearcher searcher = null;
        try {
			searcher = indexStore.createSearcher();
            Query luceneQuery = null;
            if (searchFields.length == 1) {
                QueryParser qp = new QueryParser(searchFields[0], analyzer);
                luceneQuery = qp.parse(query);
            } else {
                luceneQuery = MultiFieldQueryParser.parse(query, searchFields, analyzer);
            }

            Hits hits = searcher.search(luceneQuery);
            for (int i = 0; i < hits.length(); i++) {
                Document doc = hits.doc(i);
                result.add(doc.get(SearchUtil.IDENTIFIER));
            }
        } catch (IOException ioe) {
            log.error(ioe);
            //TODO throw a runtime search exception or ignore?             
        } catch (ParseException pe) {
            log.error(pe);
            //TODO throw a runtime search exception or ignore?
        } finally {
        	if(searcher != null){
				try {
                    searcher.close();
                } catch (IOException ioe) {
                    log.warn(ioe);
                }
        	}
        }
        return result;
    }

    public void setAnalyzer(Analyzer analyzer) {
        this.analyzer = analyzer;
    }

    public void setDefaultFields(String[] strings) {
        defaultFields = strings;
    }

}

⌨️ 快捷键说明

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