57 $this->config = $config;
58 $this->hooks = $hooks;
89 if (empty($query_parts) || empty(array_filter(
$fields))) {
102 if ($entity_subtype) {
108 if ($this->hooks->hasHandler(
'search:results', $search_type)) {
109 $results = $this->hooks->trigger(
'search:results', $search_type,
$options);
110 if (isset($results)) {
134 $options[
'search_type'] = $search_type;
142 $options[
'_elgg_search_service_normalize_options'] =
true;
183 $words = preg_split(
'/\s+/',
$query);
184 $words = array_map(
function ($e) {
188 $query = implode(
' ', $words);
199 $options[
'query_parts'] = array_unique(array_filter($parts));
217 'private_settings' => [],
222 $clean_field_property_types =
function ($new_fields) use ($default_fields) {
223 $property_types = array_keys($default_fields);
224 foreach ($property_types as $property_type) {
225 if (empty($new_fields[$property_type])) {
226 $new_fields[$property_type] = [];
233 $merge_fields =
function ($new_fields) use (&
$fields, $clean_field_property_types) {
234 if (empty($new_fields) || !is_array($new_fields)) {
238 $new_fields = $clean_field_property_types($new_fields);
244 $normalized_options = $this->normalizeTypeSubtypeOptions(
$options);
246 $type_subtype_pairs =
elgg_extract(
'type_subtype_pairs', $normalized_options);
247 if (!empty($type_subtype_pairs)) {
248 foreach ($type_subtype_pairs as $entity_type => $entity_subtypes) {
249 $result = $this->hooks->trigger(
'search:fields', $entity_type,
$options, $default_fields);
256 foreach ($entity_subtypes as $entity_subtype) {
257 $result = $this->hooks->trigger(
'search:fields',
"{$entity_type}:{$entity_subtype}",
$options, $default_fields);
276 foreach (
$fields as $property_type => $property_type_fields) {
277 if (empty(
$options[
'fields'][$property_type])) {
278 $options[
'fields'][$property_type] = [];
282 $allowed = array_intersect($property_type_fields, (array)
$options[
'fields'][$property_type]);
283 $options[
'fields'][$property_type] = array_values(array_unique($allowed));
300 if (is_string($sort)) {
307 if (!isset($sort[
'property'])) {
309 'property' =>
'time_created',
310 'property_type' =>
'attribute',
311 'direction' =>
'desc',
315 $clause =
new Database\Clauses\EntitySortByClause();
317 $clause->property_type =
elgg_extract(
'property_type', $sort);
318 $clause->direction =
elgg_extract(
'direction', $sort,
'asc');
347 $populate_where =
function ($where, $part) use ($partial_match) {
348 $where->values = $partial_match ?
"%{$part}%" : $part;
349 $where->comparison =
'LIKE';
351 $where->case_sensitive =
false;
356 $attribute_ands = [];
357 foreach ($query_parts as $part) {
359 $where->names = $attribute;
360 $populate_where($where, $part);
361 $attribute_ands[] = $where->prepare($qb, $alias);
363 $ors[] = $qb->
merge($attribute_ands,
'AND');
370 foreach ($query_parts as $part) {
372 $populate_where($where, $part);
373 $metadata_ands[] = $where->prepare($qb, $md_alias);
375 $ors[] = $qb->
merge($metadata_ands,
'AND');
378 if (!empty($annotations)) {
379 $annotations_ands = [];
381 foreach ($query_parts as $part) {
383 $populate_where($where, $part);
384 $annotations_ands[] = $where->prepare($qb, $an_alias);
386 $ors[] = $qb->
merge($annotations_ands,
'AND');
389 if (!empty($private_settings)) {
390 $private_settings_ands = [];
392 foreach ($query_parts as $part) {
394 $populate_where($where, $part);
395 $private_settings_ands[] = $where->prepare($qb, $ps_alias);
397 $ors[] = $qb->
merge($private_settings_ands,
'AND');
400 return $qb->
merge($ors,
'OR');
normalizeOptions(array $options=[])
Normalize options.
static getEntityTypes()
Get the core entity types.
__construct(Config $config, PluginHooksService $hooks, Database $db)
Constructor.
Builds quereis for matching entities by their attributes.
Database abstraction query builder.
Builds queries for matching annotations against their properties.
prepareSortOptions(array $options=[])
Normalizes sort options.
normalizeQuery(array $options=[])
Normalize query parts.
prepareSearchOptions(array $options=[])
Prepare ege* options.
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
search(array $options=[])
Returns search results as an array of entities, as a batch, or a count, depending on parameters given...
joinAnnotationTable($from_alias= '', $from_column= 'guid', $name=null, $join_type= 'inner', $joined_alias=null)
Join annotations table from alias and return joined table alias.
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
joinPrivateSettingsTable($from_alias= '', $from_column= 'guid', $name=null, $join_type= 'inner', $joined_alias=null)
Join private settings table from alias and return joined table alias.
merge($parts=null, $boolean= 'AND')
Merges multiple composite expressions with a boolean.
buildSearchWhereQuery(QueryBuilder $qb, $alias, $fields, $query_parts, $partial_match=true)
Builds search clause.
joinMetadataTable($from_alias= '', $from_column= 'guid', $name=null, $join_type= 'inner', $joined_alias=null)
Join metadata table from alias and return joined table alias.
Builds queries for filtering entties by their properties in private_settings table.
normalizeSearchFields(array $options=[])
Normalizes an array of search fields.