Skip to content

Commit 377b4be

Browse files
committed
- PR comments
1 parent cebe0b6 commit 377b4be

File tree

4 files changed

+62
-16
lines changed

4 files changed

+62
-16
lines changed

src/MongoDB.Driver/Search/OperatorSearchDefinitions.cs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -176,18 +176,30 @@ private protected override BsonDocument RenderArguments(IBsonSerializer<TDocumen
176176
new(_area.Render());
177177
}
178178

179-
internal sealed class MoreLikeThisSearchDefinition<TDocument> : OperatorSearchDefinition<TDocument>
179+
internal sealed class MoreLikeThisSearchDefinition<TDocumentCollection, TDocumentLike> : OperatorSearchDefinition<TDocumentCollection>
180180
{
181-
private readonly TDocument[] _like;
181+
private readonly TDocumentLike[] _like;
182182

183-
public MoreLikeThisSearchDefinition(IEnumerable<TDocument> like)
183+
public MoreLikeThisSearchDefinition(IEnumerable<TDocumentLike> like)
184184
: base(OperatorType.MoreLikeThis)
185185
{
186+
if (typeof(TDocumentLike) != typeof(BsonDocument) &&
187+
typeof(TDocumentLike) != typeof(TDocumentCollection))
188+
{
189+
throw new ArgumentOutOfRangeException($"Only {nameof(BsonDocument)} and {nameof(TDocumentCollection)} are supported for {nameof(TDocumentLike)} type.");
190+
}
191+
186192
_like = Ensure.IsNotNull(like, nameof(like)).ToArray();
187193
}
188194

189-
private protected override BsonDocument RenderArguments(IBsonSerializer<TDocument> documentSerializer, IBsonSerializerRegistry serializerRegistry) =>
190-
new("like", new BsonArray(_like.Select(e => e.ToBsonDocument(documentSerializer))));
195+
private protected override BsonDocument RenderArguments(IBsonSerializer<TDocumentCollection> documentSerializer, IBsonSerializerRegistry serializerRegistry)
196+
{
197+
var likeArray = typeof(TDocumentLike) == typeof(BsonDocument) ?
198+
new BsonArray(_like.OfType<BsonDocument>()) :
199+
new BsonArray(_like.OfType<TDocumentCollection>().Select(e => e.ToBsonDocument(documentSerializer)));
200+
201+
return new("like", likeArray);
202+
}
191203
}
192204

193205
internal sealed class NearSearchDefinition<TDocument> : OperatorSearchDefinition<TDocument>
@@ -282,15 +294,16 @@ private protected override BsonDocument RenderArguments(IBsonSerializer<TDocumen
282294
private static BsonValue ToBsonValue(TField value) =>
283295
value switch
284296
{
285-
sbyte v => v,
286-
byte v => v,
287-
short v => v,
288-
ushort v => v,
289-
int v => v,
290-
uint v => v,
291-
long v => v,
292-
double v => v,
293-
DateTime v => v,
297+
sbyte v => (BsonInt32)v,
298+
byte v => (BsonInt32)v,
299+
short v => (BsonInt32)v,
300+
ushort v => (BsonInt32)v,
301+
int v => (BsonInt32)v,
302+
uint v => (BsonInt32)v,
303+
long v => (BsonInt64)v,
304+
float v => (BsonDouble)v,
305+
double v => (BsonDouble)v,
306+
DateTime v => (BsonDateTime)v,
294307
_ => throw new InvalidCastException()
295308
};
296309
}

src/MongoDB.Driver/Search/SearchDefinitionBuilder.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,17 @@ public SearchDefinition<TDocument> GeoWithin<TCoordinates>(
294294
/// </param>
295295
/// <returns>A more like this search definition.</returns>
296296
public SearchDefinition<TDocument> MoreLikeThis(IEnumerable<TDocument> like) =>
297-
new MoreLikeThisSearchDefinition<TDocument>(like);
297+
new MoreLikeThisSearchDefinition<TDocument, TDocument>(like);
298+
299+
/// <summary>
300+
/// Creates a search definition that returns documents similar to the input documents.
301+
/// </summary>
302+
/// <param name="like">
303+
/// One or more documents that Atlas Search uses to extract representative terms for.
304+
/// </param>
305+
/// <returns>A more like this search definition.</returns>
306+
public SearchDefinition<TDocument> MoreLikeThis(IEnumerable<BsonDocument> like) =>
307+
new MoreLikeThisSearchDefinition<TDocument, BsonDocument>(like);
298308

299309
/// <summary>
300310
/// Creates a search definition that returns documents similar to the input documents.
@@ -306,6 +316,16 @@ public SearchDefinition<TDocument> MoreLikeThis(IEnumerable<TDocument> like) =>
306316
public SearchDefinition<TDocument> MoreLikeThis(params TDocument[] like) =>
307317
MoreLikeThis((IEnumerable<TDocument>)like);
308318

319+
/// <summary>
320+
/// Creates a search definition that returns documents similar to the input documents.
321+
/// </summary>
322+
/// <param name="like">
323+
/// One or more documents that Atlas Search uses to extract representative terms for.
324+
/// </param>
325+
/// <returns>A more like this search definition.</returns>
326+
public SearchDefinition<TDocument> MoreLikeThis(params BsonDocument[] like) =>
327+
MoreLikeThis((IEnumerable<BsonDocument>)like);
328+
309329
/// <summary>
310330
/// Creates a search definition that supports querying and scoring numeric and date values.
311331
/// </summary>

tests/MongoDB.Driver.Tests/Search/MongoQueryableTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public void SearchMeta()
3939
var subject = CreateSubject();
4040

4141
var query = subject
42-
.Where(x => x.FirstName == "Alexandra")
4342
.SearchMeta(Builders<Person>.Search.Text(x => x.FirstName, "Alex"));
4443

4544
query.ToString().Should().EndWith("Aggregate([{ \"$searchMeta\" : { \"text\" : { \"query\" : \"Alex\", \"path\" : \"fn\" } } }])");

tests/MongoDB.Driver.Tests/Search/SearchDefinitionBuilderTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,20 @@ public void MoreLikeThis_typed()
394394
LastName = "Doe"
395395
}),
396396
"{ moreLikeThis: { like: [{ fn: 'John', ln: 'Doe' }, { fn: 'Jane', ln: 'Doe' }] } }");
397+
398+
AssertRendered(
399+
subject.MoreLikeThis(
400+
new BsonDocument
401+
{
402+
{ "fn", "John" },
403+
{ "ln", "Doe" },
404+
},
405+
new BsonDocument
406+
{
407+
{ "fn", "Jane" },
408+
{ "ln", "Doe" },
409+
}),
410+
"{ moreLikeThis: { like: [{ fn: 'John', ln: 'Doe' }, { fn: 'Jane', ln: 'Doe' }] } }");
397411
}
398412

399413
[Fact]

0 commit comments

Comments
 (0)