使用Compound/Must 跨嵌入文档和根文档字段进行图集搜索

问题描述 投票:0回答:1

如何在 EnbeddedDocument 和 RootDocument 字段之间执行“Compound/Must”? 我已经为我面临的这个问题创建了一个游乐场

https://search-playground.mongodb.com/tools/code-playground/snapshots/6728e35e406912c3e0e06912

使用示例,我想在所有可搜索字段中搜索“Big”和“Piano”。两个值都必须存在。搜索结果应返回_id为“101”的文档,因为“PublisherName”中存在“Big”,“Books.Title”中存在“Piano”。

示例文档

{
    "_id": "101",
    "PublisherName": "Big book publishers",
    "PublisherAddress": "123 Mian Street",
    "Books": [
        {
            "_id": "6791913210",
            "Title": "Piano made simple",
            "Author": "John Bent"
        },
        {
            "_id": "6638200210",
            "Title": "Guitar made simple",
            "Author": "Kim Larry"
        }
    ]
}

搜索索引

{
  "mappings": {
    "dynamic": false,
    "fields": {
      "Books": {
        "dynamic": false,
        "fields": {
          "Author": {
            "analyzer": "lucene.standard",
            "type": "string"
          },
          "Title": {
            "analyzer": "lucene.standard",
            "type": "string"
          }
        },
        "type": "embeddedDocuments"
      },
      "PublisherAddress": {
        "analyzer": "lucene.standard",
        "type": "string"
      },
      "PublisherName": {
        "analyzer": "lucene.standard",
        "type": "string"
      }
    }
  }
}

我的示例搜索不起作用(添加嵌入文档时)

[
    {
        "$search": {
            "index": "default",
            "compound": {
                "must": [
                    {
                        "text": {
                            "query": "Big",
                            "path": [
                                "PublisherName",
                                "PublisherAddress"
                            ]
                        }
                    },
                    {
                        "text": {
                            "query": "Piano",
                            "path": [
                                "PublisherName",
                                "PublisherAddress"
                            ]
                        }
                    },
                    {
                        "embeddedDocument": {
                            "operator": {
                                "text": {
                                    "query": "Big",
                                    "path": [
                                        "Books.Title",
                                        "Books.Author"
                                    ]
                                },
                                "text": {
                                    "query": "Piano",
                                    "path": [
                                        "Books.Title",
                                        "Books.Author"
                                    ]
                                }
                            },
                            "path": "Books"
                        }
                    }
                ]
            }
        }
    }
]
mongodb search aggregation atlas mongodb-atlas-search
1个回答
0
投票

我的帖子已在 Mongo DB 社区论坛中得到答复。

https://www.mongodb.com/community/forums/t/atlas-search-across-enbedded-document-and-root-document-fields-using-compound-must/303015

为了解决此问题,执行了以下搜索聚合

搜索

[
  {
    "$search": {
      "index": "default",
      "compound": {
        "must": [
          {
            "text": {
              "query": "big",
              "path": {
                "wildcard": "*"
              }
            }
          },
          {
            "text": {
              "query": "piano",
              "path": {
                "wildcard": "*"
              }
            }
          }
        ]
      }
    }
  }
]
© www.soinside.com 2019 - 2024. All rights reserved.