Optimizely Graph .NET client - nested logic clauses

Vote:
 

Hello, I'm working on improving the search experience on a page that is based on Optimizely Graph and I'm struggling with .NET client API to achieve the expected query.

I want to have nested logical operators like in the example below which works nicely when executed in the admin panel, but I'm not able to achieve the same result using Linq extension methods and the documentation is missing any example about how to do that. Could you please help me with that or provide an example of how to nest in a.a NET client? 

WHERE: A1 && (B1 || B2)

    where: {
      _and: [
        {
          _or: [
            { _fulltext: { match: "Query_Text" } }
            { Name: { fuzzy: false, boost: 10, match: "Query_Text" } }
          ]
        }
        {
          ContentType: {
            in: [
              "Page1TypePage",
              "Page2TypePage"
            ]
          }
        }
      ]
#330493
Sep 25, 2024 9:46
Vote:
 

Hi Stanislaw,

Actually, there are a lot of ways to build where condition such as using builtin filter expression, delegate filter builder with Optimizely Graph Client package. We can use AndFilter, OrFilter or use Bit operator such as &, | to make condition as group. Sometimes, we also need to combine some different ways, create new delegate filter builder to build our complicated condition.

With your requirement, you can do it by this following way:

var orFilter = BooleanFilter.OrFilter<GraphModels.Content>().Or("_fulltext", new StringFilterOperators().Match(q))
    .Or("Name", new StringFilterOperators().Match(q).Fuzzy(false).Boost(10));

var result = _contentGraphClient
.OperationName("Alloy_Sample_Query").ForType<GraphModels.Content>()

.Where(x => orFilter & x.ContentType.In(new [] { "NewsPage", "ArticlePage" }))

.Fields(_ => _.Name, _ => _.Url)
.Facet(_ => _.ContentType)
.Total()            
.GetResultAsync<GraphModels.Content>().Result;

You can check other information about "where" filter here https://docs.developers.optimizely.com/platform-optimizely/v1.4.0-optimizely-graph/docs/where-filter

#330989
Oct 05, 2024 9:47
Vote:
 

I did it with a few custom filters classes.

Here's an example:

public class GroupedOrFilter : Filter
{
    private readonly List<string> _orFilters = [];

    public override string FilterClause
    {
        get
        {
            string combinedFilters = string.Join("}{", _orFilters);

            return $"_and:[{{{combinedFilters}}}]";
        }
    }

    // Method to add an OR filter block inside an AND block
    public GroupedOrFilter And(OrFilter orFilter)
    {
        _orFilters.Add(orFilter.FilterClause);
       
        return this;
    }
}
#331109
Oct 08, 2024 14:55
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.