View source
<?php
namespace Drupal\acquia_contenthub_subscriber;
use Drupal\Core\Language\LanguageInterface;
class ESQueryGenerator implements ESQueryGeneratorInterface {
public function getElasticSearchQuery(ContentHubFilterInterface $filter, $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(),
],
],
];
$supported_entity_types_bundles = $this
->getSubscriberSupportedEntityBundles();
foreach ($filter
->getConditions() as $condition) {
list($filter, $value) = explode(':', $condition);
switch ($filter) {
case 'entity_types':
$query['query']['bool']['must'][] = [
'terms' => [
'data.type' => explode(',', $value),
],
];
break;
case 'bundle':
foreach ($supported_entity_types_bundles as $entity_type => $bundles) {
foreach (explode(',', $value) as $bundle_value) {
if (in_array($bundle_value, $bundles['bundles'], TRUE)) {
$bundle_key = $bundles['bundle_key'];
break;
}
}
}
if (empty($bundle_key)) {
break;
}
$supported_languages = array_keys(\Drupal::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 'search_term':
if (!empty($value)) {
$keywordQuery = $this
->getQueryFromString($value);
if ($keywordQuery !== FALSE) {
$query['query']['bool']['must'][] = [
$keywordQuery,
];
}
}
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 $query;
}
protected function getSubscriberSupportedEntityBundles() {
$entity_manager = \Drupal::getContainer()
->get('acquia_contenthub.entity_manager');
$entity_type_manager = \Drupal::entityTypeManager();
$entity_types = $entity_manager
->getAllowedEntityTypes();
$entity_types_and_bundles = [];
foreach ($entity_types as $entity_type => $bundles) {
if ($entity_type === 'taxonomy_term') {
$bundle_key = 'vocabulary';
}
else {
$bundle_key = $entity_type_manager
->getDefinition($entity_type)
->getKey('bundle');
}
$entity_types_and_bundles[$entity_type] = [
'bundle_key' => $bundle_key,
'bundles' => array_keys($bundles),
];
}
return $entity_types_and_bundles;
}
protected 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;
}
}