如何使用 Atlas Search 复合“必须”聚合来搜索根文档字段和嵌入文档字段

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

我有以下文件

{
    "_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"
        }
    ]
}

我想同时跨根字段(出版商名称、出版商地址)和嵌入文档字段(标题、作者)执行复合“必须”搜索。

在根文档字段上使用以下复合“必须”聚合可以正常工作。此 e 聚合将返回上面的示例文档

{
    "$search": {
        "index": "my-book-Index",
        "compound": {
            "must": [
                {
                    "text": {
                        "query": "Big",
                        "path": [
                            "PublisherName",
                            "PublisherAddress"
                        ]
                    }
                },
                {
                    "text": {
                        "query": "Street",
                        "path": [
                            "PublisherName",
                            "PublisherAddress"
                        ]
                    }
                }
            ]
        }
    }
}

但是当我将 EmbeddedDocument 添加到聚合中时,复合“必须”聚合无法找到文档。

{
    "$search": {
        "index": "my-book-Index",
        "compound": {
            "must": [
                {
                    "text": {
                        "query": "Big",
                        "path": [
                            "PublisherName",
                            "PublisherAddress"
                        ]
                    }
                },
                {
                    "text": {
                        "query": "Piano",
                        "path": [
                            "PublisherName",
                            "PublisherAddress"
                        ]
                    }
                },
                {
                    "embeddedDocument": {
                        "operator": {
                            "compound": {
                                "must": [
                                    {
                                        "text": {
                                            "query": "Big",
                                            "path": [
                                                "Books.Title",
                                                "Books.Author"
                                            ]
                                        }
                                    },
                                    {
                                        "text": {
                                            "query": "Piano",
                                            "path": [
                                                "Books.Title",
                                                "Books.Author"
                                            ]
                                        }
                                    }
                                ]
                            }
                        },
                        "path": "Books"
                    }
                }
            ]
        }
    }
}

应该如何制定聚合以返回所有必须具有“Big”和“Piano”的文档? “Big”位于“PublisherName”字段中,“Piano”位于“Books.Title”字段中。两者都必须存在才能返回文档。

search aggregation atlas mongodb-atlas-search embedded-documents
1个回答
0
投票

应该如何制定聚合以返回所有必须具有“Big”和“Piano”的文档? “Big”位于“PublisherName”字段中,“Piano”位于“Books.Title”字段中。两者都必须存在才能返回文档。

如果不查看搜索索引,很难判断为什么查询不起作用。要在查询中使用

embeddedDocument
运算符,Books 数组应在搜索索引中索引为
embeddedDocuments
类型。

尝试这个配置: https://search-playground.mongodb.com/tools/code-sandbox/snapshots/6760ff5cc8d94386500c750e

© www.soinside.com 2019 - 2024. All rights reserved.