@@ -168,50 +168,47 @@ function updateResults(
168
168
// when the `searchText` is empty.
169
169
let res = searchText ? state . index . search ( `*${ searchText } *` ) : [ ] ;
170
170
171
- if ( searchConfig . boosts != undefined ) {
172
- for ( let i = 0 ; i < res . length ; i ++ ) {
173
- const item = res [ i ] ;
174
- const row = state . data . rows [ Number ( item . ref ) ] ;
175
- let boost = 1 ;
176
-
177
- // boost by exact match on name
178
- if (
179
- searchConfig . boosts . exactMatch &&
180
- row . name . toLowerCase ( ) === searchText . toLowerCase ( )
181
- ) {
182
- boost *= searchConfig . boosts . exactMatch ;
183
- }
171
+ for ( let i = 0 ; i < res . length ; i ++ ) {
172
+ const item = res [ i ] ;
173
+ const row = state . data . rows [ Number ( item . ref ) ] ;
174
+ let boost = 1 ;
175
+
176
+ // boost by exact match on name
177
+ if ( row . name
178
+ . toLowerCase ( )
179
+ . startsWith ( searchText . toLowerCase ( ) ) ) {
180
+ boost *= ( 1 / Math . abs ( row . name . length - searchText . length ) * 10 )
181
+ }
184
182
185
- // boost by kind
186
- for ( let kindName in searchConfig . boosts . byKind ?? { } ) {
187
- const kind : ReflectionKind = parseInt (
188
- Object . keys ( ReflectionKind ) . find (
189
- ( key : string ) =>
190
- ReflectionKind [ key as keyof typeof ReflectionKind ]
191
- . toString ( )
192
- . toLowerCase ( ) === kindName . toLowerCase ( )
193
- ) ?? "" ,
194
- 10
195
- ) ;
196
- if ( row . kind == kind ) {
197
- boost *= searchConfig ?. boosts ?. byKind ?. [ kindName ] ?? 1 ;
198
- }
183
+ // boost by kind
184
+ for ( let kindName in searchConfig . boosts ?. byKind ?? { } ) {
185
+ const kind : ReflectionKind = parseInt (
186
+ Object . keys ( ReflectionKind ) . find (
187
+ ( key : string ) =>
188
+ ReflectionKind [ key as keyof typeof ReflectionKind ]
189
+ . toString ( )
190
+ . toLowerCase ( ) === kindName . toLowerCase ( )
191
+ ) ?? "" ,
192
+ 10
193
+ ) ;
194
+ if ( row . kind == kind ) {
195
+ boost *= searchConfig ?. boosts ?. byKind ?. [ kindName ] ?? 1 ;
199
196
}
197
+ }
200
198
201
- // boost by category
202
- for ( let categoryTitle in searchConfig . boosts ?. byCategory ?? [ ] ) {
203
- if ( row . categories . indexOf ( categoryTitle ) > - 1 ) {
204
- boost *=
205
- searchConfig . boosts . byCategory ?. [ categoryTitle ] ?? 1 ;
206
- }
199
+ // boost by category
200
+ for ( let categoryTitle in searchConfig . boosts ?. byCategory ?? [ ] ) {
201
+ if ( row . categories . indexOf ( categoryTitle ) > - 1 ) {
202
+ boost *=
203
+ searchConfig . boosts ?. byCategory ?. [ categoryTitle ] ?? 1 ;
207
204
}
208
-
209
- item . score *= boost ;
210
205
}
211
206
212
- res . sort ( ( a , b ) => b . score - a . score ) ;
207
+ item . score *= boost ;
213
208
}
214
209
210
+ res . sort ( ( a , b ) => b . score - a . score ) ;
211
+
215
212
for (
216
213
let i = 0 , c = Math . min ( searchConfig . numResults ?? 10 , res . length ) ;
217
214
i < c ;
0 commit comments