Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Signed-off-by: Kostas <[email protected]>
  • Loading branch information
DumboJetEngine committed Jan 8, 2024
1 parent 85451de commit ab7fd4f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

using System;
using System.Runtime.Serialization;
using OpenSearch.Client.QueryDsl.Visitor;
using OpenSearch.Net.Utf8Json;

namespace OpenSearch.Client
Expand Down Expand Up @@ -131,5 +132,36 @@ out QueryContainer queryContainer

// ReSharper disable once UnusedMember.Global
internal bool ShouldSerialize(IJsonFormatterResolver formatterResolver) => IsWritable;


public QueryContainer Name(string name)
{
ContainedQuery.Name = name;
return this;
}

/// <summary>
/// Applies the `strict` attribute to the query container and all child sub-queries.
/// </summary>
/// <param name="strict"></param>
/// <returns></returns>
public QueryContainer Strict(bool strict)
{
var visitor = new QueryNodeModifierVisitor((query, ctx) => query.IsStrict = true);
Accept(visitor);
return this;
}

/// <summary>
/// Applies the `verbatim` attribute to the query container and all child sub-queries.
/// </summary>
/// <param name="strict"></param>
/// <returns></returns>
public QueryContainer Verbatim(bool strict)
{
var visitor = new QueryNodeModifierVisitor((query, ctx) => query.IsVerbatim = true);
Accept(visitor);
return this;
}
}
}
31 changes: 31 additions & 0 deletions src/OpenSearch.Client/QueryDsl/Visitor/QueryNodeModifierVisitor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;

namespace OpenSearch.Client.QueryDsl.Visitor
{
public class QueryNodeModifierVisitor : QueryVisitor
{
private readonly Action<IQuery, Context> action;
private Context context;

public struct Context
{
public int Depth { get; internal set; }
public VisitorScope Scope { get; internal set; }
}

public QueryNodeModifierVisitor(Action<IQuery, Context> action)
{
this.action = action;
context = new Context();
}

public override void Visit(IQuery query)
{
context.Depth = Depth;
context.Scope = Scope;

action(query, context);
base.Visit(query);
}
}
}

0 comments on commit ab7fd4f

Please sign in to comment.