日期:2014-05-16 浏览次数:20841 次
package com.dxcollector.entity; public class DxNovel implements java.io.Serializable { private Long id; private String pid; private String NTitle; private String NAuthor; private String NDesc; private String NType; private String NTotalWords; private String NImgUrl; private String NSourceUrl; //省略getter and setter }
package com.dxcollector.search; import java.io.File; import java.io.IOException; import java.util.List; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.LockObtainFailedException; import org.apache.lucene.util.Version; import com.dxcollector.entity.DxNovel; import com.dxcollector.novels.readnovel.NovelMgr; /** * 索引管理 * * @author 忧里修斯 */ public class IndexMgr { NovelMgr novelMgr = new NovelMgr(); /** * 创建新的索引 * * @param index_store_path 索引存放路径 */ public void createIndex(String index_store_path){ IndexWriter indexWriter = null; try { Directory directory = FSDirectory.open(new File(index_store_path)); indexWriter = new IndexWriter(directory,new StandardAnalyzer(Version.LUCENE_29),IndexWriter.MaxFieldLength.UNLIMITED); List<DxNovel> novelList = novelMgr.getAllNovel(); System.out.println("大小:"+novelList.size()); Document doc = null; for (DxNovel dxNovel : novelList) { doc = new Document(); Field titleField = new Field("NTitle",dxNovel.getNTitle(),Field.Store.YES,Field.Index.ANALYZED); Field authorField = new Field("NAuthor",dxNovel.getNAuthor(),Field.Store.YES,Field.Index.NOT_ANALYZED); Field descField = new Field("NDesc",dxNovel.getNDesc(),Field.Store.YES,Field.Index.ANALYZED); Field typeField = new Field("NType",dxNovel.getNType(),Field.Store.YES,Field.Index.ANALYZED); doc.add(titleField); doc.add(authorField); doc.add(descField); doc.add(typeField); indexWriter.addDocument(doc); } indexWriter.optimize(); indexWriter.close(); } catch (CorruptIndexException e) { e.printStackTrace(); } catch (LockObtainFailedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("索引建立完成"); } public static void main(String[] args) { IndexMgr im = new IndexMgr(); im.createIndex("C:/index/"); } }
package com.dxcollector.search; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.queryParser.MultiFieldQueryParser; import org.apache.lucene.queryParser.ParseException; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; import com.dxcollector.entity.DxNovel; /** * 搜索管理器 * * @author 忧里修斯 * */ public class SearchMgr { //索引文件存放的位置 private String index_store_path = "C:/index/"; public SearchMgr(String index_store_path){ this.index_store_path = index_store_path; } /** * 搜索小说 * * @param searchType 搜索类型,对应小说标题NTitle等 * @param keyword 关键字 * @return List<DxNovel> */ public List<DxNovel> search(String searc