Open Data Catalog v2.0.0
Search.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6 use Drupal\Core\Entity\EntityTypeManager;
8 use Drupal\search_api\Query\ResultSet;
9 use Drupal\search_api\Utility\QueryHelperInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11 
19 class Search implements ContainerInjectionInterface {
22  // @todo Use real classes to get proper encapsulation.
24 
25  const EVENT_SEARCH = 'dkan_metastore_search_search';
26  const EVENT_SEARCH_PARAMS = 'dkan_metastore_search_search_params';
27 
28  // @todo this constant is used by QueryBuilder, maybe we need a class.
29  const EVENT_SEARCH_QUERY_BUILDER_CONDITION = 'dkan_metastore_search_query_builder_condition';
30 
36  private $index;
37 
43  private $metastoreService;
44 
50  private $entityTypeManager;
51 
57  private $queryHelper;
58 
69  public function __construct(
70  Metastore $metastoreService,
71  EntityTypeManager $entityTypeManager,
72  QueryHelperInterface $queryHelper
73  ) {
74  $this->metastoreService = $metastoreService;
75  $this->entityTypeManager = $entityTypeManager;
76  $this->queryHelper = $queryHelper;
77 
78  $this->setSearchIndex('dkan');
79  }
80 
86  public static function create(ContainerInterface $container) {
87  return new static(
88  $container->get('dkan.metastore.service'),
89  $container->get('entity_type.manager'),
90  $container->get('search_api.query_helper')
91  );
92  }
93 
103  public function search(array $params = []) {
104  $params = $this->dispatchEvent(self::EVENT_SEARCH_PARAMS, $params);
105  $query = $this->getQuery($params, $this->index, $this->queryHelper)[0];
106  $result = $query->execute();
107 
108  $count = $result->getResultCount();
109  $data = $this->getData($result);
110 
111  $data = $this->dispatchEvent(self::EVENT_SEARCH, $data);
112 
113  return (object) [
114  'total' => $count,
115  'results' => $data,
116  ];
117  }
118 
128  public function facets(array $params = []) : array {
129  $params['page-size'] = PHP_INT_MAX;
130  $params['page'] = 1;
131 
132  list($query, $activeConditions) = $this->getQuery($params, $this->index, $this->queryHelper);
133 
134  if ($activeConditions) {
135  $facets = $this->getFacetsFromContent($params, $query);
136  }
137  else {
138  $facets = $this->getFacetsFromIndex($params, $this->index, $query);
139  }
140 
141  return $facets;
142  }
143 
153  private function getData(ResultSet $result) {
154  $metastore = $this->metastoreService;
155 
156  return array_filter(array_map(
157  function ($item) use ($metastore) {
158  $id = $item->getId();
159  $id = str_replace('dkan_dataset/', '', $id);
160  try {
161  return json_decode($metastore->get('dataset', $id));
162  }
163  catch (\Exception $e) {
164  return NULL;
165  }
166  },
167  $result->getResultItems()
168  ));
169  }
170 
177  private function setSearchIndex(string $id) {
178  $storage = $this->entityTypeManager
179  ->getStorage('search_api_index');
180  $this->index = $storage->load($id);
181 
182  if (!$this->index) {
183  throw new \Exception("An index named [{$id}] does not exist.");
184  }
185  }
186 
193  public function getSearchIndex() {
194  if (isset($this->index)) {
195  return $this->index;
196  }
197  return NULL;
198  }
199 
200 }
Drupal\metastore_search\FacetsFromContentTrait
trait FacetsFromContentTrait
Definition: FacetsFromContentTrait.php:13
Drupal\metastore_search\Search\create
static create(ContainerInterface $container)
Definition: Search.php:86
Drupal\metastore\Service
Definition: Uuid5.php:5
Drupal\metastore_search\Search\search
search(array $params=[])
Definition: Search.php:103
Drupal\metastore_search\Search\EVENT_SEARCH_QUERY_BUILDER_CONDITION
const EVENT_SEARCH_QUERY_BUILDER_CONDITION
Definition: Search.php:29
Drupal\metastore_search\Search
Definition: Search.php:19
Drupal\metastore_search\Search\__construct
__construct(Metastore $metastoreService, EntityTypeManager $entityTypeManager, QueryHelperInterface $queryHelper)
Definition: Search.php:69
Drupal\metastore_search\FacetsFromIndexTrait
trait FacetsFromIndexTrait
Definition: FacetsFromIndexTrait.php:13
Drupal\metastore_search
Drupal\metastore_search\QueryBuilderTrait
trait QueryBuilderTrait
Definition: QueryBuilderTrait.php:16
Drupal\metastore_search\Search\facets
facets(array $params=[])
Definition: Search.php:128
Drupal\metastore_search\Search\EVENT_SEARCH
const EVENT_SEARCH
Definition: Search.php:25
Drupal\metastore_search\Search\getSearchIndex
getSearchIndex()
Definition: Search.php:193
Drupal\metastore_search\Search\EVENT_SEARCH_PARAMS
const EVENT_SEARCH_PARAMS
Definition: Search.php:26