Fixed Changing behaviour of search requires more work than necessary

Kirby

Well-known member
Affected version
2.1.7
\XF\Pub\Controller\Search::actionSearch() requires that a keyword or username is given.

Changing this behaviour (for example to allow searching for threads by prefix only) requires a fair amount of code duplication.

If this part was changed from
PHP:
if ($query->getErrors())
{
    return $this->error($query->getErrors());
}
if (!strlen($query->getKeywords()) && !$query->getUserIds())
{
    return $this->error(\XF::phrase('please_specify_search_query_or_name_of_member'));
}

to smth. like
PHP:
if (!strlen($query->getKeywords()) && !$query->getUserIds())
{
    $query->error('no_keyword_or_member', \XF::phrase('please_specify_search_query_or_name_of_member'));
}
if ($query->getErrors())
{
    return $this->error($query->getErrors());
}

it would become a lot easier (and IMHO cleaner, might even make sense to move that check to prepareSearchQuery()).
 
My preference would be to change !strlen($query->getKeywords()) && !$query->getUserIds() to $query->isEmptySearch() with the new function Query::isEmptySearch.

This would allow sane extending of what is an "empty" search.
 
Thank you for reporting this issue, it has now been resolved. We are aiming to include any changes that have been made in a future XF release (2.1.8).

Change log:
Implement search source method to determine if a query is empty
There may be a delay before changes are rolled out to the XenForo Community.
 
Just to expand on that message a bit, this is now implemented via \XF\Serach\Source\AbstractSource::isQueryEmpty, so it can be overriddeen on a source-by-source basis. (As, for example, MySQL FT has really terrible performance with a lot of results, which is what the limit tries to help with. Elasticsearch may not need to be as stringent.) This is only checked in the same place as the old code so it doesn't really enforce any greater limits but it's much more direct to override.
 
Top Bottom