Lucene创建Document代码部分-循序渐进学Lucene

作者 : admin 于 2008年10月08日, 18:47:49
2008
10-8
  1. import java.io.*;
  2. import org.apache.lucene.document.Document;
  3. import org.apache.lucene.document.Field;
  4. import org.apache.lucene.index.IndexWriter;
  5. import org.apache.lucene.analysis.SimpleAnalyzer;
  6. public class Index
  7. {
  8. public static void main( String args[] )
  9. {
  10. Document doc = new Document();
  11. //注释1
  12. Field f1 = new Field("name1","value1",Field.Store.YES,Field.Index.TOKENIZED);
  13. Field f2 = new Field("name2","value2",Field.Store.YES,Field.Index.TOKENIZED);
  14. doc.add( f1 );
  15. doc.add( f2 );
  16. try
  17. {
  18. IndexWriter writer = new IndexWriter( "./" , new SimpleAnalyzer() , true );
  19. writer.addDocument( doc );
  20. writer.close();
  21. }
  22. catch (Exception e)
  23. {
  24.                        e.printStackTrace();
  25. }
  26. }
  27. }

注释1:Field方法的在2.0.0版本中有5种方法,在2.3.2中增加到了7种,详细用法请参阅官方文档
http://lucene.apache.org/java/2_0_0/api/org/apache/lucene/document/Field.html
http://lucene.apache.org/java/2_3_2/api/org/apache/lucene/document/Field.html

Lucene文档模型(Document)简解

作者 : admin 于 2008年10月04日, 11:39:51
2008
10-4

最近仔细研究了Lucene的文档模式,现用通俗语言来解释。

Lucene的索引,基本结构为 Index->Document->Field 这样可以构建一个非关系型二维结构,由不同field构成的一个Document,由不同的Document构成的Index。

Lucene的Field方法很丰富,在2.0版本以后,由三种Field类型增加到了5种,支持三种数据流的传入方式(字符串,读取,二进制传入),支持数据的存储,索引,分词。

而我们使用Lucene的主要功能在于分词和生成索引。具体应用待详细使用后介绍。

我的一个Java程序

作者 : admin 于 2008年10月03日, 17:42:56
2008
10-3

PHP总有其局限性,比如解释型语言在速度上的弱点,制约了它在效率方面的发挥。facebook开放了源代码,其底层大都是c来编写,而我现在计划用Java为一些服务写后台,这是我的第一个Java程序,希望大家多批评,虽然上边依然有太多php的影子。

  1. import java.sql.*;
  2. /*
  3.  *  Java Mysql数据库连接类
  4.  *  我的第一个java程序
  5.  */
  6. public class MysqlConn
  7. {
  8. private String dsn = "jdbc:mysql://localhost:3306/test";
  9. private String username = "root";
  10. private String password = "123456";
  11.  
  12. private Connection conn = null;
  13. private Statement stmt = null;
  14. private ResultSet rs = null;
  15.  
  16.  
  17.  
  18.  
  19.     public void MysqlConn()
  20. {
  21. }
  22.     //初始化连接参数
  23. public void SetDsn( String Dsn )
  24. {
  25. this.dsn = Dsn;
  26. }
  27. //初始化用户名密码
  28. public void SetUserPass( String Username , String Password )
  29. {
  30. this.username = Username;
  31. this.password = Password;
  32. }
  33. //连接函数
  34. public void Conn()
  35. {
  36. try
  37. {
  38. //加载Connetc/J驱动
  39. //Class.forName("com.mysql.jdbc.Driver");
  40. Class.forName("org.gjt.mm.mysql.Driver");
  41. //开始连接
  42. this.conn = DriverManager.getConnection( this.dsn , this.username , this.password );
  43. this.stmt = this.conn.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE , ResultSet.CONCUR_UPDATABLE );
  44. }
  45. catch(SQLException ex)
  46. {
  47. System.out.println("Error : " + ex.toString());
  48. System.out.println("没有找到JDBC/ODBC驱动程序!");
  49. System.exit(0);
  50. }
  51. catch(Exception e)
  52. {
  53. System.out.println("Errors: " + e.toString());
  54. System.exit(0);
  55. }
  56. }
  57.     //返回结果集
  58. public ResultSet sqlQuery( String sql )
  59. {
  60. try
  61. {
  62. this.rs = this.stmt.executeQuery( sql );
  63. }
  64. catch (SQLException ex)
  65. {
  66. System.out.println("Error : " + ex.toString());
  67. System.exit(0);
  68. }
  69. catch (Exception ex)
  70. {
  71. System.out.println("Error : " + ex.toString());
  72. System.exit(0);
  73. }
  74. return rs;
  75. }
  76. //执行 update,insert之类
  77. public void Exec( String sql )
  78. {
  79. try
  80. {
  81. this.stmt.executeUpdate( sql )
  82. }
  83. catch (SQLException ex)
  84. {
  85. System.out.println("Error : " + ex.toString());
  86. System.exit(0);
  87. }
  88. catch (Exception ex)
  89. {
  90. System.out.println("Error : " + ex.toString());
  91. System.exit(0);
  92. }
  93. }
  94.  
  95. }

 Page 2 of 2 « 1  2