Reputation: 888
I have the below document
{
"_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"
}
]
}
I would like to perform a Compound "MUST" search across the root fields (PublisherName,PublisherAddress) and embeded document fields (Title, Author) at the same time.
Using the below Compound "Must" aggregation on the root document fields work OK. This e aggregation will return the sample document above
{
"$search": {
"index": "my-book-Index",
"compound": {
"must": [
{
"text": {
"query": "Big",
"path": [
"PublisherName",
"PublisherAddress"
]
}
},
{
"text": {
"query": "Street",
"path": [
"PublisherName",
"PublisherAddress"
]
}
}
]
}
}
}
But the Compound "Must" aggregation is faling to find a document when I add the EmbeddedDocument to the aggregation.
{
"$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"
}
}
]
}
}
}
How should the aggregation be formulated to return all documents that MUST have "Big" and "Piano"? "Big" is in the "PublisherName" field, and "Piano" is in the "Books.Title" field. Both MUST exist for the document to be returned.
Upvotes: 0
Views: 37
Reputation: 110
How should the aggregation be formulated to return all documents that MUST have "Big" and "Piano"? "Big" is in the "PublisherName" field, and "Piano" is in the "Books.Title" field. Both MUST exist for the document to be returned.
It's hard to tell why your query doesn't work without looking into the search index. To use embeddedDocument
operator in query, Books array should be indexed as the embeddedDocuments
type in the search index.
Try this configuration: https://search-playground.mongodb.com/tools/code-sandbox/snapshots/6760ff5cc8d94386500c750e
Upvotes: 0