@@ -73,6 +73,7 @@ public function updateSearchIndex(): void
7373
7474 $ pepper = (string ) config ('encrypted-search.search_pepper ' , '' );
7575 $ max = (int ) config ('encrypted-search.max_prefix_depth ' , 6 );
76+ $ min = (int ) config ('encrypted-search.min_prefix_length ' , 1 );
7677 $ useElastic = config ('encrypted-search.elasticsearch.enabled ' , false );
7778
7879 $ rows = [];
@@ -108,7 +109,7 @@ public function updateSearchIndex(): void
108109
109110 // Generate prefix-based tokens
110111 if (!empty ($ modes ['prefix ' ])) {
111- foreach (Tokens::prefixes ($ normalized , $ max , $ pepper ) as $ token ) {
112+ foreach (Tokens::prefixes ($ normalized , $ max , $ pepper, $ min ) as $ token ) {
112113 $ rows [] = [
113114 'model_type ' => static ::class,
114115 'model_id ' => $ this ->getKey (),
@@ -275,18 +276,30 @@ public function scopeEncryptedExact(Builder $query, string $field, string $term)
275276 public function scopeEncryptedPrefix (Builder $ query , string $ field , string $ term ): Builder
276277 {
277278 $ pepper = (string ) config ('encrypted-search.search_pepper ' , '' );
279+ $ minLength = (int ) config ('encrypted-search.min_prefix_length ' , 1 );
278280 $ normalized = Normalizer::normalize ($ term );
279281
280282 if (!$ normalized ) {
281283 return $ query ->whereRaw ('1=0 ' );
282284 }
283285
286+ // Check if search term meets minimum length requirement
287+ if (mb_strlen ($ normalized , 'UTF-8 ' ) < $ minLength ) {
288+ return $ query ->whereRaw ('1=0 ' );
289+ }
290+
284291 $ tokens = Tokens::prefixes (
285292 $ normalized ,
286293 (int ) config ('encrypted-search.max_prefix_depth ' , 6 ),
287- $ pepper
294+ $ pepper ,
295+ $ minLength
288296 );
289297
298+ // If no tokens generated (term too short), return no results
299+ if (empty ($ tokens )) {
300+ return $ query ->whereRaw ('1=0 ' );
301+ }
302+
290303 // Check if Elasticsearch is enabled
291304 if (config ('encrypted-search.elasticsearch.enabled ' , false )) {
292305 $ modelIds = $ this ->searchElasticsearch ($ field , $ tokens , 'prefix ' );
0 commit comments