Lucene 心得

public void createIndex1(Connection conn, AttachInfoDTO1 main) throws SQLException {
  //定義
  IndexWriter writer = null;
  //文件路徑
  Directory directory = null;
  // 索引文件夾
  File dir = new File("PICCAssets/index");
  String path = dir.getAbsolutePath();
  String path1 = path.replace("bin", "webapps");
  try {
   directory = FSDirectory.open(new File(path1));
   writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new IKAnalyzer()));
   Document doc = null;
   File f = null;
   InputStream br = null;
   if (main != null) {
    int fileid = main.getId();
    String dname = main.getFileUrl();
    String rname = "http://192.168.1.10:8080/PICCAssets/" + dname;
    FtpTask ftp = new FtpTask();
    String path11 = dname.substring(0, dname.lastIndexOf("/") + 1);
    String file = dname.substring(dname.lastIndexOf("/") + 1);
    ftp.connectServer(path11);
    br = );
      SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    String author = main.getStaffName();
    if ((author == null) || (("").equals(author))) {
     author = "系統管理員";
    }
    doc = new Document();
    Metadata metadata = new Metadata();
    doc.add(new Field("content", new Tika().parseToString(br), Field.Store.YES, Field.Index.ANALYZED, TermVector.WITH_POSITIONS_OFFSETS));
    doc.add(new Field("title", main.getAttachName(), Field.Store.YES, Field.Index.ANALYZED, TermVector.WITH_POSITIONS_OFFSETS));
    doc.add(new Field("author", author, Field.Store.YES, Field.Index.ANALYZED, TermVector.WITH_POSITIONS_OFFSETS));
    doc.add(new Field("rid", Integer.toString(main.getId()), Field.Store.YES, Field.Index.ANALYZED, TermVector.WITH_POSITIONS_OFFSETS));
    doc.add(new Field("filename", main.getAttachName(), Field.Store.YES, Field.Index.NOT_ANALYZED));
    doc.add(new Field("path", dname, Field.Store.YES, Field.Index.NOT_ANALYZED));
    doc.add(new Field("dpath", rname, Field.Store.YES, Field.Index.NOT_ANALYZED));
    doc.add(new Field("dtype", main.getFileTypeLever1(), Field.Store.YES, Field.Index.NOT_ANALYZED));
    doc.add(new Field("date",  main.getUploadDate(), Field.Store.YES, Field.Index.NOT_ANALYZED));
    int page = 0;
    try {
     page = Integer.parseInt(metadata.get("xmpTPg:NPages"));
    } catch (NumberFormatException e) {
    }
    if (doc != null) {
     try {
      writer.addDocument(doc);
     } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
     }
    }
   }
  } catch (IOException e) {
   e.printStackTrace();
  } catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
  } finally {
   if (writer != null) {
    try {
     writer.close();
    } catch (CorruptIndexException e) {
     e.printStackTrace();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
 }

刪除所有索引java

 /**
  * 刪除所有索引
  * 
  * @throws Exception
  */
 public void deleteAllIndex() throws Exception {
  IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35));
  IndexWriter indexWriter = new IndexWriter(FileIndexUtils.getDirectory(), config);
  indexWriter.deleteAll();
  indexWriter.close();
 }

刪除單個索引web

/**
  * 刪除單個索引
  * 
  * @throws Exception
  */
 public void deleteOneIndex(String uid) throws Exception {
  IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35));
  IndexWriter indexWriter = new IndexWriter(FileIndexUtils.getDirectory(), config);
  indexWriter.deleteDocuments(new Term("rid", uid));
  indexWriter.close();
 }

//高亮查詢索引對應的文檔 以及組合查詢app

