1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="zend.search.lucene.advanced">
4 <title>Advanced</title>
6 <sect2 id="zend.search.lucene.advanced.format_migration">
7 <title>Starting from 1.6, handling index format transformations</title>
10 <classname>Zend_Search_Lucene</classname> component works with Java Lucene 1.4-1.9, 2.1
11 and 2.3 index formats.
15 Current index format may be requested using <code>$index->getFormatVersion()</code>
16 call. It returns one of the following values:
21 <constant>Zend_Search_Lucene::FORMAT_PRE_2_1</constant> for Java Lucene
28 <constant>Zend_Search_Lucene::FORMAT_2_1</constant> for Java Lucene 2.1
29 index format (also used for Lucene 2.2).
35 <constant>Zend_Search_Lucene::FORMAT_2_3</constant> for Java Lucene 2.3
43 Index modifications are performed <emphasis>only</emphasis> if any index update is done.
44 That happens if a new document is added to an index or index optimization is started
45 manually by <code>$index->optimize()</code> call.
49 In a such case <classname>Zend_Search_Lucene</classname> may convert index to the higher
50 format version. That <emphasis>always</emphasis> happens for the indices in
51 <constant>Zend_Search_Lucene::FORMAT_PRE_2_1</constant> format, which are automatically
52 converted to 2.1 format.
56 You may manage conversion process and assign target index format by
57 <code>$index->setFormatVersion()</code> which takes
58 <constant>Zend_Search_Lucene::FORMAT_2_1</constant> or
59 <constant>Zend_Search_Lucene::FORMAT_2_3</constant> constant as a parameter:
64 <constant>Zend_Search_Lucene::FORMAT_2_1</constant> actually does nothing
65 since pre-2.1 indices are automatically converted to 2.1 format.
70 <constant>Zend_Search_Lucene::FORMAT_2_3</constant> forces conversion to the
78 Backward conversions are not supported.
82 <title>Important!</title>
85 Once index is converted to upper version it can't be converted back. So make a
86 backup of your index when you plan migration to upper version, but want to have
87 possibility to go back.
92 <sect2 id="zend.search.lucene.advanced.static">
93 <title>Using the index as static property</title>
96 The <classname>Zend_Search_Lucene</classname> object uses the destructor method to
97 commit changes and clean up resources.
101 It stores added documents in memory and dumps new index segment to disk depending on
102 <code>MaxBufferedDocs</code> parameter.
106 If <code>MaxBufferedDocs</code> limit is not reached then there are some "unsaved"
107 documents which are saved as a new segment in the object's destructor method. The index
108 auto-optimization procedure is invoked if necessary depending on the values of the
109 <code>MaxBufferedDocs</code>, <code>MaxMergeDocs</code> and <code>MergeFactor</code>
114 Static object properties (see below) are destroyed <emphasis>after</emphasis> the last
115 line of the executed script.
117 <programlisting language="php"><![CDATA[
119 private static $_index;
121 public static function initIndex() {
122 self::$_index = Zend_Search_Lucene::open('path/to/index');
126 Searcher::initIndex();
131 All the same, the destructor for static properties is correctly invoked at this point in
132 the program's execution.
136 One potential problem is exception handling. Exceptions thrown by destructors of static
137 objects don't have context, because the destructor is executed after the script has
142 You might see a "Fatal error: Exception thrown without a stack frame in Unknown on line
143 0" error message instead of exception description in such cases.
147 <classname>Zend_Search_Lucene</classname> provides a workaround to this problem with the
148 <methodname>commit()</methodname> method. It saves all unsaved changes and frees memory
149 used for storing new segments. You are free to use the commit operation any time- or
150 even several times- during script execution. You can still use the
151 <classname>Zend_Search_Lucene</classname> object for searching, adding or deleting
152 document after the commit operation. But the <methodname>commit()</methodname> call
153 guarantees that if there are no document added or deleted after the call to
154 <methodname>commit()</methodname>, then the <classname>Zend_Search_Lucene</classname>
155 destructor has nothing to do and will not throw exception:
157 <programlisting language="php"><![CDATA[
159 private static $_index;
161 public static function initIndex() {
162 self::$_index = Zend_Search_Lucene::open('path/to/index');
167 public static function commit() {
168 self::$_index->commit();
172 Searcher::initIndex();
176 // Script shutdown routine