Jeff VanHorn
Jeff VanHorn

Reputation: 1

How to get mongodb facets from searchmeta in c#

I am trying to port an atlas searchmeta into c#

The atlas function:

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

I have the first part of the facet object converted with this: There is an iterative process that builds the filters, but I have hard coded and example for brevity.

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();

The code above gives me the count, but facet is null. This makes sense, because no facets were defined. This is the code from the atlas function I am stuck on:

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

Where do I add that in c#?

Upvotes: 0

Views: 361

Answers (1)

Jeff VanHorn
Jeff VanHorn

Reputation: 1

For anyone interested, I need to change the aggregate call a little

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

Upvotes: 0

Related Questions