Zend_Search_Lucene-Advanced.xml 6.6 KB

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