Zend_Search_Lucene-Extending.xml 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.search.lucene.extending">
  4. <title>Extensibility</title>
  5. <sect2 id="zend.search.lucene.extending.analysis">
  6. <title>Text Analysis</title>
  7. <para>
  8. The <classname>Zend_Search_Lucene_Analysis_Analyzer</classname> class is used by the indexer to tokenize document
  9. text fields.
  10. </para>
  11. <para>
  12. The <methodname>Zend_Search_Lucene_Analysis_Analyzer::getDefault()</methodname> and <code>
  13. Zend_Search_Lucene_Analysis_Analyzer::setDefault()</code> methods are used
  14. to get and set the default analyzer.
  15. </para>
  16. <para>
  17. You can assign your own text analyzer or choose it from the set of predefined analyzers:
  18. <classname>Zend_Search_Lucene_Analysis_Analyzer_Common_Text</classname> and
  19. <classname>Zend_Search_Lucene_Analysis_Analyzer_Common_Text_CaseInsensitive</classname> (default).
  20. Both of them interpret tokens as sequences of letters.
  21. <classname>Zend_Search_Lucene_Analysis_Analyzer_Common_Text_CaseInsensitive</classname> converts all tokens
  22. to lower case.
  23. </para>
  24. <para>
  25. To switch between analyzers:
  26. </para>
  27. <programlisting language="php"><![CDATA[
  28. Zend_Search_Lucene_Analysis_Analyzer::setDefault(
  29. new Zend_Search_Lucene_Analysis_Analyzer_Common_Text());
  30. ...
  31. $index->addDocument($doc);
  32. ]]></programlisting>
  33. <para>
  34. The <classname>Zend_Search_Lucene_Analysis_Analyzer_Common</classname> class is designed to be an ancestor of all user
  35. defined analyzers. User should only define the <methodname>reset()</methodname> and <methodname>nextToken()</methodname> methods,
  36. which takes its string from the $_input member and returns tokens one by one
  37. (a <constant>NULL</constant> value indicates the end of the stream).
  38. </para>
  39. <para>
  40. The <methodname>nextToken()</methodname> method should call the <methodname>normalize()</methodname> method on each
  41. token. This will allow you to use token filters with your analyzer.
  42. </para>
  43. <para>
  44. Here is an example of a custom analyzer, which accepts words with digits as terms:
  45. <example id="zend.search.lucene.extending.analysis.example-1">
  46. <title>Custom text Analyzer</title>
  47. <programlisting language="php"><![CDATA[
  48. /**
  49. * Here is a custom text analyser, which treats words with digits as
  50. * one term
  51. */
  52. class My_Analyzer extends Zend_Search_Lucene_Analysis_Analyzer_Common
  53. {
  54. private $_position;
  55. /**
  56. * Reset token stream
  57. */
  58. public function reset()
  59. {
  60. $this->_position = 0;
  61. }
  62. /**
  63. * Tokenization stream API
  64. * Get next token
  65. * Returns null at the end of stream
  66. *
  67. * @return Zend_Search_Lucene_Analysis_Token|null
  68. */
  69. public function nextToken()
  70. {
  71. if ($this->_input === null) {
  72. return null;
  73. }
  74. while ($this->_position < strlen($this->_input)) {
  75. // skip white space
  76. while ($this->_position < strlen($this->_input) &&
  77. !ctype_alnum( $this->_input[$this->_position] )) {
  78. $this->_position++;
  79. }
  80. $termStartPosition = $this->_position;
  81. // read token
  82. while ($this->_position < strlen($this->_input) &&
  83. ctype_alnum( $this->_input[$this->_position] )) {
  84. $this->_position++;
  85. }
  86. // Empty token, end of stream.
  87. if ($this->_position == $termStartPosition) {
  88. return null;
  89. }
  90. $token = new Zend_Search_Lucene_Analysis_Token(
  91. substr($this->_input,
  92. $termStartPosition,
  93. $this->_position -
  94. $termStartPosition),
  95. $termStartPosition,
  96. $this->_position);
  97. $token = $this->normalize($token);
  98. if ($token !== null) {
  99. return $token;
  100. }
  101. // Continue if token is skipped
  102. }
  103. return null;
  104. }
  105. }
  106. Zend_Search_Lucene_Analysis_Analyzer::setDefault(
  107. new My_Analyzer());
  108. ]]></programlisting>
  109. </example>
  110. </para>
  111. </sect2>
  112. <sect2 id="zend.search.lucene.extending.filters">
  113. <title>Tokens Filtering</title>
  114. <para>
  115. The <classname>Zend_Search_Lucene_Analysis_Analyzer_Common</classname> analyzer also offers a token filtering
  116. mechanism.
  117. </para>
  118. <para>
  119. The <classname>Zend_Search_Lucene_Analysis_TokenFilter</classname> class provides an abstract interface for such filters.
  120. Your own filters should extend this class either directly or indirectly.
  121. </para>
  122. <para>
  123. Any custom filter must implement the <methodname>normalize()</methodname> method which may transform input token or signal that
  124. the current token should be skipped.
  125. </para>
  126. <para>
  127. There are three filters already defined in the analysis subpackage:
  128. <itemizedlist>
  129. <listitem>
  130. <para>
  131. <classname>Zend_Search_Lucene_Analysis_TokenFilter_LowerCase</classname>
  132. </para>
  133. </listitem>
  134. <listitem>
  135. <para>
  136. <classname>Zend_Search_Lucene_Analysis_TokenFilter_ShortWords</classname>
  137. </para>
  138. </listitem>
  139. <listitem>
  140. <para>
  141. <classname>Zend_Search_Lucene_Analysis_TokenFilter_StopWords</classname>
  142. </para>
  143. </listitem>
  144. </itemizedlist>
  145. </para>
  146. <para>
  147. The <code>LowerCase</code> filter is already used for
  148. <classname>Zend_Search_Lucene_Analysis_Analyzer_Common_Text_CaseInsensitive</classname> analyzer
  149. by default.
  150. </para>
  151. <para>
  152. The <code>ShortWords</code> and <code>StopWords</code> filters may be used with pre-defined or custom
  153. analyzers like this:
  154. <programlisting language="php"><![CDATA[
  155. $stopWords = array('a', 'an', 'at', 'the', 'and', 'or', 'is', 'am');
  156. $stopWordsFilter =
  157. new Zend_Search_Lucene_Analysis_TokenFilter_StopWords($stopWords);
  158. $analyzer =
  159. new Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum_CaseInsensitive();
  160. $analyzer->addFilter($stopWordsFilter);
  161. Zend_Search_Lucene_Analysis_Analyzer::setDefault($analyzer);
  162. ]]></programlisting>
  163. <programlisting language="php"><![CDATA[
  164. $shortWordsFilter = new Zend_Search_Lucene_Analysis_TokenFilter_ShortWords();
  165. $analyzer =
  166. new Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum_CaseInsensitive();
  167. $analyzer->addFilter($shortWordsFilter);
  168. Zend_Search_Lucene_Analysis_Analyzer::setDefault($analyzer);
  169. ]]></programlisting>
  170. </para>
  171. <para>
  172. The <classname>Zend_Search_Lucene_Analysis_TokenFilter_StopWords</classname> constructor takes an array of stop-words
  173. as an input. But stop-words may be also loaded from a file:
  174. <programlisting language="php"><![CDATA[
  175. $stopWordsFilter = new Zend_Search_Lucene_Analysis_TokenFilter_StopWords();
  176. $stopWordsFilter->loadFromFile($my_stopwords_file);
  177. $analyzer =
  178. new Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum_CaseInsensitive();
  179. $analyzer->addFilter($stopWordsFilter);
  180. Zend_Search_Lucene_Analysis_Analyzer::setDefault($analyzer);
  181. ]]></programlisting>
  182. This file should be a common text file with one word in each line. The '#' character marks a line as a comment.
  183. </para>
  184. <para>
  185. The <classname>Zend_Search_Lucene_Analysis_TokenFilter_ShortWords</classname> constructor has one optional argument.
  186. This is the word length limit, set by default to 2.
  187. </para>
  188. </sect2>
  189. <sect2 id="zend.search.lucene.extending.scoring">
  190. <title>Scoring Algorithms</title>
  191. <para>
  192. The score of a document <literal>d</literal> for a query <literal>q</literal>
  193. is defined as follows:
  194. </para>
  195. <para>
  196. <code>score(q,d) = sum( tf(t in d) * idf(t) * getBoost(t.field in d) * lengthNorm(t.field in d) ) *
  197. coord(q,d) * queryNorm(q)</code>
  198. </para>
  199. <para>
  200. tf(t in d) - <methodname>Zend_Search_Lucene_Search_Similarity::tf($freq)</methodname> - a score factor based on the frequency of a term or phrase in a document.
  201. </para>
  202. <para>
  203. idf(t) - <methodname>Zend_Search_Lucene_Search_Similarity::idf($input, $reader)</methodname> - a score factor for a simple term with the specified index.
  204. </para>
  205. <para>
  206. getBoost(t.field in d) - the boost factor for the term field.
  207. </para>
  208. <para>
  209. lengthNorm($term) - the normalization value for a field given the total
  210. number of terms contained in a field. This value is stored within the index.
  211. These values, together with field boosts, are stored in an index and multiplied
  212. into scores for hits on each field by the search code.
  213. </para>
  214. <para>
  215. Matches in longer fields are less precise, so implementations of this method
  216. usually return smaller values when numTokens is large, and larger values when numTokens is small.
  217. </para>
  218. <para>
  219. coord(q,d) - <methodname>Zend_Search_Lucene_Search_Similarity::coord($overlap, $maxOverlap)</methodname> - a
  220. score factor based on the fraction of all query terms that a document contains.
  221. </para>
  222. <para>
  223. The presence of a large portion of the query terms indicates a better match
  224. with the query, so implementations of this method usually return larger values
  225. when the ratio between these parameters is large and smaller values when
  226. the ratio between them is small.
  227. </para>
  228. <para>
  229. queryNorm(q) - the normalization value for a query given the sum of the squared weights
  230. of each of the query terms. This value is then multiplied into the weight of each query
  231. term.
  232. </para>
  233. <para>
  234. This does not affect ranking, but rather just attempts to make scores from different
  235. queries comparable.
  236. </para>
  237. <para>
  238. The scoring algorithm can be customized by defining your own Similarity class. To do this
  239. extend the <classname>Zend_Search_Lucene_Search_Similarity</classname> class as defined below, then use
  240. the <classname>Zend_Search_Lucene_Search_Similarity::setDefault($similarity);</classname> method to set it as default.
  241. </para>
  242. <programlisting language="php"><![CDATA[
  243. class MySimilarity extends Zend_Search_Lucene_Search_Similarity {
  244. public function lengthNorm($fieldName, $numTerms) {
  245. return 1.0/sqrt($numTerms);
  246. }
  247. public function queryNorm($sumOfSquaredWeights) {
  248. return 1.0/sqrt($sumOfSquaredWeights);
  249. }
  250. public function tf($freq) {
  251. return sqrt($freq);
  252. }
  253. /**
  254. * It's not used now. Computes the amount of a sloppy phrase match,
  255. * based on an edit distance.
  256. */
  257. public function sloppyFreq($distance) {
  258. return 1.0;
  259. }
  260. public function idfFreq($docFreq, $numDocs) {
  261. return log($numDocs/(float)($docFreq+1)) + 1.0;
  262. }
  263. public function coord($overlap, $maxOverlap) {
  264. return $overlap/(float)$maxOverlap;
  265. }
  266. }
  267. $mySimilarity = new MySimilarity();
  268. Zend_Search_Lucene_Search_Similarity::setDefault($mySimilarity);
  269. ]]></programlisting>
  270. </sect2>
  271. <sect2 id="zend.search.lucene.extending.storage">
  272. <title>Storage Containers</title>
  273. <para>
  274. The abstract class <classname>Zend_Search_Lucene_Storage_Directory</classname> defines directory functionality.
  275. </para>
  276. <para>
  277. The <classname>Zend_Search_Lucene</classname> constructor uses either a string or a
  278. <classname>Zend_Search_Lucene_Storage_Directory</classname> object
  279. as an input.
  280. </para>
  281. <para>
  282. The <classname>Zend_Search_Lucene_Storage_Directory_Filesystem</classname> class implements directory
  283. functionality for a file system.
  284. </para>
  285. <para>
  286. If a string is used as an input for the <classname>Zend_Search_Lucene</classname> constructor, then the index
  287. reader (<classname>Zend_Search_Lucene</classname> object) treats it as a file system path and instantiates
  288. the <classname>Zend_Search_Lucene_Storage_Directory_Filesystem</classname> object.
  289. </para>
  290. <para>
  291. You can define your own directory implementation by extending the
  292. <classname>Zend_Search_Lucene_Storage_Directory</classname> class.
  293. </para>
  294. <para>
  295. <classname>Zend_Search_Lucene_Storage_Directory</classname> methods:
  296. </para>
  297. <programlisting language="php"><![CDATA[
  298. abstract class Zend_Search_Lucene_Storage_Directory {
  299. /**
  300. * Closes the store.
  301. *
  302. * @return void
  303. */
  304. abstract function close();
  305. /**
  306. * Creates a new, empty file in the directory with the given $filename.
  307. *
  308. * @param string $name
  309. * @return void
  310. */
  311. abstract function createFile($filename);
  312. /**
  313. * Removes an existing $filename in the directory.
  314. *
  315. * @param string $filename
  316. * @return void
  317. */
  318. abstract function deleteFile($filename);
  319. /**
  320. * Returns true if a file with the given $filename exists.
  321. *
  322. * @param string $filename
  323. * @return boolean
  324. */
  325. abstract function fileExists($filename);
  326. /**
  327. * Returns the length of a $filename in the directory.
  328. *
  329. * @param string $filename
  330. * @return integer
  331. */
  332. abstract function fileLength($filename);
  333. /**
  334. * Returns the UNIX timestamp $filename was last modified.
  335. *
  336. * @param string $filename
  337. * @return integer
  338. */
  339. abstract function fileModified($filename);
  340. /**
  341. * Renames an existing file in the directory.
  342. *
  343. * @param string $from
  344. * @param string $to
  345. * @return void
  346. */
  347. abstract function renameFile($from, $to);
  348. /**
  349. * Sets the modified time of $filename to now.
  350. *
  351. * @param string $filename
  352. * @return void
  353. */
  354. abstract function touchFile($filename);
  355. /**
  356. * Returns a Zend_Search_Lucene_Storage_File object for a given
  357. * $filename in the directory.
  358. *
  359. * @param string $filename
  360. * @return Zend_Search_Lucene_Storage_File
  361. */
  362. abstract function getFileObject($filename);
  363. }
  364. ]]></programlisting>
  365. <para>
  366. The <methodname>getFileObject($filename)</methodname> method of a <classname>Zend_Search_Lucene_Storage_Directory</classname>
  367. instance returns a <classname>Zend_Search_Lucene_Storage_File</classname> object.
  368. </para>
  369. <para>
  370. The <classname>Zend_Search_Lucene_Storage_File</classname> abstract class implements file abstraction and index
  371. file reading primitives.
  372. </para>
  373. <para>
  374. You must also extend <classname>Zend_Search_Lucene_Storage_File</classname> for your directory implementation.
  375. </para>
  376. <para>
  377. Only two methods of <classname>Zend_Search_Lucene_Storage_File</classname> must be overridden in your
  378. implementation:
  379. </para>
  380. <programlisting language="php"><![CDATA[
  381. class MyFile extends Zend_Search_Lucene_Storage_File {
  382. /**
  383. * Sets the file position indicator and advances the file pointer.
  384. * The new position, measured in bytes from the beginning of the file,
  385. * is obtained by adding offset to the position specified by whence,
  386. * whose values are defined as follows:
  387. * SEEK_SET - Set position equal to offset bytes.
  388. * SEEK_CUR - Set position to current location plus offset.
  389. * SEEK_END - Set position to end-of-file plus offset. (To move to
  390. * a position before the end-of-file, you need to pass a negative value
  391. * in offset.)
  392. * Upon success, returns 0; otherwise, returns -1
  393. *
  394. * @param integer $offset
  395. * @param integer $whence
  396. * @return integer
  397. */
  398. public function seek($offset, $whence=SEEK_SET) {
  399. ...
  400. }
  401. /**
  402. * Read a $length bytes from the file and advance the file pointer.
  403. *
  404. * @param integer $length
  405. * @return string
  406. */
  407. protected function _fread($length=1) {
  408. ...
  409. }
  410. }
  411. ]]></programlisting>
  412. </sect2>
  413. </sect1>
  414. <!--
  415. vim:se ts=4 sw=4 et:
  416. -->