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(...)
, `.Verbatim(...…
…)`, `.Name(...)` methods on `QueryContainer` to help modify contained query attributes. Signed-off-by: Kostas <ghost0002001@hotmail.com>
- Loading branch information
1 parent
afa7c7c
commit 478d7e8
Showing
5 changed files
with
204 additions
and
4 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
24 changes: 24 additions & 0 deletions
24
src/OpenSearch.Client/QueryDsl/Visitor/StrictnessPropagatingVisitor.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,24 @@ | ||
/* 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 | ||
{ | ||
public class StrictnessPropagatingVisitor : QueryVisitor | ||
{ | ||
private readonly bool _strict; | ||
|
||
public StrictnessPropagatingVisitor(bool strict) => _strict = strict; | ||
|
||
public override void Visit(IQuery query) | ||
{ | ||
query.IsStrict = _strict; | ||
base.Visit(query); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/OpenSearch.Client/QueryDsl/Visitor/VerbatimPropagatingVisitor.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,22 @@ | ||
/* 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 | ||
{ | ||
public class VerbatimPropagatingVisitor : QueryVisitor | ||
{ | ||
private readonly bool _verbatim; | ||
|
||
public VerbatimPropagatingVisitor(bool verbatim) => _verbatim = verbatim; | ||
|
||
public override void Visit(IQuery query) | ||
{ | ||
query.IsVerbatim = _verbatim; | ||
base.Visit(query); | ||
} | ||
} | ||
} |
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,102 @@ | ||
/* 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 }, IsStrict = !targetStrict, IsVerbatim = !targetVerbatim }; | ||
var query2 = new TermQuery { Field = "field2", Value = 7, IsStrict = !targetStrict, IsVerbatim = !targetVerbatim }; | ||
var query3 = new BoolQuery { Must = new QueryContainer[] { query1, query2 }, IsStrict = !targetStrict, IsVerbatim = !targetVerbatim }; | ||
var queryContainer = new QueryContainer(query3); | ||
|
||
// Act | ||
queryContainer.Strict(targetStrict, recurse: true); | ||
queryContainer.Verbatim(targetVerbatim, recurse: true); | ||
|
||
// 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(false, false)] | ||
[InlineData(false, true)] | ||
[InlineData(true, false)] | ||
[InlineData(true, true)] | ||
public void StrictAndVerbatimAttributesAreSetCorrectly(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 }, IsStrict = !targetStrict, IsVerbatim = !targetVerbatim }; | ||
var query2 = new TermQuery { Field = "field2", Value = 7, IsStrict = !targetStrict, IsVerbatim = !targetVerbatim }; | ||
var query3 = new BoolQuery { Must = new QueryContainer[] { query1, query2 }, IsStrict = !targetStrict, IsVerbatim = !targetVerbatim }; | ||
var queryContainer = new QueryContainer(query3); | ||
|
||
// Act | ||
queryContainer.Strict(targetStrict, recurse: false); | ||
queryContainer.Verbatim(targetVerbatim, recurse: false); | ||
|
||
// 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"); | ||
} | ||
|
||
|
||
} | ||
} |