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