-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-5543: Add new options for Atlas Search Text and Phrase operators #1678
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* Copyright 2010-present MongoDB Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace MongoDB.Driver | ||
{ | ||
/// <summary> | ||
/// Represents the criteria used to match terms in a query for the Atlas Search Text operator. | ||
/// </summary> | ||
public enum MatchCriteria | ||
{ | ||
/// <summary> | ||
/// Match documents containing any of the terms from a query. | ||
/// </summary> | ||
Any, | ||
/// <summary> | ||
/// Match documents containing all the terms from a query. | ||
/// </summary> | ||
All | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All should be the first enum value to make it default. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -317,6 +317,7 @@ internal sealed class PhraseSearchDefinition<TDocument> : OperatorSearchDefiniti | |
{ | ||
private readonly SearchQueryDefinition _query; | ||
private readonly int? _slop; | ||
private readonly string _synonyms; | ||
|
||
public PhraseSearchDefinition( | ||
SearchPathDefinition<TDocument> path, | ||
|
@@ -329,11 +330,23 @@ public PhraseSearchDefinition( | |
_slop = slop; | ||
} | ||
|
||
public PhraseSearchDefinition( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need two ctors? This class is internal so it's safe to change. |
||
SearchPathDefinition<TDocument> path, | ||
SearchQueryDefinition query, | ||
SearchPhraseOptions<TDocument> options) | ||
: base(OperatorType.Phrase, path, options?.Score) | ||
{ | ||
_query = Ensure.IsNotNull(query, nameof(query)); | ||
_slop = options?.Slop; | ||
_synonyms = options?.Synonyms; | ||
} | ||
|
||
private protected override BsonDocument RenderArguments(RenderArgs<TDocument> args) => | ||
new() | ||
{ | ||
{ "query", _query.Render() }, | ||
{ "slop", _slop, _slop != null } | ||
{ "slop", _slop, _slop != null }, | ||
{ "synonyms", _synonyms, _synonyms != null } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we omit empty string also? |
||
}; | ||
} | ||
|
||
|
@@ -455,6 +468,7 @@ private protected override BsonDocument RenderArguments(RenderArgs<TDocument> ar | |
internal sealed class TextSearchDefinition<TDocument> : OperatorSearchDefinition<TDocument> | ||
{ | ||
private readonly SearchFuzzyOptions _fuzzy; | ||
private readonly MatchCriteria? _matchCriteria; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose it should not be nullable. |
||
private readonly SearchQueryDefinition _query; | ||
private readonly string _synonyms; | ||
|
||
|
@@ -471,12 +485,25 @@ public TextSearchDefinition( | |
_synonyms = synonyms; | ||
} | ||
|
||
public TextSearchDefinition( | ||
SearchPathDefinition<TDocument> path, | ||
SearchQueryDefinition query, | ||
SearchTextOptions<TDocument> options) | ||
: base(OperatorType.Text, path, options?.Score) | ||
{ | ||
_query = Ensure.IsNotNull(query, nameof(query)); | ||
_fuzzy = options?.Fuzzy; | ||
_synonyms = options?.Synonyms; | ||
_matchCriteria = options?.MatchCriteria; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
private protected override BsonDocument RenderArguments(RenderArgs<TDocument> args) => | ||
new() | ||
{ | ||
{ "query", _query.Render() }, | ||
{ "fuzzy", () => _fuzzy.Render(), _fuzzy != null }, | ||
{ "synonyms", _synonyms, _synonyms != null } | ||
{ "synonyms", _synonyms, _synonyms != null }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we omit empty string also? |
||
{ "matchCriteria", _matchCriteria == MatchCriteria.Any ? "any" : "all", _matchCriteria != null } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In most cases CSharp driver do not write default values into the wire, so the check should skip on writing "all":
Making
|
||
}; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* Copyright 2010-present MongoDB Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace MongoDB.Driver.Search | ||
{ | ||
/// <summary> | ||
/// Options for atlas search phrase operator. | ||
/// </summary> | ||
public sealed class SearchPhraseOptions<TDocument> | ||
{ | ||
/// <summary> | ||
/// The score modifier. | ||
/// </summary> | ||
public SearchScoreDefinition<TDocument> Score { get; set; } | ||
|
||
/// <summary> | ||
/// The allowable distance between words in the query phrase. | ||
/// </summary> | ||
public int? Slop { get; set; } | ||
|
||
/// <summary> | ||
/// The name of the synonym mapping definition in the index definition. Value can't be an empty string. | ||
/// </summary> | ||
public string Synonyms { get; set; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* Copyright 2010-present MongoDB Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace MongoDB.Driver.Search | ||
{ | ||
/// <summary> | ||
/// Options for atlas search text operator. | ||
/// </summary> | ||
public sealed class SearchTextOptions<TDocument> | ||
{ | ||
/// <summary> | ||
/// The options for fuzzy search. | ||
/// </summary> | ||
public SearchFuzzyOptions Fuzzy { get; set; } | ||
|
||
/// <summary> | ||
/// The criteria to use to match the terms in the query. Value can be either "any" or "all". | ||
/// Defaults to "all" if omitted. | ||
/// </summary> | ||
public MatchCriteria MatchCriteria { get; set; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nullable? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would say we do not need the property be nullable. It should simply have the default value of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The property should default to "all", but the default value of the enum is Any. Need to change enum to have All as default value. |
||
|
||
/// <summary> | ||
/// The score modifier. | ||
/// </summary> | ||
public SearchScoreDefinition<TDocument> Score { get; set; } | ||
|
||
/// <summary> | ||
/// The name of the synonym mapping definition in the index definition. Value can't be an empty string. | ||
/// </summary> | ||
public string Synonyms { get; set; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. xml-docs says: |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -770,10 +770,22 @@ public void Phrase() | |
subject.Phrase("x", "foo", 5), | ||
"{ phrase: { query: 'foo', path: 'x', slop: 5 } }"); | ||
|
||
AssertRendered( | ||
subject.Phrase("x", "foo", new SearchPhraseOptions<BsonDocument> { Synonyms = "testSynonyms" }), | ||
"{ phrase: { query: 'foo', path: 'x', synonyms: 'testSynonyms' } }"); | ||
|
||
AssertRendered( | ||
subject.Phrase("x", "foo", 5), | ||
"{ phrase: { query: 'foo', path: 'x', slop: 5 } }"); | ||
|
||
var scoreBuilder = new SearchScoreDefinitionBuilder<BsonDocument>(); | ||
AssertRendered( | ||
subject.Phrase("x", "foo", score: scoreBuilder.Constant(1)), | ||
"{ phrase: { query: 'foo', path: 'x', score: { constant: { value: 1 } } } }"); | ||
|
||
AssertRendered( | ||
subject.Phrase("x", "foo", new SearchPhraseOptions<BsonDocument> { Score = scoreBuilder.Constant(1), Slop = 5}), | ||
"{ phrase: { query: 'foo', slop: 5, path: 'x', score: { constant: { value: 1 } } } }"); | ||
} | ||
|
||
[Fact] | ||
|
@@ -791,6 +803,10 @@ public void Phrase_typed() | |
subject.Phrase(x => x.Hobbies, "foo"), | ||
"{ phrase: { query: 'foo', path: 'hobbies' } }"); | ||
|
||
AssertRendered( | ||
subject.Phrase(x => x.FirstName, "foo", new SearchPhraseOptions<Person> { Synonyms = "testSynonyms" }), | ||
"{ phrase: { query: 'foo', synonyms: 'testSynonyms', path: 'fn' } }"); | ||
|
||
AssertRendered( | ||
subject.Phrase( | ||
new FieldDefinition<Person>[] | ||
|
@@ -1076,6 +1092,10 @@ public void Text() | |
subject.Text(new[] { "x", "y" }, new[] { "foo", "bar" }, "testSynonyms"), | ||
"{ text: { query: ['foo', 'bar'], synonyms: 'testSynonyms', path: ['x', 'y'] } }"); | ||
|
||
AssertRendered( | ||
subject.Text(new[] { "x", "y" }, new[] { "foo", "bar" }, new SearchTextOptions<BsonDocument>{ MatchCriteria = MatchCriteria.Any }), | ||
"{ text: { query: ['foo', 'bar'], matchCriteria: 'any', path: ['x', 'y'] } }"); | ||
|
||
AssertRendered( | ||
subject.Text("x", "foo", new SearchFuzzyOptions()), | ||
"{ text: { query: 'foo', path: 'x', fuzzy: {} } }"); | ||
|
@@ -1095,6 +1115,10 @@ public void Text() | |
AssertRendered( | ||
subject.Text("x", "foo", "testSynonyms", scoreBuilder.Constant(1)), | ||
"{ text: { query: 'foo', synonyms: 'testSynonyms', path: 'x', score: { constant: { value: 1 } } } }"); | ||
|
||
AssertRendered( | ||
subject.Text("x", "foo", new SearchTextOptions<BsonDocument> {Score = scoreBuilder.Constant(1), MatchCriteria = MatchCriteria.All}), | ||
"{ text: { query: 'foo', matchCriteria: 'all', path: 'x', score: { constant: { value: 1 } } } }"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we skip on writing the default |
||
} | ||
|
||
[Fact] | ||
|
@@ -1105,6 +1129,9 @@ public void Text_typed() | |
AssertRendered( | ||
subject.Text(x => x.FirstName, "foo"), | ||
"{ text: { query: 'foo', path: 'fn' } }"); | ||
AssertRendered( | ||
subject.Text(x => x.FirstName, "foo", new SearchTextOptions<Person> { MatchCriteria = MatchCriteria.All}), | ||
"{ text: { query: 'foo', matchCriteria: 'all', path: 'fn' } }"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same with skipping |
||
AssertRendered( | ||
subject.Text("FirstName", "foo"), | ||
"{ text: { query: 'foo', path: 'fn' } }"); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should in Search folder (and namespace).