Skip to content

Commit

Permalink
Merge pull request #1194 from gchq/feature/improve-search
Browse files Browse the repository at this point in the history
  • Loading branch information
a3957273 authored Apr 2, 2024
2 parents b0117ee + d203c07 commit b582d3f
Show file tree
Hide file tree
Showing 2 changed files with 10 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
11 changes: 9 additions & 2 deletions backend/src/services/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,19 @@ export async function searchModels(
}
}

const results = await ModelModel
let cursor = ModelModel
// Find only matching documents
.find(query)

if (!search) {
// Sort by last updated
.sort({ updatedAt: -1 })
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 b582d3f

Please sign in to comment.