How to I add facet definitions to searchMeta in c#

I am lost as to where I add the facet definitions in the searchmeta pipline.

Here is a simplified example of my process

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

I have a process the adds clauses based on data passed in, so to simplify the example here is one clause added to the filter with hard coded values:

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

This works for getting the lower bounds count, but the facets return null, which makes sense, since I have not defined any. I just can’t seam to find where I add them in.

Here is a working example I am trying to port from atlas to c#

$searchMeta: {
        index: defaults.graphIndex,
        facet:{
          operator: {
            compound:{
              must:defaults.aggregateFilters
            }
          },
          facets: searchMetaFacets
        }
      }

This is the part I am stuck on converting to c# facets: searchMetaFacets

Hi Jeff. I see you’re having difficulty figuring out how to use the facets in the search meta pipeline. We recognize that our existing documentation is a bit sparse on this use case and our team is already planning to expand documentation to address this question.

In the meantime, hopefully I can help.

The short answer is that you can include your facet definition using an additional pipeline stage (facet) and that the query should take the form of:

collection().Aggregate().SearchMeta(Builders.Search.Facet(...))

Here’s a working example of an aggregation using facets from our driver test cases.

    var result = GetTestCollection().Aggregate()
                .SearchMeta(Builders.Search.Facet(
                    Builders.Search.Phrase(x => x.Body, "life, liberty, and the pursuit of happiness"),
                    Builders.SearchFacet.String("string", x => x.Author, 100),
                    Builders.SearchFacet.Number("number", x => x.Index, 0, 100),
                    Builders.SearchFacet.Date("date", x => x.Date, DateTime.MinValue, DateTime.MaxValue)))
                .Single();

You can also reference the API docs for the Facet pipeline stage here.

I’m out of office for the next few days, but I’ll check back in on Monday to see how things are looking. Hope this helps!

That was exactly what I needed!

Thanks.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.