solr_field.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. * Constructor.
  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. * Get the type of the Solr field, according to the Solr schema.
  47. *
  48. * Note that field types like "text", "boolean", and "date" are conventions,
  49. * but their presence and behavior are entirely determined by the particular
  50. * schema.xml file used by a Solr core.
  51. *
  52. * @return string
  53. * The type of the Solr field.
  54. */
  55. public function getType() {
  56. return $this->field->type;
  57. }
  58. /**
  59. * Get an array of field properties.
  60. *
  61. * @return array
  62. * An array of properties describing the solr schema. The array keys are
  63. * single-character codes, and the values are human-readable labels. This
  64. * will be a subset of the SearchApiSolrField::schemaLabels array.
  65. */
  66. public function getSchema() {
  67. if (!isset($this->schema)) {
  68. foreach (str_split(str_replace('-', '', $this->field->schema)) as $key) {
  69. $this->schema[$key] = isset(self::$schemaLabels[$key]) ? self::$schemaLabels[$key] : $key;
  70. }
  71. }
  72. return $this->schema;
  73. }
  74. /**
  75. * Get the "dynamic base" of this field.
  76. *
  77. * This typically looks like 'ss_*, and is used to aggregate fields based on
  78. * "hungarian" naming conventions.
  79. *
  80. * @return string
  81. * The mask describing the solr aggregate field, if there is one.
  82. */
  83. public function getDynamicBase() {
  84. return isset($this->field->dynamicBase) ? $this->field->dynamicBase : NULL;
  85. }
  86. /**
  87. * Determine whether this field may be suitable for use as a key field.
  88. *
  89. * Unfortunately, it seems like the best way to find an actual uniqueKey field
  90. * according to Solr is to examine the Solr core's schema.xml.
  91. *
  92. * @return boolean
  93. * Whether the field is suitable for use as a key.
  94. */
  95. public function isPossibleKey() {
  96. return !$this->getDynamicBase()
  97. && !in_array($this->getType(), array('boolean', 'date', 'text'))
  98. && $this->isStored()
  99. && !$this->isMultivalued();
  100. }
  101. /**
  102. * Determine whether a field is suitable for sorting.
  103. *
  104. * In order for a field to yield useful sorted results in Solr, it must be
  105. * indexed, not multivalued, and not tokenized. It's ok if a field is
  106. * tokenized and yields only one token, but there's no general way to check
  107. * for that.
  108. *
  109. * @return boolean
  110. * Whether the field is suitable for sorting.
  111. */
  112. public function isSortable() {
  113. return $this->isIndexed()
  114. && !$this->isMultivalued()
  115. && !$this->isTokenized();
  116. }
  117. /**
  118. * The following functions return information about specific properties of this field.
  119. *
  120. * @return boolean
  121. */
  122. public function isIndexed() {
  123. $this->getSchema();
  124. return isset($this->schema['I']);
  125. }
  126. public function isTokenized() {
  127. $this->getSchema();
  128. return isset($this->schema['T']);
  129. }
  130. public function isStored() {
  131. $this->getSchema();
  132. return isset($this->schema['S']);
  133. }
  134. public function isMultivalued() {
  135. $this->getSchema();
  136. return isset($this->schema['M']);
  137. }
  138. public function isTermVectorStored() {
  139. $this->getSchema();
  140. return isset($this->schema['V']);
  141. }
  142. public function isStoreOffsetWithTermVector() {
  143. $this->getSchema();
  144. return isset($this->schema['o']);
  145. }
  146. public function isStorePositionWithTermVector() {
  147. $this->getSchema();
  148. return isset($this->schema['p']);
  149. }
  150. public function isOmitNorms() {
  151. $this->getSchema();
  152. return isset($this->schema['O']);
  153. }
  154. public function isLazy() {
  155. $this->getSchema();
  156. return isset($this->schema['L']);
  157. }
  158. public function isBinary() {
  159. $this->getSchema();
  160. return isset($this->schema['B']);
  161. }
  162. public function isCompressed() {
  163. $this->getSchema();
  164. return isset($this->schema['C']);
  165. }
  166. public function isSortMissingFirst() {
  167. $this->getSchema();
  168. return isset($this->schema['f']);
  169. }
  170. public function isSortMissingLast() {
  171. $this->getSchema();
  172. return isset($this->schema['l']);
  173. }
  174. }