forked from opensearch-project/opensearch-net
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for opensearch-project#375 - Added
.Strict(bool)
and `.Verbatim…
…(bool)` methods on `QueryContainer` that recursively apply the respective attribute to all subqueries. Also added a `.Name(string)` method on `QueryContainer` that names the (root) contained query. Signed-off-by: Kostas <[email protected]>
- Loading branch information
1 parent
4e721ee
commit 0ca18bc
Showing
7 changed files
with
264 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/OpenSearch.Client/QueryDsl/Visitor/QueryNodeModifierVisitor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
using System; | ||
|
||
namespace OpenSearch.Client.QueryDsl.Visitor | ||
{ | ||
public class QueryNodeModifierVisitor : QueryVisitor | ||
{ | ||
private readonly Action<IQuery, QueryNodeModifierVisitorContext> action; | ||
private QueryNodeModifierVisitorContext context; | ||
|
||
public QueryNodeModifierVisitor(Action<IQuery, QueryNodeModifierVisitorContext> action) | ||
{ | ||
this.action = action; | ||
context = new QueryNodeModifierVisitorContext(); | ||
} | ||
|
||
public override void Visit(IQuery query) | ||
{ | ||
context.Depth = Depth; | ||
context.Scope = Scope; | ||
|
||
action(query, context); | ||
base.Visit(query); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/OpenSearch.Client/QueryDsl/Visitor/QueryNodeModifierVisitorContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
namespace OpenSearch.Client.QueryDsl.Visitor | ||
{ | ||
public struct QueryNodeModifierVisitorContext | ||
{ | ||
public int Depth { get; internal set; } | ||
public VisitorScope Scope { get; internal set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using FluentAssertions; | ||
using OpenSearch.Client; | ||
using OpenSearch.OpenSearch.Xunit.XunitPlumbing; | ||
|
||
namespace Tests.Mapping | ||
{ | ||
public class PropertyWalkerTests | ||
{ | ||
[U] | ||
public void BoolGetsMappedCorrectly() | ||
{ | ||
var result = TestPropertyType<bool>(); | ||
result.Should().Be("boolean"); | ||
} | ||
|
||
[U] | ||
public void StringGetsMappedCorrectly() | ||
{ | ||
var result = TestPropertyType<string>(); | ||
result.Should().Be("text"); | ||
} | ||
|
||
|
||
[U] | ||
public void DateTimeGetsMappedCorrectly() | ||
{ | ||
var result = TestPropertyType<DateTime>(); | ||
result.Should().Be("date"); | ||
} | ||
|
||
[U] | ||
public void IEnumerableOfNullableGetsMappedCorrectly() | ||
{ | ||
var result = TestPropertyType<IEnumerable<int?>>(); | ||
result.Should().Be("integer"); | ||
} | ||
|
||
[U] | ||
public void IListOfNullableGetsMappedCorrectly() | ||
{ | ||
var result = TestPropertyType<IList<int?>>(); | ||
result.Should().Be("integer"); | ||
} | ||
|
||
[U] | ||
public void IEnumerableOfValueTypesGetsMappedCorrectly() | ||
{ | ||
var result = TestPropertyType<IEnumerable<int>>(); | ||
result.Should().Be("integer"); | ||
} | ||
|
||
[U] | ||
public void IListOfValueTypesGetsMappedCorrectly() | ||
{ | ||
var result = TestPropertyType<IList<int>>(); | ||
result.Should().Be("integer"); | ||
} | ||
|
||
private static string TestPropertyType<T>() | ||
{ | ||
var walker = new PropertyWalker(typeof(Test<T>), null, 0); | ||
var properties = walker.GetProperties(); | ||
var result = properties.SingleOrDefault(); | ||
return result.Value?.Type; | ||
} | ||
|
||
|
||
private class Test<T> | ||
{ | ||
public T Values { get; set; } | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
using FluentAssertions; | ||
using OpenSearch.Client; | ||
using OpenSearch.OpenSearch.Xunit.XunitPlumbing; | ||
using Xunit; | ||
|
||
namespace Tests.QueryDsl.Container | ||
{ | ||
public class QueryContainerTests | ||
{ | ||
[TU] | ||
[InlineData(false, false)] | ||
[InlineData(false, true)] | ||
[InlineData(true, false)] | ||
[InlineData(true, true)] | ||
public void StrictAndVerbatimAttributesAreRecursivelySetCorrectly(bool targetStrict, bool targetVerbatim) | ||
{ | ||
// Arrange | ||
var query0 = new TermQuery { Field = "field", Value = 1, IsStrict = !targetStrict, IsVerbatim = !targetVerbatim }; | ||
var query1 = new BoolQuery { MustNot = new QueryContainer[] { query0 } }; | ||
var query2 = new TermQuery { Field = "field2", Value = 7, IsStrict = !targetStrict, IsVerbatim = !targetVerbatim }; | ||
var query3 = new BoolQuery { Must = new QueryContainer[] { query1, query2 } }; | ||
var queryContainer = new QueryContainer(query3); | ||
|
||
// Act | ||
queryContainer.Strict(targetStrict); | ||
queryContainer.Verbatim(targetVerbatim); | ||
|
||
// Assert | ||
query0.IsStrict.Should().Be(targetStrict); | ||
query0.IsVerbatim.Should().Be(targetVerbatim); | ||
query1.IsStrict.Should().Be(targetStrict); | ||
query1.IsVerbatim.Should().Be(targetVerbatim); | ||
query2.IsStrict.Should().Be(targetStrict); | ||
query2.IsVerbatim.Should().Be(targetVerbatim); | ||
query3.IsStrict.Should().Be(targetStrict); | ||
query3.IsVerbatim.Should().Be(targetVerbatim); | ||
} | ||
|
||
[TU] | ||
[InlineData("name1")] | ||
[InlineData("a name")] | ||
[InlineData(null)] | ||
public void SettingTheNameOnTheQueryContainerSetTheNameOnTheContainedQuery(string name) | ||
{ | ||
// Arrange | ||
var query0 = new TermQuery { Name = "a", Field = "field", Value = 1 }; | ||
var query1 = new BoolQuery { Name = "b", MustNot = new QueryContainer[] { query0 } }; | ||
var query2 = new TermQuery { Name = "c", Field = "field2", Value = 7 }; | ||
var query3 = new BoolQuery { Name = "d", Must = new QueryContainer[] { query1, query2 } }; | ||
var queryContainer = new QueryContainer(query3); | ||
|
||
// Act | ||
queryContainer.Name(name); | ||
|
||
// Assert | ||
query3.Name.Should().Be(name); | ||
queryContainer.ContainedQuery.Name.Should().Be(name); | ||
query0.Name.Should().Be("a"); | ||
query1.Name.Should().Be("b"); | ||
query2.Name.Should().Be("c"); | ||
} | ||
|
||
|
||
} | ||
} |