如何在C#中从searchmeta获取mongodb facet

8ehkhllq  于 2023-03-17  发布在  Go
关注(0)|答案(1)|浏览(134)

我正在尝试将一个atlas searchmeta移植到c#中
图集功能:

$searchMeta: {
  index: "myindex",
  facet:{
    operator: {
      compound:{
        must:defaults.aggregateFilters
      }
    },
    facets: {
      type: "string",
      path:"category"
    }
  }
}

我用以下代码转换了facet对象的第一部分:有一个迭代的过程来构建过滤器,但是为了简洁起见,我已经硬编码了示例。

var searchBuilder = new SearchDefinitionBuilder<MyModel>();
var clauses = new List<SearchDefinition<MyModel>>();

clauses.Add(searchBuilder.Phrase("topic", "water"));

var compoundSearchDef = Builders<Product>.Search.Compound();

compoundSearchDef.Must(clauses);

var aggPipeline = new EmptyPipelineDefinition<MyModel>()
 .AppendStage(PipelineStageDefinitionBuilder.SearchMeta<MyModel>(searchDefinition: compoundSearchDef, indexName: MySearchIndexName));

var aggResult = await collection.Aggregate(pipeline: aggPipeline).ToListAsync();

上面的代码给了我计数,但是facet为空。这是有意义的,因为没有定义facet。这是我一直在使用的atlas函数的代码:

facets: {
  type: "string",
  path:"category"
}

在c#中我该把它加到哪里呢?

qncylg1j

qncylg1j1#

如果有人感兴趣,我需要稍微更改聚合调用

var aggResult = await collection.Aggregate().SearchMeta(Builders<myModel>.Search.Facet(Builders<myModel>.Search.Compound().Must(clauses),aggFacets),indexName: "theIndex").ToListAsync();

相关问题