如何使用 Atlas Search 搜索嵌入文档字段和根文档字段

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

我的收藏中有以下文件

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

我定义了搜索索引如下

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

我可以使用单独的搜索定义在“PublisherName”或“Author”字段中进行搜索

[搜索出版商名称]

{
    "index": "book-search-Index",
    "text": {
        "query": "Big book publishers",
        "path": ["PublisherName"]
    }
}

[搜索书籍.作者]

{
    "index": "book-search-Index",
    "embeddedDocument": {
        "path": "Books",
        "operator": {
            "text": {
                "path": ["Books.Author"],
                "query": "Guitar"
            }
        }
    }
}

但我无法定义一个能够同时搜索 PublisherName 字段和 Book.Author 字段的搜索定义。我尝试了下面的方法,但出现错误 “原因:最多可以有 [autocomplete、compound、embeddedDocument、equals、exists、geoShape、geoWithin、in、knnBeta、moreLikeThis、near、phrase、queryString、range、regex、search、span、term、text、wildcard] 之一出席

{
    "index": "book-search-Index",
    "text": {
        "query": "Big book publishers",
        "path": ["PublisherName"]
    }
    "embeddedDocument": {
        "path": "Books",
        "operator": {
            "text": {
                "path": ["Books.Author"],
                "query": "Guitar"
            }
        }
    }
}
mongodb aggregation-framework aggregation mongodb-atlas-search embedded-documents
1个回答
0
投票

这个问题的解决办法是

{
    "index": "book-search-Index",
    "compound": {
        "should": [
            {
                "embeddedDocument": {
                    "path": "Books",
                    "operator": {
                        "compound": {
                            "should": [
                                {
                                    "text": {
                                        "path": [
                                            "Books.Author",
                                        ],
                                        "query": "Guitar"
                                    }
                                }
                            ]
                        }
                    }
                }
            },
            {
                "text": {
                    "query": "Big book publishers",
                    "path": [
                        "PublisherName"
                    ]
                }
            }
        ]
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.