Zend_Search_Lucene-Extending.xml 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. <programlisting language="php"><![CDATA[
  158. $stopWords = array('a', 'an', 'at', 'the', 'and', 'or', 'is', 'am');
  159. $stopWordsFilter =
  160. new Zend_Search_Lucene_Analysis_TokenFilter_StopWords($stopWords);
  161. $analyzer =
  162. new Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum_CaseInsensitive();
  163. $analyzer->addFilter($stopWordsFilter);
  164. Zend_Search_Lucene_Analysis_Analyzer::setDefault($analyzer);
  165. ]]></programlisting>
  166. <programlisting language="php"><![CDATA[
  167. $shortWordsFilter = new Zend_Search_Lucene_Analysis_TokenFilter_ShortWords();
  168. $analyzer =
  169. new Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum_CaseInsensitive();
  170. $analyzer->addFilter($shortWordsFilter);
  171. Zend_Search_Lucene_Analysis_Analyzer::setDefault($analyzer);
  172. ]]></programlisting>
  173. </para>
  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. <programlisting language="php"><![CDATA[
  178. $stopWordsFilter = new Zend_Search_Lucene_Analysis_TokenFilter_StopWords();
  179. $stopWordsFilter->loadFromFile($my_stopwords_file);
  180. $analyzer =
  181. new Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum_CaseInsensitive();
  182. $analyzer->addFilter($stopWordsFilter);
  183. Zend_Search_Lucene_Analysis_Analyzer::setDefault($analyzer);
  184. ]]></programlisting>
  185. This file should be a common text file with one word in each line. The '#' character
  186. marks a line as a comment.
  187. </para>
  188. <para>
  189. The <classname>Zend_Search_Lucene_Analysis_TokenFilter_ShortWords</classname>
  190. constructor has one optional argument. This is the word length limit, set by default to
  191. 2.
  192. </para>
  193. </sect2>
  194. <sect2 id="zend.search.lucene.extending.scoring">
  195. <title>Scoring Algorithms</title>
  196. <para>
  197. The score of a document <literal>d</literal> for a query <literal>q</literal>
  198. is defined as follows:
  199. </para>
  200. <para>
  201. <code>score(q,d) = sum( tf(t in d) * idf(t) * getBoost(t.field in d) *
  202. lengthNorm(t.field in d) ) * coord(q,d) * queryNorm(q)</code>
  203. </para>
  204. <para>
  205. tf(t in d) - <methodname>Zend_Search_Lucene_Search_Similarity::tf($freq)</methodname> -
  206. a score factor based on the frequency of a term or phrase in a document.
  207. </para>
  208. <para>
  209. idf(t) - <methodname>Zend_Search_Lucene_Search_Similarity::idf($input,
  210. $reader)</methodname> - a score factor for a simple term with the specified index.
  211. </para>
  212. <para>
  213. getBoost(t.field in d) - the boost factor for the term field.
  214. </para>
  215. <para>
  216. lengthNorm($term) - the normalization value for a field given the total
  217. number of terms contained in a field. This value is stored within the index.
  218. These values, together with field boosts, are stored in an index and multiplied
  219. into scores for hits on each field by the search code.
  220. </para>
  221. <para>
  222. Matches in longer fields are less precise, so implementations of this method usually
  223. return smaller values when numTokens is large, and larger values when numTokens is
  224. small.
  225. </para>
  226. <para>
  227. coord(q,d) - <methodname>Zend_Search_Lucene_Search_Similarity::coord($overlap,
  228. $maxOverlap)</methodname> - a score factor based on the fraction of all query terms
  229. that a document contains.
  230. </para>
  231. <para>
  232. The presence of a large portion of the query terms indicates a better match
  233. with the query, so implementations of this method usually return larger values
  234. when the ratio between these parameters is large and smaller values when
  235. the ratio between them is small.
  236. </para>
  237. <para>
  238. queryNorm(q) - the normalization value for a query given the sum of the squared weights
  239. of each of the query terms. This value is then multiplied into the weight of each query
  240. term.
  241. </para>
  242. <para>
  243. This does not affect ranking, but rather just attempts to make scores from different
  244. queries comparable.
  245. </para>
  246. <para>
  247. The scoring algorithm can be customized by defining your own Similarity class. To do
  248. this extend the <classname>Zend_Search_Lucene_Search_Similarity</classname> class as
  249. defined below, then use the
  250. <classname>Zend_Search_Lucene_Search_Similarity::setDefault($similarity);</classname>
  251. method to set it as default.
  252. </para>
  253. <programlisting language="php"><![CDATA[
  254. class MySimilarity extends Zend_Search_Lucene_Search_Similarity {
  255. public function lengthNorm($fieldName, $numTerms) {
  256. return 1.0/sqrt($numTerms);
  257. }
  258. public function queryNorm($sumOfSquaredWeights) {
  259. return 1.0/sqrt($sumOfSquaredWeights);
  260. }
  261. public function tf($freq) {
  262. return sqrt($freq);
  263. }
  264. /**
  265. * It's not used now. Computes the amount of a sloppy phrase match,
  266. * based on an edit distance.
  267. */
  268. public function sloppyFreq($distance) {
  269. return 1.0;
  270. }
  271. public function idfFreq($docFreq, $numDocs) {
  272. return log($numDocs/(float)($docFreq+1)) + 1.0;
  273. }
  274. public function coord($overlap, $maxOverlap) {
  275. return $overlap/(float)$maxOverlap;
  276. }
  277. }
  278. $mySimilarity = new MySimilarity();
  279. Zend_Search_Lucene_Search_Similarity::setDefault($mySimilarity);
  280. ]]></programlisting>
  281. </sect2>
  282. <sect2 id="zend.search.lucene.extending.storage">
  283. <title>Storage Containers</title>
  284. <para>
  285. The abstract class <classname>Zend_Search_Lucene_Storage_Directory</classname> defines
  286. directory functionality.
  287. </para>
  288. <para>
  289. The <classname>Zend_Search_Lucene</classname> constructor uses either a string or a
  290. <classname>Zend_Search_Lucene_Storage_Directory</classname> object as an input.
  291. </para>
  292. <para>
  293. The <classname>Zend_Search_Lucene_Storage_Directory_Filesystem</classname> class
  294. implements directory functionality for a file system.
  295. </para>
  296. <para>
  297. If a string is used as an input for the <classname>Zend_Search_Lucene</classname>
  298. constructor, then the index reader (<classname>Zend_Search_Lucene</classname> object)
  299. treats it as a file system path and instantiates the
  300. <classname>Zend_Search_Lucene_Storage_Directory_Filesystem</classname> object.
  301. </para>
  302. <para>
  303. You can define your own directory implementation by extending the
  304. <classname>Zend_Search_Lucene_Storage_Directory</classname> class.
  305. </para>
  306. <para>
  307. <classname>Zend_Search_Lucene_Storage_Directory</classname> methods:
  308. </para>
  309. <programlisting language="php"><![CDATA[
  310. abstract class Zend_Search_Lucene_Storage_Directory {
  311. /**
  312. * Closes the store.
  313. *
  314. * @return void
  315. */
  316. abstract function close();
  317. /**
  318. * Creates a new, empty file in the directory with the given $filename.
  319. *
  320. * @param string $name
  321. * @return void
  322. */
  323. abstract function createFile($filename);
  324. /**
  325. * Removes an existing $filename in the directory.
  326. *
  327. * @param string $filename
  328. * @return void
  329. */
  330. abstract function deleteFile($filename);
  331. /**
  332. * Returns true if a file with the given $filename exists.
  333. *
  334. * @param string $filename
  335. * @return boolean
  336. */
  337. abstract function fileExists($filename);
  338. /**
  339. * Returns the length of a $filename in the directory.
  340. *
  341. * @param string $filename
  342. * @return integer
  343. */
  344. abstract function fileLength($filename);
  345. /**
  346. * Returns the UNIX timestamp $filename was last modified.
  347. *
  348. * @param string $filename
  349. * @return integer
  350. */
  351. abstract function fileModified($filename);
  352. /**
  353. * Renames an existing file in the directory.
  354. *
  355. * @param string $from
  356. * @param string $to
  357. * @return void
  358. */
  359. abstract function renameFile($from, $to);
  360. /**
  361. * Sets the modified time of $filename to now.
  362. *
  363. * @param string $filename
  364. * @return void
  365. */
  366. abstract function touchFile($filename);
  367. /**
  368. * Returns a Zend_Search_Lucene_Storage_File object for a given
  369. * $filename in the directory.
  370. *
  371. * @param string $filename
  372. * @return Zend_Search_Lucene_Storage_File
  373. */
  374. abstract function getFileObject($filename);
  375. }
  376. ]]></programlisting>
  377. <para>
  378. The <methodname>getFileObject($filename)</methodname> method of a
  379. <classname>Zend_Search_Lucene_Storage_Directory</classname> instance returns a
  380. <classname>Zend_Search_Lucene_Storage_File</classname> object.
  381. </para>
  382. <para>
  383. The <classname>Zend_Search_Lucene_Storage_File</classname> abstract class implements
  384. file abstraction and index file reading primitives.
  385. </para>
  386. <para>
  387. You must also extend <classname>Zend_Search_Lucene_Storage_File</classname> for your
  388. directory implementation.
  389. </para>
  390. <para>
  391. Only two methods of <classname>Zend_Search_Lucene_Storage_File</classname> must be
  392. overridden in your implementation:
  393. </para>
  394. <programlisting language="php"><![CDATA[
  395. class MyFile extends Zend_Search_Lucene_Storage_File {
  396. /**
  397. * Sets the file position indicator and advances the file pointer.
  398. * The new position, measured in bytes from the beginning of the file,
  399. * is obtained by adding offset to the position specified by whence,
  400. * whose values are defined as follows:
  401. * SEEK_SET - Set position equal to offset bytes.
  402. * SEEK_CUR - Set position to current location plus offset.
  403. * SEEK_END - Set position to end-of-file plus offset. (To move to
  404. * a position before the end-of-file, you need to pass a negative value
  405. * in offset.)
  406. * Upon success, returns 0; otherwise, returns -1
  407. *
  408. * @param integer $offset
  409. * @param integer $whence
  410. * @return integer
  411. */
  412. public function seek($offset, $whence=SEEK_SET) {
  413. ...
  414. }
  415. /**
  416. * Read a $length bytes from the file and advance the file pointer.
  417. *
  418. * @param integer $length
  419. * @return string
  420. */
  421. protected function _fread($length=1) {
  422. ...
  423. }
  424. }
  425. ]]></programlisting>
  426. </sect2>
  427. </sect1>
  428. <!--
  429. vim:se ts=4 sw=4 et:
  430. -->