在应用匹配过滤器之前,从MongoDB Atlas搜索中获取唯一值的列表
我使用MongoDB Atlas搜索来搜索数据库中的资源列表(我使用的是Mongoose,因此语法略有不同):
const allApprovedSearchResults = await Resource.aggregate([{
$search: {
compound: {
should: [
{
wildcard: {
query: queryStringSegmented,
path: ["title", "link", "creatorName"],
allowAnalyzedField: true,
}
},
{
wildcard: {
query: queryStringSegmented,
path: ["topics"],
allowAnalyzedField: true,
"score": { "boost": { "value": 2 } },
}
}
,
{
wildcard: {
query: queryStringSegmented,
path: ["description"],
allowAnalyzedField: true,
score: { "boost": { "value": .2 } },
}
}
]
}
}
}])
.match(matchFilter)
.exec();
const uniqueLanguagesInSearchResults = [...new Set(allApprovedSearchResults.map(resource => resource.language))];
最后一行将检索结果集中的所有唯一语言。 但是,我想要在.match(matchfilter)
之前的所有语言列表。是否有办法在没有过滤器的情况下进行第二次搜索而没有进行此操作?
I use MongoDB Atlas Search to search through a list of resources in my database (I'm using Mongoose, hence the slightly different syntax):
const allApprovedSearchResults = await Resource.aggregate([{
$search: {
compound: {
should: [
{
wildcard: {
query: queryStringSegmented,
path: ["title", "link", "creatorName"],
allowAnalyzedField: true,
}
},
{
wildcard: {
query: queryStringSegmented,
path: ["topics"],
allowAnalyzedField: true,
"score": { "boost": { "value": 2 } },
}
}
,
{
wildcard: {
query: queryStringSegmented,
path: ["description"],
allowAnalyzedField: true,
score: { "boost": { "value": .2 } },
}
}
]
}
}
}])
.match(matchFilter)
.exec();
const uniqueLanguagesInSearchResults = [...new Set(allApprovedSearchResults.map(resource => resource.language))];
The last line retrieves all unique languages in the results set. However, I want a list of all the languages before .match(matchFilter)
is applied. Is there a way to do this without running a second search without the filters?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在
$ search
之后使用$ facet
:您没有提供结构,因此我假设“语言”是字段名称。
$ facet
创建一个叉子 - 一个称为“过滤器”的部分将仅包含过滤结果,而另一个称为Alllanguages的零件将包含一组所有语言,无论您使用哪种语言。添加$ project
在每个$ facet
管道中的步骤以格式化数据。根据 docs> docs ,它应该起作用:
You can use a
$facet
after the$search
:You did not provide a structure so I'm assuming 'language' is the field name. The
$facet
creates a fork - one part called 'filter' will contain only the filtered results, while the other one, called allLanguages, will contain a set of all languages, regardless of the filter.You can add$project
steps inside each$facet
pipeline to format the data.According to the docs, it should work :)