Zend_Search_Lucene-IndexCreation.xml 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <sect1 id="zend.search.lucene.index-creation">
  2. <title>建立索引</title>
  3. <sect2 id="zend.search.lucene.index-creation.creating">
  4. <title>创建新索引</title>
  5. <para>
  6. 在 Zend_Search_Lucene 模块和 Java Lucene 中实现了索引的创建和更新机制。你都可以使用。
  7. </para>
  8. <para>
  9. 下面的 PHP 代码提供了一个如何使用 Zend_Search_Lucene 索引 API 的例子:
  10. </para>
  11. <programlisting role="php"><![CDATA[<?php
  12. // Setting the second argument to TRUE creates a new index
  13. $index = new Zend_Search_Lucene('/data/my-index', true);
  14. $doc = new Zend_Search_Lucene_Document();
  15. // Store document URL to identify it in search result.
  16. $doc->addField(Zend_Search_Lucene_Field::Text('url', $docUrl));
  17. // Index document content
  18. $doc->addField(Zend_Search_Lucene_Field::UnStored('contents', $docContent));
  19. // Add document to the index.
  20. $index->addDocument($doc);
  21. // Write changes to the index.
  22. $index->commit();
  23. ?>]]></programlisting>
  24. <para>
  25. 在进行了 commit 提交操作之后,新添加的文档就可以被检索了。
  26. </para>
  27. <para>
  28. <code>Zend_Search_Lucene::commit()</code> 会在脚本执行结束前以及任意搜索请求开始之前被自动调用。
  29. </para>
  30. <para>
  31. 每一次 commit() 调用产生新的索引分段。因此尽可能少的请求它。当然另一方面,在一步中提交大量的文档需要更多的内存。
  32. </para>
  33. <para>
  34. 字段分段管理优化是 Zend_Search_Lucene 未来要增强的内容。
  35. </para>
  36. </sect2>
  37. <sect2 id="zend.search.lucene.index-creation.updating">
  38. <title>更新索引</title>
  39. <para>
  40. 同样的过程可以用于更新现存的索引。唯一的区别是打开相应的索引是不需要第二个参数:
  41. </para>
  42. <programlisting role="php"><![CDATA[<?php
  43. // Open existing index
  44. $index = new Zend_Search_Lucene('/data/my-index');
  45. $doc = new Zend_Search_Lucene_Document();
  46. // Store document URL to identify it in search result.
  47. $doc->addField(Zend_Search_Lucene_Field::Text('url', $docUrl));
  48. // Index document content
  49. $doc->addField(Zend_Search_Lucene_Field::UnStored('contents', $docContent));
  50. // Add document to the index.
  51. $index->addDocument($doc);
  52. // Write changes to the index.
  53. $index->commit();
  54. ?>]]></programlisting>
  55. <para>
  56. 每一次 commit() 调用(显式的或者隐式的)产生新的索引分段。
  57. </para>
  58. <para>
  59. Zend_Search_Lucene 不会自动管理分段。因此你应该关注分段的大小。一方面一个大的分段可能更加理想,另一方面在创建时需要更大的内存。
  60. </para>
  61. <para>
  62. Lucene Java 和 Luke (Lucene Index Toolbox - <ulink url="http://www.getopt.org/luke/">http://www.getopt.org/luke/</ulink>)可以用于优化这个版本的 Zend_Search_Lucene 产生的索引。
  63. </para>
  64. </sect2>
  65. </sect1>
  66. <!--
  67. vim:se ts=4 sw=4 et:
  68. -->