在应用匹配过滤器之前,从MongoDB Atlas搜索中获取唯一值的列表

发布于 2025-01-23 13:49:42 字数 1398 浏览 0 评论 0原文

我使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放低过去 2025-01-30 13:49:42

您可以在$ search之后使用$ facet

.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 } },
                    }
                }
            ]
        }
    }
},
  {
    "$facet": {
      "filter": [
        {$match: matchFilter}
      ],
      "allLanguages ": [
        {$group: {_id: 0, all: {$addToSet: '$language'}}}, //<- replace '$language' with real field name
      ]
    }
  }
])

您没有提供结构,因此我假设“语言”是字段名称。 $ facet创建一个叉子 - 一个称为“过滤器”的部分将仅包含过滤结果,而另一个称为Alllanguages的零件将包含一组所有语言,无论您使用哪种语言。添加$ project在每个$ facet管道中的步骤以格式化数据。

根据 docs> docs ,它应该起作用:

You can use a $facet after the $search:

.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 } },
                    }
                }
            ]
        }
    }
},
  {
    "$facet": {
      "filter": [
        {$match: matchFilter}
      ],
      "allLanguages ": [
        {$group: {_id: 0, all: {$addToSet: '$language'}}}, //<- replace '$language' with real field name
      ]
    }
  }
])

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文