Skip to content

Commit

Permalink
Merge pull request #654 from HDRUK/hotfix/GAT-663-SUPP-838
Browse files Browse the repository at this point in the history
Hotfix/gat 663 supp 838
  • Loading branch information
cdjreekie authored Feb 1, 2022
2 parents 876deb3 + f334944 commit bd4da74
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 67 deletions.
40 changes: 40 additions & 0 deletions src/resources/dataUseRegister/dataUseRegister.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,46 @@ export default class DataUseRegisterRepository extends Repository {
}
}

async getDataUseRegistersFilters(query, options = {}) {
if (options.aggregate) {
const searchTerm = (query && query['$and'] && query['$and'].find(exp => !isNil(exp['$text']))) || {};

if (searchTerm) {
query['$and'] = query['$and'].filter(exp => !exp['$text']);
}

const aggregateQuery = [
{ $match: searchTerm },
{
$lookup: {
from: 'publishers',
localField: 'publisher',
foreignField: '_id',
as: 'publisherDetails',
},
},
{
$addFields: {
publisherInfo: { name: '$publisherDetails.name' },
},
},
{ $match: { $and: [...query['$and']] } },
];

if (query.fields) {
aggregateQuery.push({
$project: query.fields.split(',').reduce((obj, key) => {
return { ...obj, [key]: 1 };
}, {}),
});
}
return DataUseRegister.aggregate(aggregateQuery);
} else {
const options = { lean: true };
return this.find(query, options);
}
}

getDataUseRegisterByApplicationId(applicationId) {
return this.dataUseRegister.findOne({ projectId: applicationId }, 'id').lean();
}
Expand Down
2 changes: 1 addition & 1 deletion src/resources/filters/filters.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export default class FiltersService {
break;
case 'dataUseRegister':
fields = `organisationName,organisationSector,keywords,publisherDetails.name,fundersAndSponsors`;
entities = await this.DataUseRegisterRepository.getDataUseRegisters({ ...query, fields }, { aggregate: true });
entities = await this.DataUseRegisterRepository.getDataUseRegistersFilters({ ...query, fields }, { aggregate: true });
break;
}
// 3. Loop over each entity
Expand Down
162 changes: 96 additions & 66 deletions src/resources/search/search.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,26 @@ export async function getObjectResult(type, searchAll, searchQuery, startIndex,
newSearchQuery['$and'] = newSearchQuery['$and'].filter(exp => !exp['$text']);
}

let dataUseSort = {};

switch (sort) {
case '':
dataUseSort = searchAll ? { lastActivity: -1 } : { score: { $meta: 'textScore' } };
break;
case 'relevance':
dataUseSort = searchAll ? { projectTitle: 1 } : { score: { $meta: 'textScore' } };
break;
case 'popularity':
dataUseSort = searchAll ? { counter: -1, projectTitle: 1 } : { counter: -1, score: { $meta: 'textScore' } };
break;
case 'latest':
dataUseSort = searchAll ? { lastActivity: -1 } : { lastActivity: -1, score: { $meta: 'textScore' } };
break;
case 'resources':
dataUseSort = searchAll ? { relatedResourcesCount: -1 } : { relatedResourcesCount: -1, score: { $meta: 'textScore' } };
break;
}

