query_type_term.inc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * @file
  4. * Term query type plugin for the Apache Solr adapter.
  5. */
  6. /**
  7. * Plugin for "term" query types.
  8. */
  9. class SearchApiFacetapiTerm extends FacetapiQueryType implements FacetapiQueryTypeInterface {
  10. /**
  11. * Returns the query type associated with the plugin.
  12. *
  13. * @return string
  14. * The query type.
  15. */
  16. static public function getType() {
  17. return 'term';
  18. }
  19. /**
  20. * Adds the filter to the query object.
  21. *
  22. * @param SearchApiQueryInterface $query
  23. * An object containing the query in the backend's native API.
  24. */
  25. public function execute($query) {
  26. // Return terms for this facet.
  27. $this->adapter->addFacet($this->facet, $query);
  28. $settings = $this->adapter->getFacet($this->facet)->getSettings()->settings;
  29. // First check if the facet is enabled for this search.
  30. $default_true = isset($settings['default_true']) ? $settings['default_true'] : TRUE;
  31. $facet_search_ids = isset($settings['facet_search_ids']) ? $settings['facet_search_ids'] : array();
  32. if ($default_true != empty($facet_search_ids[$query->getOption('search id')])) {
  33. // Facet is not enabled for this search ID.
  34. return;
  35. }
  36. // Retrieve the active facet filters.
  37. $active = $this->adapter->getActiveItems($this->facet);
  38. if (empty($active)) {
  39. return;
  40. }
  41. // Create the facet filter, and add a tag to it so that it can be easily
  42. // identified down the line by services when they need to exclude facets.
  43. $operator = $settings['operator'];
  44. if ($operator == FACETAPI_OPERATOR_AND) {
  45. $conjunction = 'AND';
  46. }
  47. elseif ($operator == FACETAPI_OPERATOR_OR) {
  48. $conjunction = 'OR';
  49. }
  50. else {
  51. throw new SearchApiException(t('Unknown facet operator %operator.', array('%operator' => $operator)));
  52. }
  53. $tags = array('facet:' . $this->facet['field']);
  54. $facet_filter = $query->createFilter($conjunction, $tags);
  55. foreach ($active as $filter => $filter_array) {
  56. $field = $this->facet['field'];
  57. $this->addFacetFilter($facet_filter, $field, $filter);
  58. }
  59. // Now add the filter to the query.
  60. $query->filter($facet_filter);
  61. }
  62. /**
  63. * Helper method for setting a facet filter on a query or query filter object.
  64. */
  65. protected function addFacetFilter($query_filter, $field, $filter) {
  66. // Test if this filter should be negated.
  67. $settings = $this->adapter->getFacet($this->facet)->getSettings();
  68. $exclude = !empty($settings->settings['exclude']);
  69. // Integer (or other nun-string) filters might mess up some of the following
  70. // comparison expressions.
  71. $filter = (string) $filter;
  72. if ($filter == '!') {
  73. $query_filter->condition($field, NULL, $exclude ? '<>' : '=');
  74. }
  75. elseif ($filter[0] == '[' && $filter[strlen($filter) - 1] == ']' && ($pos = strpos($filter, ' TO '))) {
  76. $lower = trim(substr($filter, 1, $pos));
  77. $upper = trim(substr($filter, $pos + 4, -1));
  78. if ($lower == '*' && $upper == '*') {
  79. $query_filter->condition($field, NULL, $exclude ? '=' : '<>');
  80. }
  81. elseif (!$exclude) {
  82. if ($lower != '*') {
  83. // Iff we have a range with two finite boundaries, we set two
  84. // conditions (larger than the lower bound and less than the upper
  85. // bound) and therefore have to make sure that we have an AND
  86. // conjunction for those.
  87. if ($upper != '*' && !($query_filter instanceof SearchApiQueryInterface || $query_filter->getConjunction() === 'AND')) {
  88. $original_query_filter = $query_filter;
  89. $query_filter = new SearchApiQueryFilter('AND');
  90. }
  91. $query_filter->condition($field, $lower, '>=');
  92. }
  93. if ($upper != '*') {
  94. $query_filter->condition($field, $upper, '<=');
  95. }
  96. }
  97. else {
  98. // Same as above, but with inverted logic.
  99. if ($lower != '*') {
  100. if ($upper != '*' && ($query_filter instanceof SearchApiQueryInterface || $query_filter->getConjunction() === 'AND')) {
  101. $original_query_filter = $query_filter;
  102. $query_filter = new SearchApiQueryFilter('OR');
  103. }
  104. $query_filter->condition($field, $lower, '<');
  105. }
  106. if ($upper != '*') {
  107. $query_filter->condition($field, $upper, '>');
  108. }
  109. }
  110. }
  111. else {
  112. $query_filter->condition($field, $filter, $exclude ? '<>' : '=');
  113. }
  114. if (isset($original_query_filter)) {
  115. $original_query_filter->filter($query_filter);
  116. }
  117. }
  118. /**
  119. * Initializes the facet's build array.
  120. *
  121. * @return array
  122. * The initialized render array.
  123. */
  124. public function build() {
  125. $facet = $this->adapter->getFacet($this->facet);
  126. // The current search per facet is stored in a static variable (during
  127. // initActiveFilters) so that we can retrieve it here and get the correct
  128. // current search for this facet.
  129. $search_ids = drupal_static('search_api_facetapi_active_facets', array());
  130. if (empty($search_ids[$facet['name']]) || !search_api_current_search($search_ids[$facet['name']])) {
  131. return array();
  132. }
  133. $search_id = $search_ids[$facet['name']];
  134. $search = search_api_current_search($search_id);
  135. $build = array();
  136. $results = $search[1];
  137. if (isset($results['search_api_facets']) && isset($results['search_api_facets'][$this->facet['name']])) {
  138. $values = $results['search_api_facets'][$this->facet['name']];
  139. foreach ($values as $value) {
  140. $filter = $value['filter'];
  141. // As Facet API isn't really suited for our native facet filter
  142. // representations, convert the format here. (The missing facet can
  143. // stay the same.)
  144. if ($filter[0] == '"') {
  145. $filter = substr($filter, 1, -1);
  146. }
  147. elseif ($filter != '!') {
  148. // This is a range filter.
  149. $filter = substr($filter, 1, -1);
  150. $pos = strpos($filter, ' ');
  151. if ($pos !== FALSE) {
  152. $filter = '[' . substr($filter, 0, $pos) . ' TO ' . substr($filter, $pos + 1) . ']';
  153. }
  154. }
  155. $build[$filter] = array(
  156. '#count' => $value['count'],
  157. );
  158. }
  159. }
  160. return $build;
  161. }
  162. }