solr_field.inc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. /**
  3. * Logic around Solr field schema information.
  4. */
  5. class SearchApiSolrField {
  6. /**
  7. * @var array
  8. * Human-readable labels for Solr schema properties.
  9. */
  10. public static $schemaLabels = array(
  11. 'I' => 'Indexed',
  12. 'T' => 'Tokenized',
  13. 'S' => 'Stored',
  14. 'M' => 'Multivalued',
  15. 'V' => 'TermVector Stored',
  16. 'o' => 'Store Offset With TermVector',
  17. 'p' => 'Store Position With TermVector',
  18. 'O' => 'Omit Norms',
  19. 'L' => 'Lazy',
  20. 'B' => 'Binary',
  21. 'C' => 'Compressed',
  22. 'f' => 'Sort Missing First',
  23. 'l' => 'Sort Missing Last',
  24. );
  25. /**
  26. * @var stdclass
  27. * The original field object.
  28. */
  29. protected $field;
  30. /**
  31. * @var array
  32. * An array of schema properties for this field. This will be a subset of
  33. * the SearchApiSolrField::schemaLabels array.
  34. */
  35. protected $schema;
  36. /**
  37. * Constructs a field information object.
  38. *
  39. * @param stdClass $field
  40. * A field object from Solr's "Luke" servlet.
  41. */
  42. public function __construct($field) {
  43. $this->field = $field;
  44. }
  45. /**
  46. * Gets the raw information of the field.
  47. *
  48. * @return object
  49. * A field metadata object.
  50. */
  51. public function getRaw() {
  52. return $this->field;
  53. }
  54. /**
  55. * Gets the type of the Solr field, according to the Solr schema.
  56. *
  57. * Note that field types like "text", "boolean", and "date" are conventions,
  58. * but their presence and behavior are entirely determined by the particular
  59. * schema.xml file used by a Solr core.
  60. *
  61. * @return string
  62. * The type of the Solr field.
  63. */
  64. public function getType() {
  65. return $this->field->type;
  66. }
  67. /**
  68. * Gets an array of field properties.
  69. *
  70. * @return array
  71. * An array of properties describing the solr schema. The array keys are
  72. * single-character codes, and the values are human-readable labels. This
  73. * will be a subset of the SearchApiSolrField::schemaLabels array.
  74. */
  75. public function getSchema() {
  76. if (!isset($this->schema)) {
  77. foreach (str_split(str_replace('-', '', $this->field->schema)) as $key) {
  78. $this->schema[$key] = isset(self::$schemaLabels[$key]) ? self::$schemaLabels[$key] : $key;
  79. }
  80. }
  81. return $this->schema;
  82. }
  83. /**
  84. * Gets the "dynamic base" of this field.
  85. *
  86. * This typically looks like 'ss_*, and is used to aggregate fields based on
  87. * "hungarian" naming conventions.
  88. *
  89. * @return string
  90. * The mask describing the solr aggregate field, if there is one.
  91. */
  92. public function getDynamicBase() {
  93. return isset($this->field->dynamicBase) ? $this->field->dynamicBase : NULL;
  94. }
  95. /**
  96. * Determines whether this field may be suitable for use as a key field.
  97. *
  98. * Unfortunately, it seems like the best way to find an actual uniqueKey field
  99. * according to Solr is to examine the Solr core's schema.xml.
  100. *
  101. * @return bool
  102. * Whether the field is suitable for use as a key.
  103. */
  104. public function isPossibleKey() {
  105. return !$this->getDynamicBase()
  106. && !in_array($this->getType(), array('boolean', 'date', 'text'))
  107. && $this->isStored()
  108. && !$this->isMultivalued();
  109. }
  110. /**
  111. * Determines whether a field is suitable for sorting.
  112. *
  113. * In order for a field to yield useful sorted results in Solr, it must be
  114. * indexed, not multivalued, and not tokenized. It's ok if a field is
  115. * tokenized and yields only one token, but there's no general way to check
  116. * for that.
  117. *
  118. * @return bool
  119. * Whether the field is suitable for sorting.
  120. */
  121. public function isSortable() {
  122. return $this->isIndexed()
  123. && !$this->isMultivalued()
  124. && !$this->isTokenized();
  125. }
  126. /**
  127. * Determines whether this field is indexed.
  128. *
  129. * @return bool
  130. * TRUE if the field is indexed, FALSE otherwise.
  131. */
  132. public function isIndexed() {
  133. $this->getSchema();
  134. return isset($this->schema['I']);
  135. }
  136. /**
  137. * Determines whether this field is tokenized.
  138. *
  139. * @return bool
  140. * TRUE if the field is tokenized, FALSE otherwise.
  141. */
  142. public function isTokenized() {
  143. $this->getSchema();
  144. return isset($this->schema['T']);
  145. }
  146. /**
  147. * Determines whether this field is stored.
  148. *
  149. * @return bool
  150. * TRUE if the field is stored, FALSE otherwise.
  151. */
  152. public function isStored() {
  153. $this->getSchema();
  154. return isset($this->schema['S']);
  155. }
  156. /**
  157. * Determines whether this field is multi-valued.
  158. *
  159. * @return bool
  160. * TRUE if the field is multi-valued, FALSE otherwise.
  161. */
  162. public function isMultivalued() {
  163. $this->getSchema();
  164. return isset($this->schema['M']);
  165. }
  166. /**
  167. * Determines whether this field has stored term vectors.
  168. *
  169. * @return bool
  170. * TRUE if the field has stored term vectors, FALSE otherwise.
  171. */
  172. public function isTermVectorStored() {
  173. $this->getSchema();
  174. return isset($this->schema['V']);
  175. }
  176. /**
  177. * Determines whether this field has the "termOffsets" option set.
  178. *
  179. * @return bool
  180. * TRUE if the field has the "termOffsets" option set, FALSE otherwise.
  181. */
  182. public function isStoreOffsetWithTermVector() {
  183. $this->getSchema();
  184. return isset($this->schema['o']);
  185. }
  186. /**
  187. * Determines whether this field has the "termPositions" option set.
  188. *
  189. * @return bool
  190. * TRUE if the field has the "termPositions" option set, FALSE otherwise.
  191. */
  192. public function isStorePositionWithTermVector() {
  193. $this->getSchema();
  194. return isset($this->schema['p']);
  195. }
  196. /**
  197. * Determines whether this field omits norms when indexing.
  198. *
  199. * @return bool
  200. * TRUE if the field omits norms, FALSE otherwise.
  201. */
  202. public function isOmitNorms() {
  203. $this->getSchema();
  204. return isset($this->schema['O']);
  205. }
  206. /**
  207. * Determines whether this field is lazy-loaded.
  208. *
  209. * @return bool
  210. * TRUE if the field is lazy-loaded, FALSE otherwise.
  211. */
  212. public function isLazy() {
  213. $this->getSchema();
  214. return isset($this->schema['L']);
  215. }
  216. /**
  217. * Determines whether this field is binary.
  218. *
  219. * @return bool
  220. * TRUE if the field is binary, FALSE otherwise.
  221. */
  222. public function isBinary() {
  223. $this->getSchema();
  224. return isset($this->schema['B']);
  225. }
  226. /**
  227. * Determines whether this field is compressed.
  228. *
  229. * @return bool
  230. * TRUE if the field is compressed, FALSE otherwise.
  231. */
  232. public function isCompressed() {
  233. $this->getSchema();
  234. return isset($this->schema['C']);
  235. }
  236. /**
  237. * Determines whether this field sorts missing entries first.
  238. *
  239. * @return bool
  240. * TRUE if the field sorts missing entries first, FALSE otherwise.
  241. */
  242. public function isSortMissingFirst() {
  243. $this->getSchema();
  244. return isset($this->schema['f']);
  245. }
  246. /**
  247. * Determines whether this field sorts missing entries last.
  248. *
  249. * @return bool
  250. * TRUE if the field sorts missing entries last, FALSE otherwise.
  251. */
  252. public function isSortMissingLast() {
  253. $this->getSchema();
  254. return isset($this->schema['l']);
  255. }
  256. }