View source
<?php
namespace Drupal\acquia_contenthub;
use Drupal\acquia_contenthub\Client\ClientManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ContentHubSearch {
protected $clientManager;
protected $languageManager;
private $entityTypeManager;
public static function create(ContainerInterface $container, LanguageManagerInterface $language_manager, EntityTypeManagerInterface $entity_type_manager) {
return new static($container
->get('acquia_contenthub.client_manager'), $language_manager, $entity_type_manager);
}
public function __construct(ClientManagerInterface $client_manager, LanguageManagerInterface $language_manager, EntityTypeManagerInterface $entity_type_manager) {
$this->clientManager = $client_manager;
$this->languageManager = $language_manager;
$this->entityTypeManager = $entity_type_manager;
}
public function executeSearchQuery(array $query) {
if ($query_response = $this->clientManager
->createRequest('searchEntity', [
$query,
])) {
return $query_response['hits'];
}
return FALSE;
}
public function getFilters($search_term) {
if ($search_term) {
$items = array_map('trim', explode(',', $search_term));
$last_item = array_pop($items);
$query['query'] = [
'query_string' => [
'query' => $last_item,
'default_operator' => 'and',
],
];
$query['_source'] = TRUE;
$query['highlight'] = [
'fields' => [
'*' => new \stdClass(),
],
];
$result = $this
->executeSearchQuery($query);
return $result ? $result['hits'] : FALSE;
}
}
public function getReferenceFilters($search_term) {
if ($search_term) {
$match[] = [
'match' => [
'_all' => $search_term,
],
];
$query['query']['filtered']['query']['bool']['must'] = $match;
$query['query']['filtered']['query']['bool']['must_not']['term']['data.type'] = 'taxonomy_term';
$query['_source'] = TRUE;
$query['highlight'] = [
'fields' => [
'*' => new \stdClass(),
],
];
$result = $this
->executeSearchQuery($query);
return $result ? $result['hits'] : FALSE;
}
}
public function getElasticSearchQueryResponse(array $conditions, $asset_uuid, $asset_type, array $options = []) {
$query = [
'query' => [
'bool' => [
'must' => [],
],
],
'size' => !empty($options['count']) ? $options['count'] : 10,
'from' => !empty($options['start']) ? $options['start'] : 0,
'highlight' => [
'fields' => [
'*' => new \stdClass(),
],
],
];
foreach ($conditions as $condition) {
list($filter, $value) = explode(':', $condition);
switch ($filter) {
case 'search_term':
if (!empty($value)) {
$keywordQuery = $this
->getQueryFromString($value);
if ($keywordQuery != FALSE) {
$query['query']['bool']['must'][] = [
$keywordQuery,
];
}
}
break;
case 'entity_types':
$query['query']['bool']['must'][] = [
'terms' => [
'data.type' => explode(',', $value),
],
];
break;
case 'bundle':
$bundle_key = $this->entityTypeManager
->getDefinition($asset_type)
->getKey('bundle');
if (empty($bundle_key)) {
break;
}
$supported_languages = array_keys($this->languageManager
->getLanguages(LanguageInterface::STATE_ALL));
if (empty($supported_languages)) {
break;
}
$bundle_bool_queries = [
'bool' => [
'should' => [],
],
];
$bundles = explode(',', $value);
foreach ($bundles as $bundle) {
foreach ($supported_languages as $supported_language) {
$bundle_bool_queries['bool']['should'][] = [
'term' => [
"data.attributes.{$bundle_key}.value.{$supported_language}" => $bundle,
],
];
}
}
$query['query']['bool']['must'][] = $bundle_bool_queries;
break;
case 'tags':
$tags_bool_queries = [
'bool' => [
'should' => [],
],
];
$tags = explode(',', $value);
foreach ($tags as $tag) {
$keywordQuery = $this
->getQueryFromString($tag);
$tags_bool_queries['bool']['should'][] = $keywordQuery;
}
$query['query']['bool']['must'][] = $tags_bool_queries;
break;
case 'origins':
$query['query']['bool']['must'][] = [
'match' => [
'data.origin' => $value,
],
];
break;
case 'modified':
$date_modified['time_zone'] = '+1:00';
$dates = explode('to', $value);
$from = isset($dates[0]) ? trim($dates[0]) : '';
$to = isset($dates[1]) ? trim($dates[1]) : '';
if (!empty($from)) {
$date_modified['gte'] = $from;
}
if (!empty($to)) {
$date_modified['lte'] = $to;
}
$query['query']['bool']['must'][] = [
'range' => [
'data.modified' => $date_modified,
],
];
break;
}
}
if (!empty($options['sort']) && strtolower($options['sort']) !== 'relevance') {
$query['sort']['data.modified'] = strtolower($options['sort']);
}
if (isset($asset_uuid)) {
$query['query']['bool']['must'][] = [
'term' => [
'_id' => $asset_uuid,
],
];
}
return $this
->executeSearchQuery($query);
}
private function getQueryFromString($queryString) {
$query = [
'bool' => [
'must' => [],
],
];
$queryStringTokens = preg_split("/[^a-zA-Z\\d:]+/", $queryString);
foreach ($queryStringTokens as $token) {
if (strlen($token) != 0) {
$query['bool']['must'][] = [
'match' => [
'_all' => "*{$token}*",
],
];
}
}
return $query;
}
public function buildElasticSearchQuery(array $conditions, $asset_uuid, $asset_type) {
$result = $this
->getElasticSearchQueryResponse($conditions, $asset_uuid, $asset_type);
if ($result & !empty($result['total'])) {
return $result['total'];
}
return 0;
}
public function buildChronologicalQuery(array $options = []) {
$query['query']['match_all'] = new \stdClass();
$query['sort']['data.modified'] = 'desc';
if (!empty($options['sort']) && strtolower($options['sort']) !== 'relevance') {
$query['sort']['data.modified'] = strtolower($options['sort']);
}
$query['filter']['term']['data.type'] = 'node';
$query['size'] = !empty($options['count']) ? $options['count'] : 10;
$query['from'] = !empty($options['start']) ? $options['start'] : 0;
$result = $this
->executeSearchQuery($query);
return $result;
}
}