You're viewing an archived version of our docs. Check out our current documentation →

Upsert a composition rule

Upsert a Composition Rule for the specified composition ID.

Usage

Required ACL: editSettings
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
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.PutCompositionRuleAsync(
  "compositionID",
  "rule-with-exernal-source",
  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,
            },
          },
        },
      },
    },
  }
);