Skip to content

Commit

Permalink
Fixed test
Browse files Browse the repository at this point in the history
  • Loading branch information
niemyjski committed Sep 3, 2024
1 parent 780342d commit a0666bf
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using Foundatio.Parsers.LuceneQueries.Nodes;
using Foundatio.Parsers.LuceneQueries.Visitors;
Expand Down Expand Up @@ -189,9 +189,14 @@ public static void SetOriginalField(this IFieldQueryNode node, string field)
}

private const string OperationTypeKey = "@OperationType";
public static bool HasOperationType(this IQueryNode node)
{
return node.Data.ContainsKey(OperationTypeKey);
}

public static string GetOperationType(this IQueryNode node)
{
if (!node.Data.TryGetValue(OperationTypeKey, out var value))
if (!node.Data.TryGetValue(OperationTypeKey, out object value))
return null;

return (string)value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@ public class AssignOperationTypeVisitor : ChainableQueryVisitor
{
public override Task VisitAsync(GroupNode node, IQueryVisitorContext context)
{
if (node.HasOperationType())
return Task.CompletedTask;

if (String.IsNullOrEmpty(node.Field))
return base.VisitAsync(node, context);

if (node.Left is not TermNode leftTerm)
{
// For sub aggregations we need to see if there is a parent with parens
var closestParentWithParens = node.GetGroupNode();
if (closestParentWithParens is { HasParens: true })
return base.VisitAsync(node, context);

context.AddValidationError($"Aggregations ({node.Field}) must specify a field.");
return Task.CompletedTask;
}
Expand All @@ -35,6 +43,17 @@ public override Task VisitAsync(GroupNode node, IQueryVisitorContext context)

public override void Visit(TermNode node, IQueryVisitorContext context)
{
if (node.HasOperationType())
return;

if (String.IsNullOrEmpty(node.Field))
return;

// For sub aggregations we need to see if there is a parent with parens
var closestParentWithParens = node.GetGroupNode();
if (closestParentWithParens is { HasParens: true })
return;

if (String.IsNullOrEmpty(node.Field) && !String.IsNullOrEmpty(node.Term))
{
context.AddValidationError($"Aggregations ({node.Term}) must specify a field.");
Expand All @@ -51,7 +70,7 @@ public override void Visit(TermNode node, IQueryVisitorContext context)
node.SetOperationType(node.Field);
node.Field = node.Term;
node.Term = null;
}
}
}

public static class AggregationType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ private async Task ResolveField(IFieldQueryNode node, IQueryVisitorContext conte

if (resolvedField == null)
{
if (context.QueryType is QueryTypes.Aggregation && node.Field.StartsWith("@"))
return;

// add field to unresolved fields list
context.GetValidationResult().UnresolvedFields.Add(node.Field);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1495,20 +1495,21 @@ private async Task<string> GetIncludeAsync(string name)
return "included:value";
}

[Fact]
public async Task CanValidateAggregation()
[Theory]
[InlineData("terms:field1")]
[InlineData("terms:(field1~100 @missing:__missing__)")]
[InlineData("terms:(field1~100 (@missing:__missing__))")]
public async Task CanValidateAggregation(string aggregation)
{
var index = CreateRandomIndex<MyType>(d => d.Properties(p => p.Keyword(e => e.Name(m => m.Field1))));
await Client.IndexAsync(new MyType { Field1 = "value123" }, i => i.Index(index));
await Client.Indices.RefreshAsync(index);

string index = CreateRandomIndex<MyType>(d => d.Properties(p => p.Keyword(e => e.Name(m => m.Field1))));
var context = new ElasticQueryVisitorContext { QueryType = QueryTypes.Aggregation };
var parser = new ElasticQueryParser(c => c.UseMappings(Client, index).SetValidationOptions(new QueryValidationOptions { AllowUnresolvedFields = false, }).SetLoggerFactory(Log));
var node = await parser.ParseAsync("terms:(id~100 @missing:__missing__)", context);
var parser = new ElasticQueryParser(c => c.UseMappings(Client, index).SetLoggerFactory(Log));
var node = await parser.ParseAsync(aggregation, context);

var result = await ValidationVisitor.RunAsync(node, context);
Assert.True(result.IsValid, result.Message);
Assert.Single(result.ReferencedFields, "field1");
Assert.Empty(result.UnresolvedFields);
}
}

Expand Down

0 comments on commit a0666bf

Please sign in to comment.