为什么我在Java循环中构建FacetOperation时只能得到第一个操作的结果?

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

我正在尝试在Java中的循环内构建一个FacetOperation,但我只得到第一个操作的结果。这是我的代码:

FacetOperation facetOperation = null;
for (int key = 0; key < 2; key++) {
    if (facetOperation == null) {
        facetOperation = facet(match(ctx -> basicQueryOriginal.getQueryObject()),
                project().and(DATA_SOURCE_ID).as("id"),
                group("id").count().as("count"),
                project("count").andExclude("_id")
        ).as("Category " + key);
    } else {
        facetOperation.and(match(ctx -> basicQueryOriginal.getQueryObject()),
                project().and(DATA_SOURCE_ID).as("id"),
                group("id").count().as("count"),
                project("count").andExclude("_id")
        ).as("Category " + key);
    }
}

Aggregation agg = newAggregation(facetOperation);
AggregationResults<Document> groupResults = mongoTemplate.aggregate(agg, "collection", Document.class);
Document facet = groupResults.getMappedResults().get(0);
facet.forEach((key, value) -> {
    countDtos.add(new CountDto(key, ((ArrayList) value).size()));
});

该循环旨在创建多个 FacetOperation 操作,但当我运行代码时,我仅获得类别 0的结果。

我也想获得类别1的结果,我正在使用MongoDB Java驱动程序和Spring Data MongoDB。谁能帮我找出我的代码有什么问题吗?

此外,如果没有循环,它的工作原理如以下答案多个方面中所述。但我的需要是使用循环来实现这个目的。

更新

@valijon 建议的解决方案对我有用。现在我还在集合中创建了一个Text Index。 我可以在我的基本查询中添加排序,并且使用它可以正常工作:

basicQuery.fields().projectAs(MongoExpression.create("'$meta' : 'textScore'"), "score");
Document document = new Document("score", MongoExpression.create("'$meta' : 'textScore'").toDocument());
basicQuery.setSortObject(document) 

现在,当我尝试在聚合中使用这个基本查询时。我收到以下错误:

org.springframework.data.mongodb.UncategorizedMongoDbException: Command failed with error 40218 (Location40218): 'query requires text score metadata, but it is not available' on server localhost:27017.

如何进行聚合以忽略文本索引或使用 $meta?

mongodb aggregation-framework spring-data-mongodb mongotemplate
1个回答
0
投票

我已经实现了@Valijon建议的更改,并且能够在聚合管道中添加对文本索引的支持。

这是我最终有效的代码:

FacetOperation facetOperation = null;
for (int key = 0; key < 2; key++) {
    if (facetOperation == null) {
        facetOperation = facet(match(ctx -> basicQueryOriginal.getQueryObject()),
                project().and(DATA_SOURCE_ID).as("id"),
                group("id").count().as("count"),
                project("count").andExclude("_id")
        ).as("Category " + key);
    } else {
        facetOperation = facetOperation.and(match(ctx -> basicQueryOriginal.getQueryObject()),
                project().and(DATA_SOURCE_ID).as("id"),
                group("id").count().as("count"),
                project("count").andExclude("_id")
        ).as("Category " + key);
    }
}
MatchOperation matchOperation = Aggregation.match(
        new TextCriteria().matchingAny("[street]").caseSensitive(false)
);
AddFieldsOperation addFieldsOperation = addFields()
        .addFieldWithValue("score", new Document("$meta", "textScore")).build();

Aggregation agg = newAggregation(matchOperation, addFieldsOperation, facetOperation);
AggregationResults<Document> groupResults = mongoTemplate.aggregate(agg, "collection", Document.class);
Document facet = groupResults.getMappedResults().get(0);
facet.forEach((key, value) -> {
    countDtos.add(new CountDto(key, ((ArrayList) value).size()));
});
© www.soinside.com 2019 - 2024. All rights reserved.