queryObject = [
{ $match: searchTerm },
{
Expand All @@ -146,6 +166,16 @@ export async function getObjectResult(type, searchAll, searchQuery, startIndex,
as: 'publisherDetails',
},
},
{
$addFields: {
publisherInfo: { name: '$publisherDetails.name' },
},
},
{ $match: newSearchQuery },
{ $addFields: { relatedResourcesCount: { $size: { $ifNull: ['$relatedObjects', []] } } } },
{ $sort: dataUseSort },
{ $skip: parseInt(startIndex) },
{ $limit: maxResults },
{
$lookup: {
from: 'tools',
Expand All @@ -170,12 +200,6 @@ export async function getObjectResult(type, searchAll, searchQuery, startIndex,
as: 'gatewayDatasetsInfo',
},
},
{
$addFields: {
publisherInfo: { name: '$publisherDetails.name' },
},
},
{ $match: newSearchQuery },
{
$project: {
_id: 0,
Expand Down Expand Up @@ -390,75 +414,81 @@ export async function getObjectResult(type, searchAll, searchQuery, startIndex,
];
}

if (sort === '') {
if (type === 'dataset') {
if (type !== 'dataUseRegister') {
if (sort === '') {
if (type === 'dataset') {
if (searchAll) queryObject.push({ $sort: { 'datasetfields.metadataquality.weighted_quality_score': -1, name: 1 } });
else queryObject.push({ $sort: { score: { $meta: 'textScore' } } });
} else if (type === 'paper') {
if (searchAll) queryObject.push({ $sort: { journalYear: -1 } });
else queryObject.push({ $sort: { journalYear: -1, score: { $meta: 'textScore' } } });
} else {
if (form === 'true' && searchAll) {
queryObject.push({ $sort: { myEntity: -1, latestUpdate: -1 } });
} else if (form === 'true' && !searchAll) {
queryObject.push({ $sort: { myEntity: -1, score: { $meta: 'textScore' } } });
} else if (form !== 'true' && searchAll) {
queryObject.push({ $sort: { latestUpdate: -1 } });
} else if (form !== 'true' && !searchAll) {
queryObject.push({ $sort: { score: { $meta: 'textScore' } } });
}
}
} else if (sort === 'relevance') {
if (type === 'person') {
if (searchAll) queryObject.push({ $sort: { lastname: 1 } });
else queryObject.push({ $sort: { score: { $meta: 'textScore' } } });
} else {
if (searchAll) queryObject.push({ $sort: { name: 1 } });
else queryObject.push({ $sort: { score: { $meta: 'textScore' } } });
}
} else if (sort === 'popularity') {
if (type === 'person') {
if (searchAll) queryObject.push({ $sort: { counter: -1, lastname: 1 } });
else queryObject.push({ $sort: { counter: -1, score: { $meta: 'textScore' } } });
} else {
if (searchAll) queryObject.push({ $sort: { counter: -1, name: 1 } });
else queryObject.push({ $sort: { counter: -1, score: { $meta: 'textScore' } } });
}
} else if (sort === 'metadata') {
if (searchAll) queryObject.push({ $sort: { 'datasetfields.metadataquality.weighted_quality_score': -1, name: 1 } });
else queryObject.push({ $sort: { score: { $meta: 'textScore' } } });
} else if (type === 'paper') {
if (searchAll) queryObject.push({ $sort: { journalYear: -1 } });
else queryObject.push({ $sort: { journalYear: -1, score: { $meta: 'textScore' } } });
} else {
else queryObject.push({ $sort: { 'datasetfields.metadataquality.weighted_quality_score': -1, score: { $meta: 'textScore' } } });
} else if (sort === 'startdate') {
if (form === 'true' && searchAll) {
queryObject.push({ $sort: { myEntity: -1, latestUpdate: -1 } });
queryObject.push({ $sort: { myEntity: -1, 'courseOptions.startDate': 1 } });
} else if (form === 'true' && !searchAll) {
queryObject.push({ $sort: { myEntity: -1, score: { $meta: 'textScore' } } });
queryObject.push({ $sort: { myEntity: -1, 'courseOptions.startDate': 1, score: { $meta: 'textScore' } } });
} else if (form !== 'true' && searchAll) {
queryObject.push({ $sort: { latestUpdate: -1 } });
queryObject.push({ $sort: { 'courseOptions.startDate': 1 } });
} else if (form !== 'true' && !searchAll) {
queryObject.push({ $sort: { score: { $meta: 'textScore' } } });
queryObject.push({ $sort: { myEntity: -1, 'courseOptions.startDate': 1, score: { $meta: 'textScore' } } });
}
} else if (sort === 'latest') {
if (searchAll) queryObject.push({ $sort: { latestUpdate: -1 } });
else queryObject.push({ $sort: { latestUpdate: -1, score: { $meta: 'textScore' } } });
} else if (sort === 'resources') {
if (searchAll) queryObject.push({ $sort: { relatedresources: -1 } });
else queryObject.push({ $sort: { relatedresources: -1, score: { $meta: 'textScore' } } });
} else if (sort === 'sortbyyear') {
if (type === 'paper') {
if (searchAll) queryObject.push({ $sort: { journalYear: -1 } });
else queryObject.push({ $sort: { journalYear: -1, score: { $meta: 'textScore' } } });
}
}
} else if (sort === 'relevance') {
if (type === 'person') {
if (searchAll) queryObject.push({ $sort: { lastname: 1 } });
else queryObject.push({ $sort: { score: { $meta: 'textScore' } } });
} else {
if (searchAll) queryObject.push({ $sort: { name: 1 } });
else queryObject.push({ $sort: { score: { $meta: 'textScore' } } });
}
} else if (sort === 'popularity') {
if (type === 'person') {
if (searchAll) queryObject.push({ $sort: { counter: -1, lastname: 1 } });
else queryObject.push({ $sort: { counter: -1, score: { $meta: 'textScore' } } });
} else {
if (searchAll) queryObject.push({ $sort: { counter: -1, name: 1 } });
else queryObject.push({ $sort: { counter: -1, score: { $meta: 'textScore' } } });
}
} else if (sort === 'metadata') {
if (searchAll) queryObject.push({ $sort: { 'datasetfields.metadataquality.weighted_quality_score': -1, name: 1 } });
else queryObject.push({ $sort: { 'datasetfields.metadataquality.weighted_quality_score': -1, score: { $meta: 'textScore' } } });
} else if (sort === 'startdate') {
if (form === 'true' && searchAll) {
queryObject.push({ $sort: { myEntity: -1, 'courseOptions.startDate': 1 } });
} else if (form === 'true' && !searchAll) {
queryObject.push({ $sort: { myEntity: -1, 'courseOptions.startDate': 1, score: { $meta: 'textScore' } } });
} else if (form !== 'true' && searchAll) {
queryObject.push({ $sort: { 'courseOptions.startDate': 1 } });
} else if (form !== 'true' && !searchAll) {
queryObject.push({ $sort: { myEntity: -1, 'courseOptions.startDate': 1, score: { $meta: 'textScore' } } });
}
} else if (sort === 'latest') {
if (searchAll) queryObject.push({ $sort: { latestUpdate: -1 } });
else queryObject.push({ $sort: { latestUpdate: -1, score: { $meta: 'textScore' } } });
} else if (sort === 'resources') {
if (searchAll) queryObject.push({ $sort: { relatedresources: -1 } });
else queryObject.push({ $sort: { relatedresources: -1, score: { $meta: 'textScore' } } });
} else if (sort === 'sortbyyear') {
if (type === 'paper') {
if (searchAll) queryObject.push({ $sort: { journalYear: -1 } });
else queryObject.push({ $sort: { journalYear: -1, score: { $meta: 'textScore' } } });
}
}

// Get paged results based on query params
const searchResults = await collection
.aggregate(queryObject)
.skip(parseInt(startIndex))
.limit(parseInt(maxResults))
.catch(err => {
console.log(err);
});
// Return data
const searchResults =
type === 'dataUseRegister'
? await collection.aggregate(queryObject).catch(err => {
console.log(err);
})
: await collection
.aggregate(queryObject)
.skip(parseInt(startIndex))
.limit(parseInt(maxResults))
.catch(err => {
console.log(err);
});

return { data: searchResults };
}

Expand Down

0 comments on commit bd4da74

Please sign in to comment.