Skip to content

Commit

Permalink
feat(indexing): built-in operations 'IncrementFrom' and 'IncrementSet'
Browse files Browse the repository at this point in the history
  • Loading branch information
spinach committed Aug 17, 2020
1 parent a86bf1b commit 5f2055b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Algolia.Search.Test/Serializer/SerializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,34 @@ public void TestPartialUpdateOperation_Increment()
Assert.AreEqual(json, "{\"objectID\":\"myID\",\"update\":{\"_operation\":\"Increment\",\"value\":2}}");
}

[Test]
[Parallelizable]
public void TestPartialUpdateOperation_IncrementFrom()
{
RecordWithPartialUpdateOperation<int> record = new RecordWithPartialUpdateOperation<int>
{
ObjectID = "myID",
Update = PartialUpdateOperation<int>.IncrementFrom(2),
};

string json = JsonConvert.SerializeObject(record, JsonConfig.AlgoliaJsonSerializerSettings);
Assert.AreEqual(json, "{\"objectID\":\"myID\",\"update\":{\"_operation\":\"IncrementFrom\",\"value\":2}}");
}

[Test]
[Parallelizable]
public void TestPartialUpdateOperation_IncrementSet()
{
RecordWithPartialUpdateOperation<int> record = new RecordWithPartialUpdateOperation<int>
{
ObjectID = "myID",
Update = PartialUpdateOperation<int>.IncrementSet(2),
};

string json = JsonConvert.SerializeObject(record, JsonConfig.AlgoliaJsonSerializerSettings);
Assert.AreEqual(json, "{\"objectID\":\"myID\",\"update\":{\"_operation\":\"IncrementSet\",\"value\":2}}");
}

[Test]
[Parallelizable]
public void TestPartialUpdateOperation_Decrement()
Expand Down
10 changes: 10 additions & 0 deletions src/Algolia.Search/Models/Enums/PartialUpdateOperationType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ public class PartialUpdateOperationType
/// </summary>
public const string Increment = "Increment";

/// <summary>
/// Increment a numeric integer attribute only if the provided value matches the current value, and otherwise ignores the whole object update
/// </summary>
public const string IncrementFrom = "IncrementFrom";

/// <summary>
/// Increment a numeric integer attribute only if the provided value is greater than the current value, and otherwise ignore the whole object update
/// </summary>
public const string IncrementSet = "IncrementSet";

/// <summary>
/// Decrement by an integer value
/// </summary>
Expand Down
28 changes: 28 additions & 0 deletions src/Algolia.Search/Models/Search/PartialUpdateOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,34 @@ public static PartialUpdateOperation<int> Increment(int value)
};
}

/// <summary>
/// Increment a numeric integer attribute only if the provided value matches the current value, and otherwise ignores the whole object update
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static PartialUpdateOperation<int> IncrementFrom(int value)
{
return new PartialUpdateOperation<int>
{
Operation = PartialUpdateOperationType.IncrementFrom,
Value = value,
};
}

/// <summary>
/// Increment a numeric integer attribute only if the provided value is greater than the current value, and otherwise ignore the whole object update
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static PartialUpdateOperation<int> IncrementSet(int value)
{
return new PartialUpdateOperation<int>
{
Operation = PartialUpdateOperationType.IncrementSet,
Value = value,
};
}

/// <summary>
/// Decrement by an integer value
/// </summary>
Expand Down

0 comments on commit 5f2055b

Please sign in to comment.