Field.php 5.9 KB

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