Showing
2 changed files
with
46 additions
and
9 deletions
| ... | @@ -134,20 +134,20 @@ abstract class Entity extends Model | ... | @@ -134,20 +134,20 @@ abstract class Entity extends Model |
| 134 | $termString .= $term . '* '; | 134 | $termString .= $term . '* '; |
| 135 | } | 135 | } |
| 136 | $fields = implode(',', $fieldsToSearch); | 136 | $fields = implode(',', $fieldsToSearch); |
| 137 | - $search = static::whereRaw('MATCH(' . $fields . ') AGAINST(? IN BOOLEAN MODE)', [$termString]); | 137 | + $termStringEscaped = \DB::connection()->getPdo()->quote($termString); |
| 138 | + $search = static::addSelect(\DB::raw('*, MATCH(name) AGAINST('.$termStringEscaped.' IN BOOLEAN MODE) AS title_relevance')); | ||
| 139 | + $search = $search->whereRaw('MATCH(' . $fields . ') AGAINST(? IN BOOLEAN MODE)', [$termString]); | ||
| 140 | + | ||
| 141 | + // Add additional where terms | ||
| 138 | foreach ($wheres as $whereTerm) { | 142 | foreach ($wheres as $whereTerm) { |
| 139 | $search->where($whereTerm[0], $whereTerm[1], $whereTerm[2]); | 143 | $search->where($whereTerm[0], $whereTerm[1], $whereTerm[2]); |
| 140 | } | 144 | } |
| 141 | 145 | ||
| 142 | - if (!static::isA('book')) { | 146 | + // Load in relations |
| 143 | - $search = $search->with('book'); | 147 | + if (!static::isA('book')) $search = $search->with('book'); |
| 144 | - } | 148 | + if (static::isA('page')) $search = $search->with('chapter'); |
| 145 | - | ||
| 146 | - if (static::isA('page')) { | ||
| 147 | - $search = $search->with('chapter'); | ||
| 148 | - } | ||
| 149 | 149 | ||
| 150 | - return $search->get(); | 150 | + return $search->orderBy('title_relevance', 'desc')->get(); |
| 151 | } | 151 | } |
| 152 | 152 | ||
| 153 | /** | 153 | /** | ... | ... |
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +use Illuminate\Database\Schema\Blueprint; | ||
| 4 | +use Illuminate\Database\Migrations\Migration; | ||
| 5 | + | ||
| 6 | +class FulltextWeighting extends Migration | ||
| 7 | +{ | ||
| 8 | + /** | ||
| 9 | + * Run the migrations. | ||
| 10 | + * | ||
| 11 | + * @return void | ||
| 12 | + */ | ||
| 13 | + public function up() | ||
| 14 | + { | ||
| 15 | + DB::statement('ALTER TABLE pages ADD FULLTEXT name_search(name)'); | ||
| 16 | + DB::statement('ALTER TABLE books ADD FULLTEXT name_search(name)'); | ||
| 17 | + DB::statement('ALTER TABLE chapters ADD FULLTEXT name_search(name)'); | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + /** | ||
| 21 | + * Reverse the migrations. | ||
| 22 | + * | ||
| 23 | + * @return void | ||
| 24 | + */ | ||
| 25 | + public function down() | ||
| 26 | + { | ||
| 27 | + Schema::table('pages', function(Blueprint $table) { | ||
| 28 | + $table->dropIndex('name_search'); | ||
| 29 | + }); | ||
| 30 | + Schema::table('books', function(Blueprint $table) { | ||
| 31 | + $table->dropIndex('name_search'); | ||
| 32 | + }); | ||
| 33 | + Schema::table('chapters', function(Blueprint $table) { | ||
| 34 | + $table->dropIndex('name_search'); | ||
| 35 | + }); | ||
| 36 | + } | ||
| 37 | +} |
-
Please register or sign in to post a comment