Zend_Search_Lucene-Overview.xml 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.search.lucene.overview">
  4. <title>Overview</title>
  5. <sect2 id="zend.search.lucene.introduction">
  6. <title>Introduction</title>
  7. <para><classname>Zend_Search_Lucene</classname> is a general purpose text search engine written entirely in <acronym>PHP</acronym> 5.
  8. Since it stores its index on the filesystem and does not require a database
  9. server, it can add search capabilities to almost any <acronym>PHP</acronym>-driven website.
  10. <classname>Zend_Search_Lucene</classname> supports the following features:
  11. <itemizedlist>
  12. <listitem>
  13. <para>Ranked searching - best results returned first</para>
  14. </listitem>
  15. <listitem>
  16. <para>
  17. Many powerful query types: phrase queries, boolean queries, wildcard queries,
  18. proximity queries, range queries and many others.
  19. </para>
  20. </listitem>
  21. <listitem>
  22. <para>Search by specific field (e.g., title, author, contents)</para>
  23. </listitem>
  24. </itemizedlist>
  25. <classname>Zend_Search_Lucene</classname> was derived from the Apache Lucene project. The currently (starting from ZF 1.6) supported Lucene index format
  26. versions are 1.4 - 2.3. For more information on Lucene, visit <ulink url="http://lucene.apache.org/java/docs/"/>.
  27. </para>
  28. <note>
  29. <title/>
  30. <para>
  31. Previous <classname>Zend_Search_Lucene</classname> implementations support the Lucene 1.4 (1.9) - 2.1 index formats.
  32. </para>
  33. <para>
  34. Starting from Zend Framework 1.5 any index created using pre-2.1 index format is automatically upgraded to Lucene 2.1 format
  35. after the <classname>Zend_Search_Lucene</classname> update and will not be compatible with <classname>Zend_Search_Lucene</classname> implementations included into Zend Framework 1.0.x.
  36. </para>
  37. </note>
  38. </sect2>
  39. <sect2 id="zend.search.lucene.index-creation.documents-and-fields">
  40. <title>Document and Field Objects</title>
  41. <para>
  42. <classname>Zend_Search_Lucene</classname> operates with documents as atomic objects for indexing. A document is
  43. divided into named fields, and fields have content that can be searched.
  44. </para>
  45. <para>
  46. A document is represented by the <classname>Zend_Search_Lucene_Document</classname> class, and this objects of this class contain
  47. instances of <classname>Zend_Search_Lucene_Field</classname> that represent the fields on the document.
  48. </para>
  49. <para>
  50. It is important to note that any information can be added to the index.
  51. Application-specific information or metadata can be stored in the document
  52. fields, and later retrieved with the document during search.
  53. </para>
  54. <para>
  55. It is the responsibility of your application to control the indexer.
  56. This means that data can be indexed from any source
  57. that is accessible by your application. For example, this could be the
  58. filesystem, a database, an HTML form, etc.
  59. </para>
  60. <para>
  61. <classname>Zend_Search_Lucene_Field</classname> class provides several static methods to create fields with
  62. different characteristics:
  63. </para>
  64. <programlisting language="php"><![CDATA[
  65. $doc = new Zend_Search_Lucene_Document();
  66. // Field is not tokenized, but is indexed and stored within the index.
  67. // Stored fields can be retrived from the index.
  68. $doc->addField(Zend_Search_Lucene_Field::Keyword('doctype',
  69. 'autogenerated'));
  70. // Field is not tokenized nor indexed, but is stored in the index.
  71. $doc->addField(Zend_Search_Lucene_Field::UnIndexed('created',
  72. time()));
  73. // Binary String valued Field that is not tokenized nor indexed,
  74. // but is stored in the index.
  75. $doc->addField(Zend_Search_Lucene_Field::Binary('icon',
  76. $iconData));
  77. // Field is tokenized and indexed, and is stored in the index.
  78. $doc->addField(Zend_Search_Lucene_Field::Text('annotation',
  79. 'Document annotation text'));
  80. // Field is tokenized and indexed, but is not stored in the index.
  81. $doc->addField(Zend_Search_Lucene_Field::UnStored('contents',
  82. 'My document content'));
  83. ]]></programlisting>
  84. <para>
  85. Each of these methods (excluding the <methodname>Zend_Search_Lucene_Field::Binary()</methodname> method) has an optional
  86. <varname>$encoding</varname> parameter for specifying input data encoding.
  87. </para>
  88. <para>
  89. Encoding may differ for different documents as well as for different fields within one document:
  90. <programlisting language="php"><![CDATA[
  91. $doc = new Zend_Search_Lucene_Document();
  92. $doc->addField(Zend_Search_Lucene_Field::Text('title',
  93. $title,
  94. 'iso-8859-1'));
  95. $doc->addField(Zend_Search_Lucene_Field::UnStored('contents',
  96. $contents,
  97. 'utf-8'));
  98. ]]></programlisting>
  99. </para>
  100. <para>
  101. If encoding parameter is omitted, then the current locale is used at processing time. For example:
  102. <programlisting language="php"><![CDATA[
  103. setlocale(LC_ALL, 'de_DE.iso-8859-1');
  104. ...
  105. $doc->addField(Zend_Search_Lucene_Field::UnStored('contents', $contents));
  106. ]]></programlisting>
  107. </para>
  108. <para>
  109. Fields are always stored and returned from the index in UTF-8 encoding. Any required conversion to UTF-8 happens
  110. automatically.
  111. </para>
  112. <para>
  113. Text analyzers (<link linkend="zend.search.lucene.extending.analysis">see below</link>) may also convert text
  114. to some other encodings. Actually, the default analyzer converts text to 'ASCII//TRANSLIT' encoding.
  115. Be careful, however; this translation may depend on current locale.
  116. </para>
  117. <para>
  118. Fields' names are defined at your discretion in the <methodname>addField()</methodname> method.
  119. </para>
  120. <para>
  121. Java Lucene uses the 'contents' field as a default field to search.
  122. <classname>Zend_Search_Lucene</classname> searches through all fields by default, but the behavior is configurable.
  123. See the <link linkend="zend.search.lucene.query-language.fields">"Default search field"</link> chapter for details.
  124. </para>
  125. </sect2>
  126. <sect2 id="zend.search.lucene.index-creation.understanding-field-types">
  127. <title>Understanding Field Types</title>
  128. <itemizedlist>
  129. <listitem>
  130. <para>
  131. <code>Keyword</code> fields are stored and indexed, meaning that they can be searched as well
  132. as displayed in search results. They are not split up into separate words by tokenization.
  133. Enumerated database fields usually translate well to Keyword fields in <classname>Zend_Search_Lucene</classname>.
  134. </para>
  135. </listitem>
  136. <listitem>
  137. <para>
  138. <code>UnIndexed</code> fields are not searchable, but they are returned with search hits. Database
  139. timestamps, primary keys, file system paths, and other external identifiers are good
  140. candidates for UnIndexed fields.
  141. </para>
  142. </listitem>
  143. <listitem>
  144. <para>
  145. <code>Binary</code> fields are not tokenized or indexed, but are stored for retrieval with search hits.
  146. They can be used to store any data encoded as a binary string, such as an image icon.
  147. </para>
  148. </listitem>
  149. <listitem>
  150. <para>
  151. <code>Text</code> fields are stored, indexed, and tokenized. Text fields are appropriate for storing
  152. information like subjects and titles that need to be searchable as well as returned with
  153. search results.
  154. </para>
  155. </listitem>
  156. <listitem>
  157. <para>
  158. <code>UnStored</code> fields are tokenized and indexed, but not stored in the index. Large amounts of
  159. text are best indexed using this type of field. Storing data creates a larger index on
  160. disk, so if you need to search but not redisplay the data, use an UnStored field.
  161. UnStored fields are practical when using a <classname>Zend_Search_Lucene</classname> index in
  162. combination with a relational database. You can index large data fields with UnStored
  163. fields for searching, and retrieve them from your relational database by using a separate
  164. field as an identifier.
  165. </para>
  166. <table id="zend.search.lucene.index-creation.understanding-field-types.table">
  167. <title>Zend_Search_Lucene_Field Types</title>
  168. <tgroup cols="5">
  169. <thead>
  170. <row>
  171. <entry>Field Type</entry>
  172. <entry>Stored</entry>
  173. <entry>Indexed</entry>
  174. <entry>Tokenized</entry>
  175. <entry>Binary</entry>
  176. </row>
  177. </thead>
  178. <tbody>
  179. <row>
  180. <entry>Keyword</entry>
  181. <entry>Yes</entry>
  182. <entry>Yes</entry>
  183. <entry>No</entry>
  184. <entry>No</entry>
  185. </row>
  186. <row>
  187. <entry>UnIndexed</entry>
  188. <entry>Yes</entry>
  189. <entry>No</entry>
  190. <entry>No</entry>
  191. <entry>No</entry>
  192. </row>
  193. <row>
  194. <entry>Binary</entry>
  195. <entry>Yes</entry>
  196. <entry>No</entry>
  197. <entry>No</entry>
  198. <entry>Yes</entry>
  199. </row>
  200. <row>
  201. <entry>Text</entry>
  202. <entry>Yes</entry>
  203. <entry>Yes</entry>
  204. <entry>Yes</entry>
  205. <entry>No</entry>
  206. </row>
  207. <row>
  208. <entry>UnStored</entry>
  209. <entry>No</entry>
  210. <entry>Yes</entry>
  211. <entry>Yes</entry>
  212. <entry>No</entry>
  213. </row>
  214. </tbody>
  215. </tgroup>
  216. </table>
  217. </listitem>
  218. </itemizedlist>
  219. </sect2>
  220. <sect2 id="zend.search.lucene.index-creation.html-documents">
  221. <title>HTML documents</title>
  222. <para>
  223. <classname>Zend_Search_Lucene</classname> offers a HTML parsing feature. Documents can be created directly from a HTML file or string:
  224. <programlisting language="php"><![CDATA[
  225. $doc = Zend_Search_Lucene_Document_Html::loadHTMLFile($filename);
  226. $index->addDocument($doc);
  227. ...
  228. $doc = Zend_Search_Lucene_Document_Html::loadHTML($htmlString);
  229. $index->addDocument($doc);
  230. ]]></programlisting>
  231. </para>
  232. <para>
  233. <classname>Zend_Search_Lucene_Document_Html</classname> class uses the <methodname>DOMDocument::loadHTML()</methodname> and
  234. <methodname>DOMDocument::loadHTMLFile()</methodname> methods to parse the source HTML, so it doesn't need HTML to be well formed or
  235. to be <acronym>XHTML</acronym>. On the other hand, it's sensitive to the encoding specified by the "meta http-equiv" header tag.
  236. </para>
  237. <para>
  238. <classname>Zend_Search_Lucene_Document_Html</classname> class recognizes document title, body and document header meta tags.
  239. </para>
  240. <para>
  241. The 'title' field is actually the /html/head/title value. It's stored within the index, tokenized and available for search.
  242. </para>
  243. <para>
  244. The 'body' field is the actual body content of the HTML file or string. It doesn't include scripts, comments or attributes.
  245. </para>
  246. <para>
  247. The <methodname>loadHTML()</methodname> and <methodname>loadHTMLFile()</methodname>
  248. methods of <classname>Zend_Search_Lucene_Document_Html</classname> class also have
  249. second optional argument. If it's set to <constant>TRUE</constant>, then body content is
  250. also stored within index and can be retrieved from the index. By default, the body is
  251. tokenized and indexed, but not stored.
  252. </para>
  253. <para>
  254. The third parameter of <methodname>loadHTML()</methodname> and <methodname>loadHTMLFile()</methodname> methods optionally specifies source HTML
  255. document encoding. It's used if encoding is not specified using Content-type HTTP-EQUIV meta tag.
  256. </para>
  257. <para>
  258. Other document header meta tags produce additional document fields. The field 'name' is taken from 'name' attribute, and
  259. the 'content' attribute populates the field 'value'. Both are tokenized, indexed and stored, so documents may be searched by their meta tags
  260. (for example, by keywords).
  261. </para>
  262. <para>
  263. Parsed documents may be augmented by the programmer with any other field:
  264. <programlisting language="php"><![CDATA[
  265. $doc = Zend_Search_Lucene_Document_Html::loadHTML($htmlString);
  266. $doc->addField(Zend_Search_Lucene_Field::UnIndexed('created',
  267. time()));
  268. $doc->addField(Zend_Search_Lucene_Field::UnIndexed('updated',
  269. time()));
  270. $doc->addField(Zend_Search_Lucene_Field::Text('annotation',
  271. 'Document annotation text'));
  272. $index->addDocument($doc);
  273. ]]></programlisting>
  274. </para>
  275. <para>
  276. Document links are not included in the generated document, but may be retrieved with
  277. the <methodname>Zend_Search_Lucene_Document_Html::getLinks()</methodname> and <methodname>Zend_Search_Lucene_Document_Html::getHeaderLinks()</methodname>
  278. methods:
  279. <programlisting language="php"><![CDATA[
  280. $doc = Zend_Search_Lucene_Document_Html::loadHTML($htmlString);
  281. $linksArray = $doc->getLinks();
  282. $headerLinksArray = $doc->getHeaderLinks();
  283. ]]></programlisting>
  284. </para>
  285. <para>
  286. Starting from Zend Framework 1.6 it's also possible to exclude links with <code>rel</code> attribute set to <code>'nofollow'</code>.
  287. Use <methodname>Zend_Search_Lucene_Document_Html::setExcludeNoFollowLinks($true)</methodname> to turn on this option.
  288. </para>
  289. <para>
  290. <methodname>Zend_Search_Lucene_Document_Html::getExcludeNoFollowLinks()</methodname> method returns current state of
  291. "Exclude nofollow links" flag.
  292. </para>
  293. </sect2>
  294. <sect2 id="zend.search.lucene.index-creation.docx-documents">
  295. <title>Word 2007 documents</title>
  296. <para>
  297. <classname>Zend_Search_Lucene</classname> offers a Word 2007 parsing feature. Documents can be created directly from a Word 2007 file:
  298. <programlisting language="php"><![CDATA[
  299. $doc = Zend_Search_Lucene_Document_Docx::loadDocxFile($filename);
  300. $index->addDocument($doc);
  301. ]]></programlisting>
  302. </para>
  303. <para>
  304. <classname>Zend_Search_Lucene_Document_Docx</classname> class uses the <code>ZipArchive</code> class and
  305. <code>simplexml</code> methods to parse the source document. If the <code>ZipArchive</code> class (from module php_zip)
  306. is not available, the <classname>Zend_Search_Lucene_Document_Docx</classname> will also not be available for use with Zend Framework.
  307. </para>
  308. <para>
  309. <classname>Zend_Search_Lucene_Document_Docx</classname> class recognizes document meta data and document text. Meta data consists, depending on document contents, of filename, title, subject, creator, keywords, description, lastModifiedBy, revision, modified, created.
  310. </para>
  311. <para>
  312. The 'filename' field is the actual Word 2007 file name.
  313. </para>
  314. <para>
  315. The 'title' field is the actual document title.
  316. </para>
  317. <para>
  318. The 'subject' field is the actual document subject.
  319. </para>
  320. <para>
  321. The 'creator' field is the actual document creator.
  322. </para>
  323. <para>
  324. The 'keywords' field contains the actual document keywords.
  325. </para>
  326. <para>
  327. The 'description' field is the actual document description.
  328. </para>
  329. <para>
  330. The 'lastModifiedBy' field is the username who has last modified the actual document.
  331. </para>
  332. <para>
  333. The 'revision' field is the actual document revision number.
  334. </para>
  335. <para>
  336. The 'modified' field is the actual document last modified date / time.
  337. </para>
  338. <para>
  339. The 'created' field is the actual document creation date / time.
  340. </para>
  341. <para>
  342. The 'body' field is the actual body content of the Word 2007 document. It only includes normal text, comments and revisions are not included.
  343. </para>
  344. <para>
  345. The <methodname>loadDocxFile()</methodname> methods of
  346. <classname>Zend_Search_Lucene_Document_Docx</classname> class also have second optional
  347. argument. If it's set to <constant>TRUE</constant>, then body content is also stored
  348. within index and can be retrieved from the index. By default, the body is tokenized and
  349. indexed, but not stored.
  350. </para>
  351. <para>
  352. Parsed documents may be augmented by the programmer with any other field:
  353. <programlisting language="php"><![CDATA[
  354. $doc = Zend_Search_Lucene_Document_Docx::loadDocxFile($filename);
  355. $doc->addField(Zend_Search_Lucene_Field::UnIndexed(
  356. 'indexTime',
  357. time())
  358. );
  359. $doc->addField(Zend_Search_Lucene_Field::Text(
  360. 'annotation',
  361. 'Document annotation text')
  362. );
  363. $index->addDocument($doc);
  364. ]]></programlisting>
  365. </para>
  366. </sect2>
  367. <sect2 id="zend.search.lucene.index-creation.pptx-documents">
  368. <title>Powerpoint 2007 documents</title>
  369. <para>
  370. <classname>Zend_Search_Lucene</classname> offers a Powerpoint 2007 parsing feature. Documents can be created directly from a Powerpoint 2007 file:
  371. <programlisting language="php"><![CDATA[
  372. $doc = Zend_Search_Lucene_Document_Pptx::loadPptxFile($filename);
  373. $index->addDocument($doc);
  374. ]]></programlisting>
  375. </para>
  376. <para>
  377. <classname>Zend_Search_Lucene_Document_Pptx</classname> class uses the <code>ZipArchive</code> class and
  378. <code>simplexml</code> methods to parse the source document. If the <code>ZipArchive</code> class (from module php_zip)
  379. is not available, the <classname>Zend_Search_Lucene_Document_Pptx</classname> will also not be available for use with Zend Framework.
  380. </para>
  381. <para>
  382. <classname>Zend_Search_Lucene_Document_Pptx</classname> class recognizes document meta data and document text. Meta data consists, depending on document contents, of filename, title, subject, creator, keywords, description, lastModifiedBy, revision, modified, created.
  383. </para>
  384. <para>
  385. The 'filename' field is the actual Powerpoint 2007 file name.
  386. </para>
  387. <para>
  388. The 'title' field is the actual document title.
  389. </para>
  390. <para>
  391. The 'subject' field is the actual document subject.
  392. </para>
  393. <para>
  394. The 'creator' field is the actual document creator.
  395. </para>
  396. <para>
  397. The 'keywords' field contains the actual document keywords.
  398. </para>
  399. <para>
  400. The 'description' field is the actual document description.
  401. </para>
  402. <para>
  403. The 'lastModifiedBy' field is the username who has last modified the actual document.
  404. </para>
  405. <para>
  406. The 'revision' field is the actual document revision number.
  407. </para>
  408. <para>
  409. The 'modified' field is the actual document last modified date / time.
  410. </para>
  411. <para>
  412. The 'created' field is the actual document creation date / time.
  413. </para>
  414. <para>
  415. The 'body' field is the actual content of all slides and slide notes in the Powerpoint 2007 document.
  416. </para>
  417. <para>
  418. The <methodname>loadPptxFile()</methodname> methods of
  419. <classname>Zend_Search_Lucene_Document_Pptx</classname> class also have second optional
  420. argument. If it's set to <constant>TRUE</constant>, then body content is also stored
  421. within index and can be retrieved from the index. By default, the body is tokenized and
  422. indexed, but not stored.
  423. </para>
  424. <para>
  425. Parsed documents may be augmented by the programmer with any other field:
  426. <programlisting language="php"><![CDATA[
  427. $doc = Zend_Search_Lucene_Document_Pptx::loadPptxFile($filename);
  428. $doc->addField(Zend_Search_Lucene_Field::UnIndexed(
  429. 'indexTime',
  430. time()));
  431. $doc->addField(Zend_Search_Lucene_Field::Text(
  432. 'annotation',
  433. 'Document annotation text'));
  434. $index->addDocument($doc);
  435. ]]></programlisting>
  436. </para>
  437. </sect2>
  438. <sect2 id="zend.search.lucene.index-creation.xlsx-documents">
  439. <title>Excel 2007 documents</title>
  440. <para>
  441. <classname>Zend_Search_Lucene</classname> offers a Excel 2007 parsing feature. Documents can be created directly from a Excel 2007 file:
  442. <programlisting language="php"><![CDATA[
  443. $doc = Zend_Search_Lucene_Document_Xlsx::loadXlsxFile($filename);
  444. $index->addDocument($doc);
  445. ]]></programlisting>
  446. </para>
  447. <para>
  448. <classname>Zend_Search_Lucene_Document_Xlsx</classname> class uses the <code>ZipArchive</code> class and
  449. <code>simplexml</code> methods to parse the source document. If the <code>ZipArchive</code> class (from module php_zip)
  450. is not available, the <classname>Zend_Search_Lucene_Document_Xlsx</classname> will also not be available for use with Zend Framework.
  451. </para>
  452. <para>
  453. <classname>Zend_Search_Lucene_Document_Xlsx</classname> class recognizes document meta data and document text. Meta data consists, depending on document contents, of filename, title, subject, creator, keywords, description, lastModifiedBy, revision, modified, created.
  454. </para>
  455. <para>
  456. The 'filename' field is the actual Excel 2007 file name.
  457. </para>
  458. <para>
  459. The 'title' field is the actual document title.
  460. </para>
  461. <para>
  462. The 'subject' field is the actual document subject.
  463. </para>
  464. <para>
  465. The 'creator' field is the actual document creator.
  466. </para>
  467. <para>
  468. The 'keywords' field contains the actual document keywords.
  469. </para>
  470. <para>
  471. The 'description' field is the actual document description.
  472. </para>
  473. <para>
  474. The 'lastModifiedBy' field is the username who has last modified the actual document.
  475. </para>
  476. <para>
  477. The 'revision' field is the actual document revision number.
  478. </para>
  479. <para>
  480. The 'modified' field is the actual document last modified date / time.
  481. </para>
  482. <para>
  483. The 'created' field is the actual document creation date / time.
  484. </para>
  485. <para>
  486. The 'body' field is the actual content of all cells in all worksheets of the Excel 2007 document.
  487. </para>
  488. <para>
  489. The <methodname>loadXlsxFile()</methodname> methods of
  490. <classname>Zend_Search_Lucene_Document_Xlsx</classname> class also have second optional
  491. argument. If it's set to <constant>TRUE</constant>, then body content is also stored
  492. within index and can be retrieved from the index. By default, the body is tokenized and
  493. indexed, but not stored.
  494. </para>
  495. <para>
  496. Parsed documents may be augmented by the programmer with any other field:
  497. <programlisting language="php"><![CDATA[
  498. $doc = Zend_Search_Lucene_Document_Xlsx::loadXlsxFile($filename);
  499. $doc->addField(Zend_Search_Lucene_Field::UnIndexed(
  500. 'indexTime',
  501. time()));
  502. $doc->addField(Zend_Search_Lucene_Field::Text(
  503. 'annotation',
  504. 'Document annotation text'));
  505. $index->addDocument($doc);
  506. ]]></programlisting>
  507. </para>
  508. </sect2>
  509. </sect1>