Skip to content

Commit

Permalink
Increase weight of key fields, sort by matching score
Browse files Browse the repository at this point in the history
  • Loading branch information
a3957273 committed Apr 2, 2024
1 parent 3b620a9 commit d203c07
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion backend/src/models/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const ModelSchema = new Schema<ModelInterface>(
)

ModelSchema.plugin(MongooseDelete, { overrideMethods: 'all', deletedBy: true, deletedByType: Schema.Types.ObjectId })
ModelSchema.index({ '$**': 'text' })
ModelSchema.index({ '$**': 'text' }, { weights: { name: 10, description: 5 } })

const ModelModel = model<ModelInterface>('v2_Model', ModelSchema)

Expand Down
6 changes: 4 additions & 2 deletions backend/src/services/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,15 @@ export async function searchModels(
// Find only matching documents
.find(query)

if (!search)
if (!search) {
// Sort by last updated
cursor = cursor.sort({ updatedAt: -1 })
} else {
// Sort by text search
cursor = cursor.sort({ score: { $meta: 'textScore' } })
}

const results = await cursor

const auths = await authorisation.models(user, results, ModelAction.View)
return results.filter((_, i) => auths[i].success)
}
Expand Down

0 comments on commit d203c07

Please sign in to comment.