lucene3.0实例下载

简介:可以试试本站的lucene全文索引功能 http://ask.itruanjian.com/search/

项目里面有个lucene.sql文件,下面有源码下载地址

部分代码 LuceneDao.java

package lucene;
 
import java.io.File;
import java.util.ArrayList;
import java.util.List;
 
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.wltea.analyzer.lucene.IKAnalyzer;
 
import service.IArticleService;
import service.imp.ArticleService;
import util.FileUtil;
import util.PaginationSupport;
import bean.Article;
@Aspect
public class LuceneDao implements ILuceneDao{
    private IArticleService articleService;
    /**
     * 当新增文章时同时更新索引
     */
    @AfterReturning("execution(* service.imp.ArticleService.save(..))&&args(article)")
    public void createIndex(Article article) throws Exception{
        String path=FileUtil.getLocalPath(Lucene.lucenePath);
        Directory dir = new SimpleFSDirectory(new File(path));
        boolean exist = IndexReader.indexExists(dir);
//      IndexWriter.isLocked(dir);
        IndexWriter writer = new IndexWriter(dir, new IKAnalyzer(), !exist, IndexWriter.MaxFieldLength.LIMITED);
        try {
            writer.addDocument(Lucene.createDocument(article));
        } finally {
            writer.close();
        }
    }
    /**
     * 对list集合的文章批量建了索引
     */
    public void createIndex(List<Article> articles) throws Exception{
        String path=FileUtil.getLocalPath(Lucene.lucenePath);
        Directory dir = new SimpleFSDirectory(new File(path));
        boolean exist = IndexReader.indexExists(dir);
        IndexWriter writer = new IndexWriter(dir, new IKAnalyzer(), !exist, IndexWriter.MaxFieldLength.LIMITED);
        try {
            for(Article article:articles){
                writer.addDocument(Lucene.createDocument(article));
            }
            writer.optimize();
        } finally {
            writer.close();
        }
    }
    /**
     * 
     */
    @AfterReturning("execution(* service.imp.ArticleService.del(..))&&args(article)")
    public void deleteIndex(Article article) throws Exception{
        String path=FileUtil.getLocalPath(Lucene.lucenePath);
        Directory dir = new SimpleFSDirectory(new File(path));
        boolean exist = IndexReader.indexExists(dir);
        if (exist) {
            IndexWriter writer = new IndexWriter(dir, new IKAnalyzer(), false,
                    IndexWriter.MaxFieldLength.LIMITED);
            try {
                if(article.getId()!=null&&!"".equals(article.getId())){
                    writer.deleteDocuments(new Term("id", article.getId().toString()));
                }else if(article.getSortid()!=null&&!"".equals(article.getSortid())){
                    writer.deleteDocuments(new Term("sortid", article.getSortid().toString()));
                }else{
                    writer.deleteAll();
                }
            } finally {
                writer.close();
            }
        }
    }
    /**
     * 
     */
    @AfterReturning("execution(* service.imp.ArticleService.update(..))&&args(article)")
    public void updateIndex(Article article) throws Exception{
        String path=FileUtil.getLocalPath(Lucene.lucenePath);
        Directory dir = new SimpleFSDirectory(new File(path));
        boolean exist = IndexReader.indexExists(dir);
        IndexWriter writer = new IndexWriter(dir, new IKAnalyzer(), !exist, IndexWriter.MaxFieldLength.LIMITED);
        try {
            if (exist) {
                writer.deleteDocuments(new Term("id", article.getId().toString()));
            }
            writer.addDocument(Lucene.createDocument(article));
        } finally {
            writer.close();
        }
    }
     
    public List<Article> searchList() throws Exception{
        String path=FileUtil.getLocalPath(Lucene.lucenePath);
        Directory dir = new SimpleFSDirectory(new File(path));
        return null;
    }
    /**
     * 
     */
    public PaginationSupport<Article> searchPage(Article article,int startIndex,int pageSize) throws Exception{
        String path=FileUtil.getLocalPath(Lucene.lucenePath);
        Directory dir = new SimpleFSDirectory(new File(path));
        Searcher searcher = new IndexSearcher(dir);
         
        Analyzer analyzer = new IKAnalyzer();
        Query query = Lucene.createQuery(article, analyzer);
        Sort sort = new Sort(new SortField[]{SortField.FIELD_SCORE,new SortField("createtime", SortField.INT, true)});
        TopDocs docs = searcher.search(query,null,startIndex+pageSize,sort);
//      TopDocs docs = searcher.search(query,startIndex+pageSize);
        PaginationSupport<Article> p = Lucene.getResultPage(searcher, docs, startIndex,
                pageSize);
         
        List<Article> ids = p.getItems();
        List<Article> articles = new ArrayList<Article>(ids.size());
        for(Article article2:ids){
            Article article3=articleService.getById(article2.getId());
            String title=Lucene.hight(query, analyzer, article3.getTitle(), 0);
            String content=Lucene.hight(query, analyzer, article3.getContent(), 150);
            if(title!=null){
                article3.setTitle(title);
            }
            if(content!=null){
                article3.setContent(content);
            }else{
                article3.setContent(Lucene.getNoHtml(article3.getContent(), 150));
            }
            articles.add(article3);
        }
        p.setItems(articles);
        return p;
    }
    public void setArticleService(IArticleService articleService) {
        this.articleService = articleService;
    }
     
}

下载地址:http://ask.itruanjian.com/?note_down/view/111.html

相关推荐