fixed search results for short keywords

fixes #1737
This commit is contained in:
Carsten Brandt 2016-05-19 20:14:19 +02:00
parent d8fc0d251f
commit d7f72e1182
4 changed files with 18 additions and 11 deletions

View File

@ -52,5 +52,3 @@ class Searchable extends Behavior
}
}
?>

View File

@ -155,13 +155,19 @@ class ZendLuceneSearch extends Search
\ZendSearch\Lucene\Search\Query\Wildcard::setMinPrefixLength(0);
$query = new \ZendSearch\Lucene\Search\Query\Boolean();
$emptyQuery = true;
foreach (explode(" ", $keyword) as $k) {
// Require at least 3 non-wildcard characters
if (strlen($k) < $this->minQueryTokenLength) {
return null;
// Require a minimum of non-wildcard characters
if (mb_strlen($k, Yii::$app->charset) >= $this->minQueryTokenLength) {
$term = new \ZendSearch\Lucene\Index\Term("*$k*");
$query->addSubquery(new \ZendSearch\Lucene\Search\Query\Wildcard($term), true);
$emptyQuery = false;
}
$term = new \ZendSearch\Lucene\Index\Term("*" . $k . "*");
$query->addSubquery(new \ZendSearch\Lucene\Search\Query\Wildcard($term), true);
}
// if no keywords or only too short keywords are given, the result is empty.
if ($emptyQuery) {
return null;
}
// Add model filter

View File

@ -24,5 +24,3 @@ interface Searchable
public function getSearchAttributes();
}
?>

View File

@ -8,6 +8,9 @@
namespace humhub\modules\search\libs;
use humhub\components\ActiveRecord;
use Yii;
/**
* SearchResultSet
*
@ -36,22 +39,24 @@ class SearchResultSet
*/
public $pageSize;
/**
* Returns active record instances of the search results
*
* @return \yii\db\ActiveRecord
* @return ActiveRecord[]
*/
public function getResultInstances()
{
$instances = array();
foreach ($this->results as $result) {
/** @var $modelClass ActiveRecord */
$modelClass = $result->model;
$instance = $modelClass::findOne(['id' => $result->pk]);
if ($instance !== null) {
$instances[] = $instance;
} else {
\Yii::error('Could not load search result ' . $result->model . " - " . $result->pk);
Yii::error('Could not load search result ' . $result->model . " - " . $result->pk);
}
}