diff --git a/src/Traits/HasEncryptedSearchIndex.php b/src/Traits/HasEncryptedSearchIndex.php index 959395f..6c8141c 100644 --- a/src/Traits/HasEncryptedSearchIndex.php +++ b/src/Traits/HasEncryptedSearchIndex.php @@ -349,7 +349,8 @@ public function scopeEncryptedPrefix(Builder $query, string $field, string $term return $query->whereIn($this->getQualifiedKeyName(), $modelIds); } - // Fallback to database + // Fallback to database with relevance sorting + // Sort by field length (shorter matches = more relevant) return $query->whereIn($this->getQualifiedKeyName(), function ($sub) use ($field, $tokens) { $sub->select('model_id') ->from('encrypted_search_index') @@ -357,7 +358,7 @@ public function scopeEncryptedPrefix(Builder $query, string $field, string $term ->where('field', $field) ->where('type', 'prefix') ->whereIn('token', $tokens); - }); + })->orderByRaw("LENGTH({$field}) ASC"); } /** @@ -408,6 +409,8 @@ public function scopeEncryptedPrefixMulti(Builder $query, array $fields, string } // Fallback to database - use OR logic for multiple fields + // Note: Multi-field searches don't have relevance sorting due to database compatibility + // Use single-field searches for relevance-sorted results return $query->whereIn($this->getQualifiedKeyName(), function ($sub) use ($fields, $tokens) { $sub->select('model_id') ->from('encrypted_search_index')