Elasticsearch 7.1:将 mongodb 聚合管道转换为弹性搜索查询

问题描述

这是我第一次使用弹性搜索,所以请耐心等待。 我有两个模式,优惠模式和优惠评论模式,我需要按优惠标题搜索,并让每个优惠结果及其评论

要约架构

{
    title: {
        type: String,es_indexed: true,}
}

在上面的报价模式中,没有引用报价评论的字段

商品评论架构

{
    offer: {
      type: Schema.Types.ObjectId,ref: 'Offer',},comment: {
      type: String,}
}

在上面的报价评论模式中,报价字段引用了评论所属的报价。 以下聚合管道用于通过 mongodb 驱动程序实现所需的输出

Mongodb 聚合管道在优惠集合上完成

[
  {
    $lookup: {
      from: 'offercomments',let: {
        id: '$_id',pipeline: [
        {
          $match: {
            $expr: {
              $and: [
                {
                  $eq: ['$offer','$$id'],],{
          $sort: {
            createdAt: -1,{
          $limit: 1,as: 'comments',];

这里是聚合管道的一些输出样本

聚合输出示例

[
  {
    "_id": "123","title": "offer title sample","comments": [
      {
        "_id": "_123","offer": "123","comment": "Hello there"
      }
    ]
  },{
    "_id": "456","comments": [
      {
        "_id": "_456","offer": "456","comment": "Hello there"
      }
    ]
  }
]

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)