Skip to content

Commit 4ee727f

Browse files
authored
Merge pull request #776 from danieltruong/fix/mongodb-44-textscore
PUBLIC-94: Fix MongoDB 4.4 textScore compatibility
2 parents e91e608 + 0ce6bd4 commit 4ee727f

File tree

4 files changed

+30
-18
lines changed

4 files changed

+30
-18
lines changed

api/aggregators/cacAggregator.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,11 @@ exports.createMatchAggr = async (schemaName, projectId, keywords, caseSensitive,
9090
}
9191
}
9292
}
93-
},
94-
{
95-
$addFields: {
96-
score: { $meta: "textScore" }
97-
}
9893
}
9994
);
10095

96+
// Note: $meta: 'textScore' removed - CAC aggregator doesn't use $text search
97+
// and MongoDB 4.4+ requires $text when using textScore
98+
10199
return aggregation;
102100
};

api/aggregators/documentAggregator.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ exports.createMatchAggr = async (schemaName, projectId, keywords, caseSensitive,
2020
const aggregation = [];
2121
let projectModifier;
2222
let keywordModifier;
23+
let hasTextSearch = false;
2324

2425
if (projectId) {
2526
projectModifier = { project: mongoose.Types.ObjectId(projectId) };
2627
}
2728

2829
if (keywords) {
2930
keywordModifier = { $text: { $search: "\""+keywords+"\"", $caseSensitive: caseSensitive} };
31+
hasTextSearch = true;
3032
}
3133

3234
// query modifiers
@@ -124,13 +126,17 @@ exports.createMatchAggr = async (schemaName, projectId, keywords, caseSensitive,
124126
}
125127
}
126128
}
127-
},
128-
{
129+
}
130+
);
131+
132+
// Only add textScore when a $text search is present (MongoDB 4.4+ requirement)
133+
if (hasTextSearch) {
134+
aggregation.push({
129135
$addFields: {
130136
score: { $meta: 'textScore' }
131137
}
132-
}
133-
);
138+
});
139+
}
134140

135141
return aggregation;
136142
};

api/aggregators/searchAggregator.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ exports.createMatchAggr = async (schemaName, projectId, keywords, caseSensitive,
2020
const aggregation = [];
2121
let projectModifier;
2222
let keywordModifier;
23+
let hasTextSearch = false;
2324

2425
if (projectId) {
2526
projectModifier = { project: mongoose.Types.ObjectId(projectId) };
@@ -29,6 +30,7 @@ exports.createMatchAggr = async (schemaName, projectId, keywords, caseSensitive,
2930
keywords = keywords.replace(/"/g,"").trim();
3031
let keywordSearch = fuzzy && !keywords.startsWith("\"") && !keywords.endsWith("\"") ? fuzzySearch.createFuzzySearchString(keywords, 4, caseSensitive) : "\""+ keywords +"\"";
3132
keywordModifier = { $text: { $search: keywordSearch, $caseSensitive: caseSensitive } };
33+
hasTextSearch = true;
3234
}
3335

3436
// query modifiers
@@ -85,13 +87,17 @@ exports.createMatchAggr = async (schemaName, projectId, keywords, caseSensitive,
8587
}
8688
}
8789
}
88-
},
89-
{
90+
}
91+
);
92+
93+
// Only add textScore when a $text search is present (MongoDB 4.4+ requirement)
94+
if (hasTextSearch) {
95+
aggregation.push({
9096
$addFields: {
9197
score: { $meta: 'textScore' }
9298
}
93-
}
94-
);
99+
});
100+
}
95101

96102
return aggregation;
97103
};

api/dao/projectDAO.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,13 @@ exports.getProjects = async function(roles, pageNumber, pageSize, sortBy, keywor
228228
}
229229
}
230230
});
231-
// Score, Misc.
232-
queryAggregates.push(
233-
{
234-
$addFields: { score:{ $meta: 'textScore' } }
235-
});
231+
// Score - only add textScore when keywords are present (MongoDB 4.4+ requirement)
232+
if (keywords && keywords.length > 0) {
233+
queryAggregates.push(
234+
{
235+
$addFields: { score:{ $meta: 'textScore' } }
236+
});
237+
}
236238

237239
let collation =
238240
{

0 commit comments

Comments
 (0)