com.ivannotes.searchbee.SearchBee.java Source code

Java tutorial

Introduction

Here is the source code for com.ivannotes.searchbee.SearchBee.java

Source

/*
 * Copyright 2012 www.ivannotes.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/
package com.ivannotes.searchbee;

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.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;

/**
 * @author miracle.ivanlee@gmail.com since Aug 13, 2012
 * 
 * @param <T>
 */
public abstract class SearchBee<T> {

    private final static int MAX_RESULT = 500;

    protected abstract IndexWriter getIndexWriter();

    private Log logger = LogFactory.getLog(SearchBee.class);

    public boolean isIndexExists() {
        throw new RuntimeException("Unsupported function. ");
    }

    public final void doIndex(DataFetcher<T> df) throws CorruptIndexException, IOException {
        df.reset();
        IndexWriter idxWriter = getIndexWriter();
        int contiuousException = 0;
        try {
            while (df.hasMore()) {
                try {
                    List<T> data = df.fetchData();
                    for (T bean : data) {
                        Document doc = buildDocument(bean);
                        idxWriter.addDocument(doc);
                    }

                    idxWriter.commit();
                    contiuousException = 0;
                } catch (Exception e) {
                    contiuousException++;
                    logger.error("build index error", e);
                    if (contiuousException > 100) {
                        logger.error("build index exceed max continuous exception count(100), exit build.");
                        break;
                    }
                }
            }
        } finally {
            if (null != idxWriter) {
                idxWriter.close();
            }
        }

    }

    protected abstract Document buildDocument(T bean);

    /**
     * ?
     * 
     * @param keyWords key words
     * @return List<Document>
     * @throws IOException
     */
    public final List<Document> query(String keyWords, QueryBuilder queryBuilder) throws IOException {
        IndexSearcher idxSearcher = getIndexSearcher();
        Query query = queryBuilder.buildQuery(keyWords);
        TopDocs topDocs = idxSearcher.search(query, MAX_RESULT);
        ScoreDoc[] sDocs = topDocs.scoreDocs;

        List<Document> resultDocs = new ArrayList<Document>();
        if (null == sDocs || sDocs.length == 0) {
            return resultDocs;
        }

        for (int i = 0; i < sDocs.length; i++) {
            resultDocs.add(idxSearcher.doc(sDocs[i].doc));
        }

        return resultDocs;
    }

    /**
     * ?
     * 
     * @param keyWords key words
     * @param page ?
     * @param pageSize ?
     * @return {@link PaginationResult} 
     * @see PaginationResult
     * @throws IOException
     */
    public final PaginationResult doPaginationQuery(String keyWords, int page, int pageSize,
            QueryBuilder queryBuilder) throws IOException {
        PaginationResult result = new PaginationResult();
        result.setCurrentPage(page);
        result.setPageSize(pageSize);

        IndexSearcher idxSearch = getIndexSearcher();
        Query query = queryBuilder.buildQuery(keyWords);
        TopDocs topDocs = idxSearch.search(query, MAX_RESULT);

        int totalHits = topDocs.totalHits;
        result.setTotalHits(totalHits);

        int start = page * pageSize;
        int end = start + pageSize;

        List<Document> docs = new ArrayList<Document>();
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        result.setDocs(docs);
        if (null == scoreDocs || scoreDocs.length == 0) {
            return result;
        }

        for (int i = start; i < totalHits; i++) {
            if (i > (end - 1)) {
                break;
            }
            docs.add(idxSearch.doc(scoreDocs[i].doc));
        }

        if (logger.isDebugEnabled()) {
            logger.debug(String.format("search result: totalHits-%d curPage-%d pageSize-%d", result.getTotalHits(),
                    result.getCurrentPage(), result.getPageSize()));
        }

        return result;
    }

    protected abstract IndexSearcher getIndexSearcher();

    /**
     * ?????
     * 
     * @author miracle.ivanlee@gmail.com since Aug 16, 2012
     * 
     */
    public static interface QueryBuilder {

        public Query buildQuery(String keyWords);
    }

}