mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-10-01 01:36:00 -04:00
Added search name weighting. Closes #27.
This commit is contained in:
parent
c32d70abc4
commit
46c905df8a
@ -134,20 +134,20 @@ abstract class Entity extends Model
|
|||||||
$termString .= $term . '* ';
|
$termString .= $term . '* ';
|
||||||
}
|
}
|
||||||
$fields = implode(',', $fieldsToSearch);
|
$fields = implode(',', $fieldsToSearch);
|
||||||
$search = static::whereRaw('MATCH(' . $fields . ') AGAINST(? IN BOOLEAN MODE)', [$termString]);
|
$termStringEscaped = \DB::connection()->getPdo()->quote($termString);
|
||||||
|
$search = static::addSelect(\DB::raw('*, MATCH(name) AGAINST('.$termStringEscaped.' IN BOOLEAN MODE) AS title_relevance'));
|
||||||
|
$search = $search->whereRaw('MATCH(' . $fields . ') AGAINST(? IN BOOLEAN MODE)', [$termString]);
|
||||||
|
|
||||||
|
// Add additional where terms
|
||||||
foreach ($wheres as $whereTerm) {
|
foreach ($wheres as $whereTerm) {
|
||||||
$search->where($whereTerm[0], $whereTerm[1], $whereTerm[2]);
|
$search->where($whereTerm[0], $whereTerm[1], $whereTerm[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!static::isA('book')) {
|
// Load in relations
|
||||||
$search = $search->with('book');
|
if (!static::isA('book')) $search = $search->with('book');
|
||||||
}
|
if (static::isA('page')) $search = $search->with('chapter');
|
||||||
|
|
||||||
if (static::isA('page')) {
|
return $search->orderBy('title_relevance', 'desc')->get();
|
||||||
$search = $search->with('chapter');
|
|
||||||
}
|
|
||||||
|
|
||||||
return $search->get();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
37
database/migrations/2015_12_05_145049_fulltext_weighting.php
Normal file
37
database/migrations/2015_12_05_145049_fulltext_weighting.php
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
|
||||||
|
class FulltextWeighting extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
DB::statement('ALTER TABLE pages ADD FULLTEXT name_search(name)');
|
||||||
|
DB::statement('ALTER TABLE books ADD FULLTEXT name_search(name)');
|
||||||
|
DB::statement('ALTER TABLE chapters ADD FULLTEXT name_search(name)');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::table('pages', function(Blueprint $table) {
|
||||||
|
$table->dropIndex('name_search');
|
||||||
|
});
|
||||||
|
Schema::table('books', function(Blueprint $table) {
|
||||||
|
$table->dropIndex('name_search');
|
||||||
|
});
|
||||||
|
Schema::table('chapters', function(Blueprint $table) {
|
||||||
|
$table->dropIndex('name_search');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user