2
0

Zend_Search_Lucene-Advanced.xml 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.search.lucene.advanced">
  4. <title>Advanced</title>
  5. <sect2 id="zend.search.lucene.advanced.format_migration">
  6. <title>Starting from 1.6, handling index format transformations</title>
  7. <para>
  8. <classname>Zend_Search_Lucene</classname> component works with Java Lucene 1.4-1.9, 2.1 and 2.3 index formats.
  9. </para>
  10. <para>
  11. Current index format may be requested using <code>$index->getFormatVersion()</code> call.
  12. It returns one of the following values:
  13. <itemizedlist>
  14. <listitem>
  15. <para>
  16. <classname>Zend_Search_Lucene::FORMAT_PRE_2_1</classname> for Java Lucene 1.4-1.9 index format.
  17. </para>
  18. </listitem>
  19. <listitem>
  20. <para>
  21. <classname>Zend_Search_Lucene::FORMAT_2_1</classname> for Java Lucene 2.1 index format (also used for Lucene 2.2).
  22. </para>
  23. </listitem>
  24. <listitem>
  25. <para>
  26. <classname>Zend_Search_Lucene::FORMAT_2_3</classname> for Java Lucene 2.3 index format.
  27. </para>
  28. </listitem>
  29. </itemizedlist>
  30. </para>
  31. <para>
  32. Index modifications are performed <emphasis>only</emphasis> if any index update is done.
  33. That happens if a new document is added to an index or index optimization is started manually
  34. by <code>$index->optimize()</code> call.
  35. </para>
  36. <para>
  37. In a such case <classname>Zend_Search_Lucene</classname> may convert index to the higher format version.
  38. That <emphasis>always</emphasis> happens for the indices in <classname>Zend_Search_Lucene::FORMAT_PRE_2_1</classname> format,
  39. which are automatically converted to 2.1 format.
  40. </para>
  41. <para>
  42. You may manage conversion process and assign target index format by <code>$index->setFormatVersion()</code> which takes
  43. <classname>Zend_Search_Lucene::FORMAT_2_1</classname> or <classname>Zend_Search_Lucene::FORMAT_2_3</classname> constant as a parameter:
  44. <itemizedlist>
  45. <listitem>
  46. <para>
  47. <classname>Zend_Search_Lucene::FORMAT_2_1</classname> actually does nothing since pre-2.1 indices are automatically converted
  48. to 2.1 format.
  49. </para>
  50. </listitem>
  51. <listitem>
  52. <para>
  53. <classname>Zend_Search_Lucene::FORMAT_2_3</classname> forces conversion to the 2.3 format.
  54. </para>
  55. </listitem>
  56. </itemizedlist>
  57. </para>
  58. <para>
  59. Backward conversions are not supported.
  60. </para>
  61. <note>
  62. <title>Important!</title>
  63. <para>
  64. Once index is converted to upper version it can't be converted back. So make a backup of your index when you plan
  65. migration to upper version, but want to have possibility to go back.
  66. </para>
  67. </note>
  68. </sect2>
  69. <sect2 id="zend.search.lucene.advanced.static">
  70. <title>Using the index as static property</title>
  71. <para>
  72. The <classname>Zend_Search_Lucene</classname> object uses the destructor method to commit changes and clean up resources.
  73. </para>
  74. <para>
  75. It stores added documents in memory and dumps new index segment to disk depending on <code>MaxBufferedDocs</code> parameter.
  76. </para>
  77. <para>
  78. If <code>MaxBufferedDocs</code> limit is not reached then there are some "unsaved" documents which are saved as a new segment in the object's destructor method.
  79. The index auto-optimization procedure is invoked if necessary depending on the values of the <code>MaxBufferedDocs</code>, <code>MaxMergeDocs</code> and
  80. <code>MergeFactor</code> parameters.
  81. </para>
  82. <para>
  83. Static object properties (see below) are destroyed <emphasis>after</emphasis> the last line of the executed script.
  84. <programlisting language="php"><![CDATA[
  85. class Searcher {
  86. private static $_index;
  87. public static function initIndex() {
  88. self::$_index = Zend_Search_Lucene::open('path/to/index');
  89. }
  90. }
  91. Searcher::initIndex();
  92. ]]></programlisting>
  93. </para>
  94. <para>
  95. All the same, the destructor for static properties is correctly invoked at this point in the program's execution.
  96. </para>
  97. <para>
  98. One potential problem is exception handling. Exceptions thrown by destructors of static objects don't have context, because the destructor is executed after the script has already completed.
  99. </para>
  100. <para>
  101. You might see a "Fatal error: Exception thrown without a stack frame in Unknown on line 0" error message instead of exception description in such cases.
  102. </para>
  103. <para>
  104. <classname>Zend_Search_Lucene</classname> provides a workaround to this problem with the <code>commit()</code> method. It saves all unsaved changes and frees memory used for storing new segments.
  105. You are free to use the commit operation any time- or even several times- during script execution. You can still use the <classname>Zend_Search_Lucene</classname> object for searching,
  106. adding or deleting document after the commit operation. But the <code>commit()</code> call guarantees that if there are no document added or deleted after the call to <code>commit()</code>, then
  107. the <classname>Zend_Search_Lucene</classname> destructor has nothing to do and will not throw exception:
  108. <programlisting language="php"><![CDATA[
  109. class Searcher {
  110. private static $_index;
  111. public static function initIndex() {
  112. self::$_index = Zend_Search_Lucene::open('path/to/index');
  113. }
  114. ...
  115. public static function commit() {
  116. self::$_index->commit();
  117. }
  118. }
  119. Searcher::initIndex();
  120. ...
  121. // Script shutdown routine
  122. ...
  123. Searcher::commit();
  124. ...
  125. ]]></programlisting>
  126. </para>
  127. </sect2>
  128. </sect1>