IndexerFile.java
上传用户:zhihansy
上传日期:2014-12-04
资源大小:7241k
文件大小:2k
源码类别:

搜索引擎

开发平台:

Java

  1. package com.lucene;
  2. import java.io.IOException;
  3. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  4. import org.apache.lucene.document.Document;
  5. import org.apache.lucene.index.IndexWriter;
  6. import org.apache.lucene.document.Field;
  7. import jeasy.analysis.MMAnalyzer;;
  8. /*
  9.  * @CopyRigth(R) 城市通
  10.  * @author 申华锋 E-mail:leonshine@qq.com
  11.  * @version 创建时间:Mar 9, 2008 12:12:45 AM
  12.  * @description lucene查询索引
  13.  */
  14. public class IndexerFile {
  15. /*
  16.  * 把数据库查处的Photo列表建立索引
  17.  */
  18. public static int indexFile(String indexDir, Product[] list)
  19. throws IOException {
  20. //IndexWriter writer = new IndexWriter(indexDir, new StandardAnalyzer(),true);
  21. IndexWriter writer = new IndexWriter(indexDir, new MMAnalyzer(),true);
  22. writer.setUseCompoundFile(false);
  23. for (int i = 0; i < list.length; i++) {
  24. Document doc = new Document();
  25. doc.add(new Field("productId", String.valueOf(list[i].getProductId()),
  26. Field.Store.YES, Field.Index.NO));
  27. if (list[i].getTitle() != null)
  28. doc.add(new Field("title", list[i].getTitle(), Field.Store.YES,
  29. Field.Index.TOKENIZED));
  30. /*if (list[i].getDescription() != null)
  31. doc.add(new Field("description", list[i].getDescription(),
  32. Field.Store.YES, Field.Index.TOKENIZED));
  33. doc.add(new Field("address", list[i].getAddress(), Field.Store.YES,
  34. Field.Index.NO));
  35. doc.add(new Field("userName", list[i].getUserName(),
  36. Field.Store.YES, Field.Index.TOKENIZED));
  37. doc.add(new Field("userId", String.valueOf(list[i].getUserId()),
  38. Field.Store.YES, Field.Index.NO));
  39. if (list[i].getTag().length() > 0)
  40. doc.add(new Field("tag", list[i].getTag(), Field.Store.YES,
  41. Field.Index.TOKENIZED));
  42. doc.add(new Field("uploadTime", "2006-10-26", Field.Store.YES,
  43. Field.Index.TOKENIZED));*/
  44. writer.addDocument(doc);
  45. }
  46. int numIndexed = writer.docCount();
  47. writer.optimize();
  48. writer.close();
  49. return numIndexed;
  50. }
  51. }