Character Set

Extensibility

Text Analysis

The Zend_Search_Lucene_Analysis_Analyzer class is used by the indexer to tokenize document text fields.

The Zend_Search_Lucene_Analysis_Analyzer::getDefault() and Zend_Search_Lucene_Analysis_Analyzer::setDefault() methods are used to get and set the default analyzer.

You can assign your own text analyzer or choose it from the set of predefined analyzers: Zend_Search_Lucene_Analysis_Analyzer_Common_Text and Zend_Search_Lucene_Analysis_Analyzer_Common_Text_CaseInsensitive (default). Both of them interpret tokens as sequences of letters. Zend_Search_Lucene_Analysis_Analyzer_Common_Text_CaseInsensitive converts all tokens to lower case.

To switch between analyzers:

  1.  

The Zend_Search_Lucene_Analysis_Analyzer_Common class is designed to be an ancestor of all user defined analyzers. User should only define the reset() and nextToken() methods, which takes its string from the $_input member and returns tokens one by one (a NULL value indicates the end of the stream).

The nextToken() method should call the normalize() method on each token. This will allow you to use token filters with your analyzer.

Here is an example of a custom analyzer, which accepts words with digits as terms:

Example #1 Custom text Analyzer

  1. /**
  2. * Here is a custom text analyser, which treats words with digits as
  3. * one term
  4. *//**
  5.      * Reset token stream
  6.      *//**
  7.      * Tokenization stream API
  8.      * Get next token
  9.      * Returns null at the end of stream
  10.      *
  11.      * @return Zend_Search_Lucene_Analysis_Token|null
  12.      */// skip white space
  13. // read token
  14. // Empty token, end of stream.
  15. // Continue if token is skipped

Tokens Filtering

The Zend_Search_Lucene_Analysis_Analyzer_Common analyzer also offers a token filtering mechanism.

The Zend_Search_Lucene_Analysis_TokenFilter class provides an abstract interface for such filters. Your own filters should extend this class either directly or indirectly.

Any custom filter must implement the normalize() method which may transform input token or signal that the current token should be skipped.

There are three filters already defined in the analysis subpackage:

  • Zend_Search_Lucene_Analysis_TokenFilter_LowerCase

  • Zend_Search_Lucene_Analysis_TokenFilter_ShortWords

  • Zend_Search_Lucene_Analysis_TokenFilter_StopWords

The LowerCase filter is already used for Zend_Search_Lucene_Analysis_Analyzer_Common_Text_CaseInsensitive analyzer by default.

The ShortWords and StopWords filters may be used with pre-defined or custom analyzers like this:

  1. span style="color: #ff0000;">'a', 'an', 'at', 'the', 'and', 'or', 'is', 'am'
  1.  

The Zend_Search_Lucene_Analysis_TokenFilter_StopWords constructor takes an array of stop-words as an input. But stop-words may be also loaded from a file:

  1.  

This file should be a common text file with one word in each line. The '#' character marks a line as a comment.

The Zend_Search_Lucene_Analysis_TokenFilter_ShortWords constructor has one optional argument. This is the word length limit, set by default to 2.

Scoring Algorithms

The score of a document d for a query q is defined as follows:

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)

tf(t in d) - Zend_Search_Lucene_Search_Similarity::tf($freq) - a score factor based on the frequency of a term or phrase in a document.

idf(t) - Zend_Search_Lucene_Search_Similarity::idf($input, $reader) - a score factor for a simple term with the specified index.

getBoost(t.field in d) - the boost factor for the term field.

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.

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.

coord(q,d) - Zend_Search_Lucene_Search_Similarity::coord($overlap, $maxOverlap) - a score factor based on the fraction of all query terms that a document contains.

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.

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.

This does not affect ranking, but rather just attempts to make scores from different queries comparable.

The scoring algorithm can be customized by defining your own Similarity class. To do this extend the Zend_Search_Lucene_Search_Similarity class as defined below, then use the Zend_Search_Lucene_Search_Similarity::setDefault($similarity); method to set it as default.

  1. span style="color: #808080; font-style: italic;">/**
  2.      * It's not used now. Computes the amount of a sloppy phrase match,
  3.      * based on an edit distance.
  4.      */

Storage Containers

The abstract class Zend_Search_Lucene_Storage_Directory defines directory functionality.

The Zend_Search_Lucene constructor uses either a string or a Zend_Search_Lucene_Storage_Directory object as an input.

The Zend_Search_Lucene_Storage_Directory_Filesystem class implements directory functionality for a file system.

If a string is used as an input for the Zend_Search_Lucene constructor, then the index reader (Zend_Search_Lucene object) treats it as a file system path and instantiates the Zend_Search_Lucene_Storage_Directory_Filesystem object.

You can define your own directory implementation by extending the Zend_Search_Lucene_Storage_Directory class.

Zend_Search_Lucene_Storage_Directory methods:

  1. span style="color: #808080; font-style: italic;">/**
  2. * Closes the store.
  3. *
  4. * @return void
  5. *//**
  6. * Creates a new, empty file in the directory with the given $filename.
  7. *
  8. * @param string $name
  9. * @return void
  10. *//**
  11. * Removes an existing $filename in the directory.
  12. *
  13. * @param string $filename
  14. * @return void
  15. *//**
  16. * Returns true if a file with the given $filename exists.
  17. *
  18. * @param string $filename
  19. * @return boolean
  20. *//**
  21. * Returns the length of a $filename in the directory.
  22. *
  23. * @param string $filename
  24. * @return integer
  25. *//**
  26. * Returns the UNIX timestamp $filename was last modified.
  27. *
  28. * @param string $filename
  29. * @return integer
  30. *//**
  31. * Renames an existing file in the directory.
  32. *
  33. * @param string $from
  34. * @param string $to
  35. * @return void
  36. *//**
  37. * Sets the modified time of $filename to now.
  38. *
  39. * @param string $filename
  40. * @return void
  41. *//**
  42. * Returns a Zend_Search_Lucene_Storage_File object for a given
  43. * $filename in the directory.
  44. *
  45. * @param string $filename
  46. * @return Zend_Search_Lucene_Storage_File
  47. */

The getFileObject($filename) method of a Zend_Search_Lucene_Storage_Directory instance returns a Zend_Search_Lucene_Storage_File object.

The Zend_Search_Lucene_Storage_File abstract class implements file abstraction and index file reading primitives.

You must also extend Zend_Search_Lucene_Storage_File for your directory implementation.

Only two methods of Zend_Search_Lucene_Storage_File must be overridden in your implementation:

  1. span style="color: #808080; font-style: italic;">/**
  2.      * Sets the file position indicator and advances the file pointer.
  3.      * The new position, measured in bytes from the beginning of the file,
  4.      * is obtained by adding offset to the position specified by whence,
  5.      * whose values are defined as follows:
  6.      * SEEK_SET - Set position equal to offset bytes.
  7.      * SEEK_CUR - Set position to current location plus offset.
  8.      * SEEK_END - Set position to end-of-file plus offset. (To move to
  9.      * a position before the end-of-file, you need to pass a negative value
  10.      * in offset.)
  11.      * Upon success, returns 0; otherwise, returns -1
  12.      *
  13.      * @param integer $offset
  14.      * @param integer $whence
  15.      * @return integer
  16.      *//**
  17.      * Read a $length bytes from the file and advance the file pointer.
  18.      *
  19.      * @param integer $length
  20.      * @return string
  21.      */

Character Set