public function ContentHubSearch::getElasticSearchQueryResponse in Acquia Content Hub 8
Builds Search query for given search terms..
Parameters
array $conditions: The conditions string that needs to be parsed for querying elasticsearch.
string $asset_uuid: The Asset Uuid that came through a webhook.
string $asset_type: The Asset Type (entity_type_id).
array $options: An associative array of options for this query, including:
- count: number of items per page.
- start: defines the offset to start from.
Return value
int|mixed Returns query response.
1 call to ContentHubSearch::getElasticSearchQueryResponse()
- ContentHubSearch::buildElasticSearchQuery in src/
ContentHubSearch.php - Executes and ES query for a given asset uuid.
File
- src/
ContentHubSearch.php, line 156
Class
- ContentHubSearch
- Perform queries to the Content Hub "_search" endpoint [Elasticsearch].
Namespace
Drupal\acquia_contenthubCode
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(),
],
],
];
// Iterating over each condition.
foreach ($conditions as $condition) {
list($filter, $value) = explode(':', $condition);
// Tweak ES query for each filter condition.
switch ($filter) {
// For Search Term (Keyword).
case 'search_term':
if (!empty($value)) {
$keywordQuery = $this
->getQueryFromString($value);
if ($keywordQuery != FALSE) {
$query['query']['bool']['must'][] = [
$keywordQuery,
];
}
}
break;
// For entity types.
case 'entity_types':
$query['query']['bool']['must'][] = [
'terms' => [
'data.type' => explode(',', $value),
],
];
break;
// For bundles.
case 'bundle':
// Obtaining bundle_key for this bundle.
$bundle_key = $this->entityTypeManager
->getDefinition($asset_type)
->getKey('bundle');
if (empty($bundle_key)) {
break;
}
// Test all supported languages.
$supported_languages = array_keys($this->languageManager
->getLanguages(LanguageInterface::STATE_ALL));
if (empty($supported_languages)) {
break;
}
// Create and add bundle's bool queries.
$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;
// For Tags.
case 'tags':
// Create and add bundle's bool queries.
$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;
// For Origin / Source.
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);
}