Field.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Search_Lucene
  17. * @subpackage Document
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * A field is a section of a Document. Each field has two parts,
  23. * a name and a value. Values may be free text or they may be atomic
  24. * keywords, which are not further processed. Such keywords may
  25. * be used to represent dates, urls, etc. Fields are optionally
  26. * stored in the index, so that they may be returned with hits
  27. * on the document.
  28. *
  29. * @category Zend
  30. * @package Zend_Search_Lucene
  31. * @subpackage Document
  32. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Search_Lucene_Field
  36. {
  37. /**
  38. * Field name
  39. *
  40. * @var string
  41. */
  42. public $name;
  43. /**
  44. * Field value
  45. *
  46. * @var boolean
  47. */
  48. public $value;
  49. /**
  50. * Field is to be stored in the index for return with search hits.
  51. *
  52. * @var boolean
  53. */
  54. public $isStored = false;
  55. /**
  56. * Field is to be indexed, so that it may be searched on.
  57. *
  58. * @var boolean
  59. */
  60. public $isIndexed = true;
  61. /**
  62. * Field should be tokenized as text prior to indexing.
  63. *
  64. * @var boolean
  65. */
  66. public $isTokenized = true;
  67. /**
  68. * Field is stored as binary.
  69. *
  70. * @var boolean
  71. */
  72. public $isBinary = false;
  73. /**
  74. * Field are stored as a term vector
  75. *
  76. * @var boolean
  77. */
  78. public $storeTermVector = false;
  79. /**
  80. * Field boost factor
  81. * It's not stored directly in the index, but affects on normalization factor
  82. *
  83. * @var float
  84. */
  85. public $boost = 1.0;
  86. /**
  87. * Field value encoding.
  88. *
  89. * @var string
  90. */
  91. public $encoding;
  92. /**
  93. * Object constructor
  94. *
  95. * @param string $name
  96. * @param string $value
  97. * @param string $encoding
  98. * @param boolean $isStored
  99. * @param boolean $isIndexed
  100. * @param boolean $isTokenized
  101. * @param boolean $isBinary
  102. */
  103. public function __construct($name, $value, $encoding, $isStored, $isIndexed, $isTokenized, $isBinary = false)
  104. {
  105. $this->name = $name;
  106. $this->value = $value;
  107. if (!$isBinary) {
  108. $this->encoding = $encoding;
  109. $this->isTokenized = $isTokenized;
  110. } else {
  111. $this->encoding = '';
  112. $this->isTokenized = false;
  113. }
  114. $this->isStored = $isStored;
  115. $this->isIndexed = $isIndexed;
  116. $this->isBinary = $isBinary;
  117. $this->storeTermVector = false;
  118. $this->boost = 1.0;
  119. }
  120. /**
  121. * Constructs a String-valued Field that is not tokenized, but is indexed
  122. * and stored. Useful for non-text fields, e.g. date or url.
  123. *
  124. * @param string $name
  125. * @param string $value
  126. * @param string $encoding
  127. * @return Zend_Search_Lucene_Field
  128. */
  129. public static function keyword($name, $value, $encoding = '')
  130. {
  131. return new self($name, $value, $encoding, true, true, false);
  132. }
  133. /**
  134. * Constructs a String-valued Field that is not tokenized nor indexed,
  135. * but is stored in the index, for return with hits.
  136. *
  137. * @param string $name
  138. * @param string $value
  139. * @param string $encoding
  140. * @return Zend_Search_Lucene_Field
  141. */
  142. public static function unIndexed($name, $value, $encoding = '')
  143. {
  144. return new self($name, $value, $encoding, true, false, false);
  145. }
  146. /**
  147. * Constructs a Binary String valued Field that is not tokenized nor indexed,
  148. * but is stored in the index, for return with hits.
  149. *
  150. * @param string $name
  151. * @param string $value
  152. * @param string $encoding
  153. * @return Zend_Search_Lucene_Field
  154. */
  155. public static function binary($name, $value)
  156. {
  157. return new self($name, $value, '', true, false, false, true);
  158. }
  159. /**
  160. * Constructs a String-valued Field that is tokenized and indexed,
  161. * and is stored in the index, for return with hits. Useful for short text
  162. * fields, like "title" or "subject". Term vector will not be stored for this field.
  163. *
  164. * @param string $name
  165. * @param string $value
  166. * @param string $encoding
  167. * @return Zend_Search_Lucene_Field
  168. */
  169. public static function text($name, $value, $encoding = '')
  170. {
  171. return new self($name, $value, $encoding, true, true, true);
  172. }
  173. /**
  174. * Constructs a String-valued Field that is tokenized and indexed,
  175. * but that is not stored in the index.
  176. *
  177. * @param string $name
  178. * @param string $value
  179. * @param string $encoding
  180. * @return Zend_Search_Lucene_Field
  181. */
  182. public static function unStored($name, $value, $encoding = '')
  183. {
  184. return new self($name, $value, $encoding, false, true, true);
  185. }
  186. /**
  187. * Get field value in UTF-8 encoding
  188. *
  189. * @return string
  190. */
  191. public function getUtf8Value()
  192. {
  193. if (strcasecmp($this->encoding, 'utf8' ) == 0 ||
  194. strcasecmp($this->encoding, 'utf-8') == 0 ) {
  195. return $this->value;
  196. } else {
  197. return (PHP_OS != 'AIX') ? iconv($this->encoding, 'UTF-8', $this->value) : iconv('ISO8859-1', 'UTF-8', $this->value);
  198. }
  199. }
  200. }