I have an aggregation which doesn't work in mongodb
and spring boot
. I would be grateful if anyone could help me.
Here is my ExplainDoc
class:
@Document(collection = "ExplainDoc")
public class ExplainDoc{
@Id
private String id;
@TextIndexed(weight=3)
private String product_in_brief;
private Product product;
@TextScore
private Float textScore; }
And here is my other class:
@Document(collection = "product")
public class Product{
@Id
private String id;
private String category;
}
What I want to do is to make a text search and find all ExplainDoc
s which have the given text in their product_in_brief
PROVIDED THAT their product has a specific category.
In my search repository, I have an aggregation like the following:
public List<MyAggrResults> searchBriefExplanations(String text, String category){
MatchOperation matchRegion = Aggregation.match(Criteria.where("product.category").is(category));
TextCriteria criteria = TextCriteria.forDefaultLanguage().matchingAny(text);
MatchOperation match = Aggregation.match(criteria);
GroupOperation group = Aggregation.group("product.category").push("$$ROOT").as("myresults").sum("textScore").as("score");
ProjectionOperation project = Aggregation.project("product_in_brief", "product").andExpression("{$meta: \"textScore\"}").as("textScore");
}
The code works now. However, I see it is so expensive to have the product
object always as a nested document. How should I change the code if I want to use the product
object as @DBRef
? When I add the @DBRef
, the code doesn't work anymore. I think the reason is that the product.category
is not recognized anymore.
I hope somebody can help me.