Sep 25, 2025
Create or update or delete composition rules
Create or update or delete multiple composition rules.
Usage
Required ACL:
editSettings
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using Algolia.Search.Clients;
using Algolia.Search.Http;
using Algolia.Search.Models.Composition;
var client = new CompositionClient(
new CompositionConfig("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY")
);
var response = await client.SaveRulesAsync(
"rule-with-exernal-source",
new CompositionRulesBatchParams
{
Requests = new List<RulesMultipleBatchRequest>
{
new RulesMultipleBatchRequest
{
Action = Enum.Parse<Action>("Upsert"),
Body = new RulesBatchCompositionAction(
new CompositionRule
{
ObjectID = "rule-with-exernal-source",
Description = "my description",
Tags = new List<string> { "tag1", "tag2" },
Enabled = true,
Validity = new List<TimeRange>
{
new TimeRange { From = 1704063600L, Until = 1704083600L },
},
Conditions = new List<Condition>
{
new Condition
{
Anchoring = Enum.Parse<Anchoring>("Contains"),
Pattern = "harry",
},
new Condition
{
Anchoring = Enum.Parse<Anchoring>("Contains"),
Pattern = "potter",
},
},
Consequence = new CompositionRuleConsequence
{
Behavior = new CompositionBehavior
{
Injection = new Injection
{
Main = new Main
{
Source = new CompositionSource
{
Search = new CompositionSourceSearch
{
Index = "my-index",
Params = new MainInjectionQueryParameters { Filters = "brand:adidas" },
},
},
},
InjectedItems = new List<InjectedItem>
{
new InjectedItem
{
Key = "injectedItem",
Source = new InjectedItemSource(
new ExternalSource
{
External = new External
{
Index = "my-index",
Params = new BaseInjectionQueryParameters
{
Filters = "brand:adidas",
},
Ordering = Enum.Parse<ExternalOrdering>("UserDefined"),
},
}
),
Position = 0,
Length = 3,
},
},
},
},
},
}
),
},
},
}
);