如何在 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"
}
}
]
}
}
}
]
我的帖子已在 Mongo DB 社区论坛中得到答复。
为了解决此问题,执行了以下搜索聚合
搜索
[
{
"$search": {
"index": "default",
"compound": {
"must": [
{
"text": {
"query": "big",
"path": {
"wildcard": "*"
}
}
},
{
"text": {
"query": "piano",
"path": {
"wildcard": "*"
}
}
}
]
}
}
}
]