@SuppressWarnings({ "rawtypes", "unused" })
 public PagerDTO<LuceneDTO> searchPage(String key, int starttime, int endtime, String dtype, int pageIndex, int pageSize) { // String
  Analyzer luceneAnalyzer = new IKAnalyzer();
  PagerDTO<LuceneDTO> pages = new PagerDTO<LuceneDTO>();
  List<LuceneDTO> l = new ArrayList<LuceneDTO>();
  LuceneDTO bean = null;
  try {
   // 索引文件夾
   IndexSearcher searcher = new IndexSearcher(IndexReader.open(FileIndexUtils.getDirectory()));
   BooleanQuery query0 = new BooleanQuery();
   // 日期區間
   NumericRangeQuery q2 = NumericRangeQuery.newIntRange("date", starttime, endtime, true, true);
   // 關鍵字查詢
   MultiFieldQueryParser parser = new MultiFieldQueryParser(Version.LUCENE_35, new String[] { "title", "content" }, luceneAnalyzer);
   if (key == null || "".equals(key)) {
    query0.add(new TermQuery(new Term("dtype", dtype)), Occur.SHOULD);
   } else {
    Query queryKey = parser.parse(key);
    query0.add(new TermQuery(new Term("dtype", dtype)), Occur.MUST);
    query0.add(queryKey,  Occur.MUST);
   }
   // 先獲取上一頁的最後一個元素
   ScoreDoc lastSd = getLastScoreDoc(pageIndex, pageSize, query0, searcher);
   // 經過最後一個元素搜索下頁的pageSize個元素
   TopDocs tds = searcher.searchAfter(lastSd, query0, pageSize);
   int totalRecord = tds.totalHits;
   // 搜素開始時間
   Date beginTime = new Date();
   for (ScoreDoc sd : tds.scoreDocs) {
    Document doc = searcher.doc(sd.doc);
    String path = doc.get("path");
    String filen = path.split("/")[2];
    String filename = filen.substring(0, filen.lastIndexOf(".")) + ".swf";
    /**
     * 按照http遠程讀取文件方式,沒有儲存在索引上
     */
    String content = doc.get("content");
    String htitle = doc.get("title");
    QueryScorer scorer = new QueryScorer(query0);
    Fragmenter fragmenter = new SimpleSpanFragmenter(scorer);
    Formatter formatter = new SimpleHTMLFormatter("<span style='color:red'>", "</span>");
    Highlighter lighter = new Highlighter(formatter, scorer);
    lighter.setTextFragmenter(fragmenter);
    // 設置高亮字數多少
    lighter.setTextFragmenter(new SimpleFragmenter(200));
    /*
     * 設置高亮,若是有關鍵字就加亮,沒有就返回空,因此要加判斷
     */
    String str = lighter.getBestFragment(luceneAnalyzer, "f", content);
    String titlel = lighter.getBestFragment(luceneAnalyzer, "s", htitle);
    bean = new LuceneDTO();
    if (titlel != null) {
     bean.setTitle(titlel);
    } else {
     bean.setTitle(htitle);
    }
    if (str != null) {
     bean.setStr(str);
    } else {
     if (content.length() != 0 && content.length() > 250) {
      String content1 = content.substring(0, 250);
      bean.setStr(content1);
     } else {
      bean.setStr(content);
     }
    }
    bean.setDate(doc.get("date"));
    bean.setType(doc.get("title").substring(doc.get("title").lastIndexOf(".") + 1));
    bean.setPath(doc.get("author"));
    bean.setFilename(filename);
    bean.setDpath(doc.get("dpath"));
    bean.setRid(doc.get("rid"));
    l.add(bean);
   }
   // 搜索完成時間
   Date endTime = new Date();
   DecimalFormat format = new DecimalFormat("0.000");
   double timeOfSearch = (endTime.getTime() - beginTime.getTime()) * 0.001;
   int totalPage = (totalRecord - 1) / pageSize + 1;
   pages.setTimeOfSearch(format.format(timeOfSearch));
   pages.setTotalPage(totalPage);
   pages.setTotalRecord(totalRecord);
   pages.setPageIndex(pageIndex);
   pages.setPageSize(pageSize);
   pages.setDatas(l);
   searcher.close();
  } catch (IOException e) {
   e.printStackTrace();
  } catch (InvalidTokenOffsetsException e) {
   e.printStackTrace();
  } catch (ParseException e) {
   e.printStackTrace();
  }
  return pages;
 }
/**
  * 根據頁碼和分頁大小獲取上一次的最後一個ScoreDoc
  */
 private ScoreDoc getLastScoreDoc(int pageIndex, int pageSize, Query query, IndexSearcher searcher) throws IOException {
  if (pageIndex == 1)
   return null;// 若是是第一頁就返回空
  int num = pageSize * (pageIndex - 1);// 獲取上一頁的數量
  TopDocs tds = searcher.search(query, num);
  return tds.scoreDocs[num - 1];
 }
相關文章
相關標籤/搜索