Zend_Search_Lucene-Extending.xml 17 KB

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