Showing
4 changed files
with
29 additions
and
70 deletions
| ... | @@ -176,68 +176,5 @@ class Entity extends Ownable | ... | @@ -176,68 +176,5 @@ class Entity extends Ownable |
| 176 | */ | 176 | */ |
| 177 | public function entityRawQuery(){return '';} | 177 | public function entityRawQuery(){return '';} |
| 178 | 178 | ||
| 179 | - /** | ||
| 180 | - * Perform a full-text search on this entity. | ||
| 181 | - * @param string[] $terms | ||
| 182 | - * @param string[] array $wheres | ||
| 183 | - * TODO - REMOVE | ||
| 184 | - * @return mixed | ||
| 185 | - */ | ||
| 186 | - public function fullTextSearchQuery($terms, $wheres = []) | ||
| 187 | - { | ||
| 188 | - $exactTerms = []; | ||
| 189 | - $fuzzyTerms = []; | ||
| 190 | - $search = static::newQuery(); | ||
| 191 | - | ||
| 192 | - foreach ($terms as $key => $term) { | ||
| 193 | - $term = htmlentities($term, ENT_QUOTES); | ||
| 194 | - $term = preg_replace('/[+\-><\(\)~*\"@]+/', ' ', $term); | ||
| 195 | - if (preg_match('/".*?"/', $term) || is_numeric($term)) { | ||
| 196 | - $term = str_replace('"', '', $term); | ||
| 197 | - $exactTerms[] = '%' . $term . '%'; | ||
| 198 | - } else { | ||
| 199 | - $term = '' . $term . '*'; | ||
| 200 | - if ($term !== '*') $fuzzyTerms[] = $term; | ||
| 201 | - } | ||
| 202 | - } | ||
| 203 | - | ||
| 204 | - $isFuzzy = count($exactTerms) === 0 && count($fuzzyTerms) > 0; | ||
| 205 | - $fieldsToSearch = ['name', $this->textField]; | ||
| 206 | - | ||
| 207 | - // Perform fulltext search if relevant terms exist. | ||
| 208 | - if ($isFuzzy) { | ||
| 209 | - $termString = implode(' ', $fuzzyTerms); | ||
| 210 | - | ||
| 211 | - $search = $search->selectRaw('*, MATCH(name) AGAINST(? IN BOOLEAN MODE) AS title_relevance', [$termString]); | ||
| 212 | - $search = $search->whereRaw('MATCH(' . implode(',', $fieldsToSearch ). ') AGAINST(? IN BOOLEAN MODE)', [$termString]); | ||
| 213 | - } | ||
| 214 | - | ||
| 215 | - // Ensure at least one exact term matches if in search | ||
| 216 | - if (count($exactTerms) > 0) { | ||
| 217 | - $search = $search->where(function ($query) use ($exactTerms, $fieldsToSearch) { | ||
| 218 | - foreach ($exactTerms as $exactTerm) { | ||
| 219 | - foreach ($fieldsToSearch as $field) { | ||
| 220 | - $query->orWhere($field, 'like', $exactTerm); | ||
| 221 | - } | ||
| 222 | - } | ||
| 223 | - }); | ||
| 224 | - } | ||
| 225 | - | ||
| 226 | - $orderBy = $isFuzzy ? 'title_relevance' : 'updated_at'; | ||
| 227 | - | ||
| 228 | - // Add additional where terms | ||
| 229 | - foreach ($wheres as $whereTerm) { | ||
| 230 | - $search->where($whereTerm[0], $whereTerm[1], $whereTerm[2]); | ||
| 231 | - } | ||
| 232 | - | ||
| 233 | - // Load in relations | ||
| 234 | - if ($this->isA('page')) { | ||
| 235 | - $search = $search->with('book', 'chapter', 'createdBy', 'updatedBy'); | ||
| 236 | - } else if ($this->isA('chapter')) { | ||
| 237 | - $search = $search->with('book'); | ||
| 238 | - } | ||
| 239 | - | ||
| 240 | - return $search->orderBy($orderBy, 'desc'); | ||
| 241 | - } | ||
| 242 | 179 | ||
| 243 | } | 180 | } | ... | ... |
| ... | @@ -12,9 +12,10 @@ class AddSearchIndexes extends Migration | ... | @@ -12,9 +12,10 @@ class AddSearchIndexes extends Migration |
| 12 | */ | 12 | */ |
| 13 | public function up() | 13 | public function up() |
| 14 | { | 14 | { |
| 15 | - DB::statement('ALTER TABLE pages ADD FULLTEXT search(name, text)'); | 15 | + $prefix = DB::getTablePrefix(); |
| 16 | - DB::statement('ALTER TABLE books ADD FULLTEXT search(name, description)'); | 16 | + DB::statement("ALTER TABLE {$prefix}pages ADD FULLTEXT search(name, text)"); |
| 17 | - DB::statement('ALTER TABLE chapters ADD FULLTEXT search(name, description)'); | 17 | + DB::statement("ALTER TABLE {$prefix}books ADD FULLTEXT search(name, description)"); |
| 18 | + DB::statement("ALTER TABLE {$prefix}chapters ADD FULLTEXT search(name, description)"); | ||
| 18 | } | 19 | } |
| 19 | 20 | ||
| 20 | /** | 21 | /** | ... | ... |
| ... | @@ -12,9 +12,10 @@ class FulltextWeighting extends Migration | ... | @@ -12,9 +12,10 @@ class FulltextWeighting extends Migration |
| 12 | */ | 12 | */ |
| 13 | public function up() | 13 | public function up() |
| 14 | { | 14 | { |
| 15 | - DB::statement('ALTER TABLE pages ADD FULLTEXT name_search(name)'); | 15 | + $prefix = DB::getTablePrefix(); |
| 16 | - DB::statement('ALTER TABLE books ADD FULLTEXT name_search(name)'); | 16 | + DB::statement("ALTER TABLE {$prefix}pages ADD FULLTEXT name_search(name)"); |
| 17 | - DB::statement('ALTER TABLE chapters ADD FULLTEXT name_search(name)'); | 17 | + DB::statement("ALTER TABLE {$prefix}books ADD FULLTEXT name_search(name)"); |
| 18 | + DB::statement("ALTER TABLE {$prefix}chapters ADD FULLTEXT name_search(name)"); | ||
| 18 | } | 19 | } |
| 19 | 20 | ||
| 20 | /** | 21 | /** | ... | ... |
| ... | @@ -26,7 +26,19 @@ class CreateSearchIndexTable extends Migration | ... | @@ -26,7 +26,19 @@ class CreateSearchIndexTable extends Migration |
| 26 | $table->index('score'); | 26 | $table->index('score'); |
| 27 | }); | 27 | }); |
| 28 | 28 | ||
| 29 | - // TODO - Drop old fulltext indexes | 29 | + // Drop search indexes |
| 30 | + Schema::table('pages', function(Blueprint $table) { | ||
| 31 | + $table->dropIndex('search'); | ||
| 32 | + $table->dropIndex('name_search'); | ||
| 33 | + }); | ||
| 34 | + Schema::table('books', function(Blueprint $table) { | ||
| 35 | + $table->dropIndex('search'); | ||
| 36 | + $table->dropIndex('name_search'); | ||
| 37 | + }); | ||
| 38 | + Schema::table('chapters', function(Blueprint $table) { | ||
| 39 | + $table->dropIndex('search'); | ||
| 40 | + $table->dropIndex('name_search'); | ||
| 41 | + }); | ||
| 30 | 42 | ||
| 31 | app(\BookStack\Services\SearchService::class)->indexAllEntities(); | 43 | app(\BookStack\Services\SearchService::class)->indexAllEntities(); |
| 32 | } | 44 | } |
| ... | @@ -38,6 +50,14 @@ class CreateSearchIndexTable extends Migration | ... | @@ -38,6 +50,14 @@ class CreateSearchIndexTable extends Migration |
| 38 | */ | 50 | */ |
| 39 | public function down() | 51 | public function down() |
| 40 | { | 52 | { |
| 53 | + $prefix = DB::getTablePrefix(); | ||
| 54 | + DB::statement("ALTER TABLE {$prefix}pages ADD FULLTEXT search(name, text)"); | ||
| 55 | + DB::statement("ALTER TABLE {$prefix}books ADD FULLTEXT search(name, description)"); | ||
| 56 | + DB::statement("ALTER TABLE {$prefix}chapters ADD FULLTEXT search(name, description)"); | ||
| 57 | + DB::statement("ALTER TABLE {$prefix}pages ADD FULLTEXT name_search(name)"); | ||
| 58 | + DB::statement("ALTER TABLE {$prefix}books ADD FULLTEXT name_search(name)"); | ||
| 59 | + DB::statement("ALTER TABLE {$prefix}chapters ADD FULLTEXT name_search(name)"); | ||
| 60 | + | ||
| 41 | Schema::dropIfExists('search_terms'); | 61 | Schema::dropIfExists('search_terms'); |
| 42 | } | 62 | } |
| 43 | } | 63 | } | ... | ... |
-
Please register or sign in to post a comment