Skip to main content
PUT
/
1
/
compositions
/
{compositionID}
/
rules
/
{objectID}
curl
curl --request PUT \
  --url https://algolia_application_id.algolia.net/1/compositions/my_composition_object_id/rules/lorem \
  --header 'accept: application/json' \
  --header 'content-type: application/json' \
  --header 'x-algolia-api-key: ALGOLIA_API_KEY' \
  --header 'x-algolia-application-id: ALGOLIA_APPLICATION_ID' \
  --data '
{
  "objectID": "my_composition_rule_object_id",
  "conditions": [
    {
      "pattern": "{facet:genre}",
      "anchoring": "is",
      "context": "mobile",
      "filters": "genre:comedy",
      "sortBy": "price-low-to-high"
    }
  ],
  "consequence": {
    "behavior": {
      "injection": {
        "main": {
          "source": {
            "search": {
              "index": "Products",
              "params": {
                "advancedSyntax": false,
                "advancedSyntaxFeatures": [
                  "exactPhrase",
                  "excludeWords"
                ],
                "allowTyposOnNumericTokens": true,
                "alternativesAsExact": [
                  "ignorePlurals",
                  "singleWordSynonym"
                ],
                "attributeCriteriaComputedByMinProximity": false,
                "attributesToHighlight": [
                  "author",
                  "title",
                  "conten",
                  "content"
                ],
                "attributesToRetrieve": [
                  "author",
                  "title",
                  "content"
                ],
                "attributesToSnippet": [
                  "content:80",
                  "description"
                ],
                "clickAnalytics": false,
                "disableExactOnAttributes": [
                  "description"
                ],
                "disableTypoToleranceOnAttributes": [
                  "sku"
                ],
                "distinct": 1,
                "enableABTest": true,
                "enablePersonalization": false,
                "enableReRanking": true,
                "enableRules": true,
                "exactOnSingleWordQuery": "attribute",
                "facetFilters": [
                  [
                    "category:Book",
                    "category:-Movie"
                  ],
                  "author:John Doe"
                ],
                "filters": "(category:Book OR category:Ebook) AND _tags:published",
                "getRankingInfo": false,
                "highlightPostTag": "</em>",
                "highlightPreTag": "<em>",
                "ignorePlurals": [
                  "ca",
                  "es"
                ],
                "minProximity": 1,
                "minWordSizefor1Typo": 4,
                "minWordSizefor2Typos": 8,
                "naturalLanguages": [],
                "numericFilters": [
                  [
                    "inStock = 1",
                    "deliveryDate < 1441755506"
                  ],
                  "price < 1000"
                ],
                "optionalFilters": [
                  "category:Book",
                  "author:John Doe"
                ],
                "optionalWords": "lorem",
                "percentileComputation": true,
                "personalizationImpact": 100,
                "queryLanguages": [
                  "es"
                ],
                "queryType": "prefixLast",
                "removeStopWords": [
                  "ca",
                  "es"
                ],
                "removeWordsIfNoResults": "firstWords",
                "replaceSynonymsInHighlight": false,
                "responseFields": [
                  "*"
                ],
                "restrictHighlightAndSnippetArrays": false,
                "restrictSearchableAttributes": [
                  "title",
                  "author"
                ],
                "ruleContexts": [
                  "mobile"
                ],
                "snippetEllipsisText": "…",
                "synonyms": true,
                "typoTolerance": true,
                "facetingAfterDistinct": false,
                "facets": [
                  "category",
                  "disjunctive(brand)",
                  "price"
                ],
                "hitsPerPage": 20,
                "maxValuesPerFacet": 100,
                "renderingContent": {
                  "facetOrdering": {
                    "facets": {
                      "order": [
                        "lorem"
                      ]
                    },
                    "values": {
                      "property1": {
                        "order": [
                          "lorem"
                        ],
                        "sortRemainingBy": "count",
                        "hide": [
                          "lorem"
                        ]
                      },
                      "property2": {
                        "order": [
                          "lorem"
                        ],
                        "sortRemainingBy": "count",
                        "hide": [
                          "lorem"
                        ]
                      }
                    }
                  },
                  "redirect": {
                    "url": "lorem"
                  },
                  "widgets": {
                    "banners": [
                      {
                        "image": {
                          "urls": [
                            {
                              "url": "lorem"
                            }
                          ],
                          "title": "lorem"
                        },
                        "link": {
                          "url": "lorem"
                        }
                      }
                    ]
                  }
                },
                "sortFacetValuesBy": "count",
                "sumOrFiltersScores": false
              }
            }
          }
        },
        "injectedItems": [
          {
            "key": "lorem",
            "source": {
              "search": {
                "index": "Products",
                "params": {
                  "advancedSyntax": false,
                  "advancedSyntaxFeatures": [
                    "exactPhrase",
                    "excludeWords"
                  ],
                  "allowTyposOnNumericTokens": true,
                  "alternativesAsExact": [
                    "ignorePlurals",
                    "singleWordSynonym"
                  ],
                  "attributeCriteriaComputedByMinProximity": false,
                  "attributesToHighlight": [
                    "author",
                    "title",
                    "conten",
                    "content"
                  ],
                  "attributesToRetrieve": [
                    "author",
                    "title",
                    "content"
                  ],
                  "attributesToSnippet": [
                    "content:80",
                    "description"
                  ],
                  "clickAnalytics": false,
                  "disableExactOnAttributes": [
                    "description"
                  ],
                  "disableTypoToleranceOnAttributes": [
                    "sku"
                  ],
                  "distinct": 1,
                  "enableABTest": true,
                  "enablePersonalization": false,
                  "enableReRanking": true,
                  "enableRules": true,
                  "exactOnSingleWordQuery": "attribute",
                  "facetFilters": [
                    [
                      "category:Book",
                      "category:-Movie"
                    ],
                    "author:John Doe"
                  ],
                  "filters": "(category:Book OR category:Ebook) AND _tags:published",
                  "getRankingInfo": false,
                  "highlightPostTag": "</em>",
                  "highlightPreTag": "<em>",
                  "ignorePlurals": [
                    "ca",
                    "es"
                  ],
                  "minProximity": 1,
                  "minWordSizefor1Typo": 4,
                  "minWordSizefor2Typos": 8,
                  "naturalLanguages": [],
                  "numericFilters": [
                    [
                      "inStock = 1",
                      "deliveryDate < 1441755506"
                    ],
                    "price < 1000"
                  ],
                  "optionalFilters": [
                    "category:Book",
                    "author:John Doe"
                  ],
                  "optionalWords": "lorem",
                  "percentileComputation": true,
                  "personalizationImpact": 100,
                  "queryLanguages": [
                    "es"
                  ],
                  "queryType": "prefixLast",
                  "removeStopWords": [
                    "ca",
                    "es"
                  ],
                  "removeWordsIfNoResults": "firstWords",
                  "replaceSynonymsInHighlight": false,
                  "responseFields": [
                    "*"
                  ],
                  "restrictHighlightAndSnippetArrays": false,
                  "restrictSearchableAttributes": [
                    "title",
                    "author"
                  ],
                  "ruleContexts": [
                    "mobile"
                  ],
                  "snippetEllipsisText": "…",
                  "synonyms": true,
                  "typoTolerance": true
                }
              }
            },
            "position": 0,
            "length": 0,
            "metadata": {
              "hits": {
                "addItemKey": true,
                "extra": {}
              }
            }
          }
        ],
        "deduplication": {
          "positioning": "highest"
        }
      }
    }
  },
  "description": "Display a promotional banner",
  "enabled": true,
  "validity": [
    {
      "from": 42,
      "until": 42
    }
  ],
  "tags": [
    "conditional"
  ]
}
'
// Initialize the client
var client = new CompositionClient(
new CompositionConfig("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY")
);

// Call the API
var response = await client.PutCompositionRuleAsync(
"compositionID",
"rule-with-deduplication",
new CompositionRule
{
ObjectID = "rule-with-deduplication",
Description = "my description",
Enabled = true,
Conditions = new List<Condition>
{
new Condition { Anchoring = Enum.Parse<Anchoring>("Contains"), Pattern = "harry" },
},
Consequence = new CompositionRuleConsequence
{
Behavior = new CompositionBehavior(
new CompositionInjectionBehavior
{
Injection = new Injection
{
Main = new InjectionMain
{
Source = new InjectionMainSource(
new InjectionMainSearchSource { Search = new MainSearch { Index = "my-index" } }
),
},
InjectedItems = new List<InjectionInjectedItem>
{
new InjectionInjectedItem
{
Key = "my-unique-injected-item-key",
Source = new InjectedItemSource(
new InjectedItemSearchSource
{
Search = new InjectedItemSearch { Index = "my-index" },
}
),
Position = 0,
Length = 3,
},
},
Deduplication = new Deduplication
{
Positioning = Enum.Parse<DedupPositioning>("HighestInjected"),
},
},
}
),
},
}
);

// print the response
Console.WriteLine(response);
// Initialize the client
final client = CompositionClient(
appId: 'ALGOLIA_APPLICATION_ID', apiKey: 'ALGOLIA_API_KEY');

// Call the API
final response = await client.putCompositionRule(
compositionID: "compositionID",
objectID: "rule-with-deduplication",
compositionRule: CompositionRule(
objectID: "rule-with-deduplication",
description: "my description",
enabled: true,
conditions: [
Condition(
anchoring: Anchoring.fromJson("contains"),
pattern: "harry",
),
],
consequence: CompositionRuleConsequence(
behavior: CompositionInjectionBehavior(
injection: Injection(
main: InjectionMain(
source: InjectionMainSearchSource(
search: MainSearch(
index: "my-index",
),
),
),
injectedItems: [
InjectionInjectedItem(
key: "my-unique-injected-item-key",
source: InjectedItemSearchSource(
search: InjectedItemSearch(
index: "my-index",
),
),
position: 0,
length: 3,
),
],
deduplication: Deduplication(
positioning: DedupPositioning.fromJson("highestInjected"),
),
),
),
),
),
);

// print the response
print(response);
// Initialize the client
client, err := composition.NewClient("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY")
if err != nil {
// The client can fail to initialize if you pass an invalid parameter.
panic(err)
}

// Call the API
response, err := client.PutCompositionRule(client.NewApiPutCompositionRuleRequest(
"compositionID", "rule-with-deduplication",
composition.NewEmptyCompositionRule().SetObjectID("rule-with-deduplication").SetDescription("my description").SetEnabled(true).SetConditions(
[]composition.Condition{
*composition.NewEmptyCondition().SetAnchoring(composition.Anchoring("contains")).SetPattern("harry"),
}).
SetConsequence(
composition.NewEmptyCompositionRuleConsequence().SetBehavior(composition.CompositionInjectionBehaviorAsCompositionBehavior(
composition.NewEmptyCompositionInjectionBehavior().SetInjection(
composition.NewEmptyInjection().SetMain(
composition.NewEmptyInjectionMain().SetSource(composition.InjectionMainSearchSourceAsInjectionMainSource(
composition.NewEmptyInjectionMainSearchSource().SetSearch(
composition.NewEmptyMainSearch().SetIndex("my-index"))))).SetInjectedItems(
[]composition.InjectionInjectedItem{
*composition.NewEmptyInjectionInjectedItem().SetKey("my-unique-injected-item-key").SetSource(composition.InjectedItemSearchSourceAsInjectedItemSource(
composition.NewEmptyInjectedItemSearchSource().SetSearch(
composition.NewEmptyInjectedItemSearch().SetIndex("my-index")))).SetPosition(0).SetLength(3),
}).
SetDeduplication(
composition.NewEmptyDeduplication().SetPositioning(composition.DedupPositioning("highestInjected")))))))))
if err != nil {
// handle the eventual error
panic(err)
}


// print the response
print(response)
// Initialize the client
CompositionClient client = new CompositionClient("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY");

// Call the API
TaskIDResponse response = client.putCompositionRule(
"compositionID",
"rule-with-deduplication",
new CompositionRule()
.setObjectID("rule-with-deduplication")
.setDescription("my description")
.setEnabled(true)
.setConditions(Arrays.asList(new Condition().setAnchoring(Anchoring.CONTAINS).setPattern("harry")))
.setConsequence(
new CompositionRuleConsequence().setBehavior(
new CompositionInjectionBehavior().setInjection(
new Injection()
.setMain(new InjectionMain().setSource(new InjectionMainSearchSource().setSearch(new MainSearch().setIndex("my-index"))))
.setInjectedItems(
Arrays.asList(
new InjectionInjectedItem()
.setKey("my-unique-injected-item-key")
.setSource(new InjectedItemSearchSource().setSearch(new InjectedItemSearch().setIndex("my-index")))
.setPosition(0)
.setLength(3)
)
)
.setDeduplication(new Deduplication().setPositioning(DedupPositioning.HIGHEST_INJECTED))
)
)
)
);

// print the response
System.out.println(response);
// Initialize the client
const client = compositionClient('ALGOLIA_APPLICATION_ID', 'ALGOLIA_API_KEY');

// Call the API
const response = await client.putCompositionRule({
compositionID: 'compositionID',
objectID: 'rule-with-deduplication',
compositionRule: {
objectID: 'rule-with-deduplication',
description: 'my description',
enabled: true,
conditions: [{ anchoring: 'contains', pattern: 'harry' }],
consequence: {
behavior: {
injection: {
main: { source: { search: { index: 'my-index' } } },
injectedItems: [
{ key: 'my-unique-injected-item-key', source: { search: { index: 'my-index' } }, position: 0, length: 3 },
],
deduplication: { positioning: 'highestInjected' },
},
},
},
},
});


// print the response
console.log(response);
// Initialize the client
val client = CompositionClient(appId = "ALGOLIA_APPLICATION_ID", apiKey = "ALGOLIA_API_KEY")

// Call the API
var response =
client.putCompositionRule(
compositionID = "compositionID",
objectID = "rule-with-deduplication",
compositionRule =
CompositionRule(
objectID = "rule-with-deduplication",
description = "my description",
enabled = true,
conditions =
listOf(
Condition(
anchoring = Anchoring.entries.first { it.value == "contains" },
pattern = "harry",
)
),
consequence =
CompositionRuleConsequence(
behavior =
CompositionInjectionBehavior(
injection =
Injection(
main =
InjectionMain(
source =
InjectionMainSearchSource(search = MainSearch(index = "my-index"))
),
injectedItems =
listOf(
InjectionInjectedItem(
key = "my-unique-injected-item-key",
source =
InjectedItemSearchSource(
search = InjectedItemSearch(index = "my-index")
),
position = 0,
length = 3,
)
),
deduplication =
Deduplication(
positioning =
DedupPositioning.entries.first { it.value == "highestInjected" }
),
)
)
),
),
)


// print the response
println(response)
// Initialize the client
$client = CompositionClient::create('ALGOLIA_APPLICATION_ID', 'ALGOLIA_API_KEY');

// Call the API
$response = $client->putCompositionRule(
'compositionID',
'rule-with-deduplication',
['objectID' => 'rule-with-deduplication',
'description' => 'my description',
'enabled' => true,
'conditions' => [
['anchoring' => 'contains',
'pattern' => 'harry',
],
],
'consequence' => ['behavior' => ['injection' => ['main' => ['source' => ['search' => ['index' => 'my-index',
],
],
],
'injectedItems' => [
['key' => 'my-unique-injected-item-key',
'source' => ['search' => ['index' => 'my-index',
],
],
'position' => 0,
'length' => 3,
],
],
'deduplication' => ['positioning' => 'highestInjected',
],
],
],
],
],
);


// print the response
var_dump($response);
# Initialize the client
# In an asynchronous context, you can use CompositionClient instead, which exposes the exact same methods.
client = CompositionClientSync("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY")

# Call the API
response = client.put_composition_rule(
composition_id="compositionID",
object_id="rule-with-deduplication",
composition_rule={
"objectID": "rule-with-deduplication",
"description": "my description",
"enabled": True,
"conditions": [
{
"anchoring": "contains",
"pattern": "harry",
},
],
"consequence": {
"behavior": {
"injection": {
"main": {
"source": {
"search": {
"index": "my-index",
},
},
},
"injectedItems": [
{
"key": "my-unique-injected-item-key",
"source": {
"search": {
"index": "my-index",
},
},
"position": 0,
"length": 3,
},
],
"deduplication": {
"positioning": "highestInjected",
},
},
},
},
},
)


# print the response
print(response)
# Initialize the client
client = Algolia::CompositionClient.create("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY")

# Call the API
response = client.put_composition_rule(
"compositionID",
"rule-with-deduplication",
Algolia::Composition::CompositionRule.new(
algolia_object_id: "rule-with-deduplication",
description: "my description",
enabled: true,
conditions: [Algolia::Composition::Condition.new(anchoring: "contains", pattern: "harry")],
consequence: Algolia::Composition::CompositionRuleConsequence.new(
behavior: Algolia::Composition::CompositionInjectionBehavior.new(
injection: Algolia::Composition::Injection.new(
main: Algolia::Composition::InjectionMain.new(
source: Algolia::Composition::InjectionMainSearchSource.new(
search: Algolia::Composition::MainSearch.new(index: "my-index")
)
),
injected_items: [
Algolia::Composition::InjectionInjectedItem.new(
key: "my-unique-injected-item-key",
source: Algolia::Composition::InjectedItemSearchSource.new(
search: Algolia::Composition::InjectedItemSearch.new(index: "my-index")
),
position: 0,
length: 3
)
],
deduplication: Algolia::Composition::Deduplication.new(positioning: "highestInjected")
)
)
)
)
)


# print the response
puts(response)
// Initialize the client
val client = CompositionClient(appId = "ALGOLIA_APPLICATION_ID", apiKey = "ALGOLIA_API_KEY")

// Call the API
val response = Await.result(
client.putCompositionRule(
compositionID = "compositionID",
objectID = "rule-with-deduplication",
compositionRule = CompositionRule(
objectID = "rule-with-deduplication",
description = Some("my description"),
enabled = Some(true),
conditions = Some(
Seq(
Condition(
anchoring = Some(Anchoring.withName("contains")),
pattern = Some("harry")
)
)
),
consequence = CompositionRuleConsequence(
behavior = CompositionInjectionBehavior(
injection = Injection(
main = InjectionMain(
source = Some(
InjectionMainSearchSource(
search = MainSearch(
index = "my-index"
)
)
)
),
injectedItems = Some(
Seq(
InjectionInjectedItem(
key = "my-unique-injected-item-key",
source = InjectedItemSearchSource(
search = InjectedItemSearch(
index = "my-index"
)
),
position = 0,
length = 3
)
)
),
deduplication = Some(
Deduplication(
positioning = DedupPositioning.withName("highestInjected")
)
)
)
)
)
)
),
Duration(100, "sec")
)

// print the response
println(response)
// Initialize the client
let client = try CompositionClient(appID: "ALGOLIA_APPLICATION_ID", apiKey: "ALGOLIA_API_KEY")

// Call the API
let response = try await client.putCompositionRule(
compositionID: "compositionID",
objectID: "rule-with-deduplication",
compositionRule: CompositionRule(
objectID: "rule-with-deduplication",
conditions: [CompositionCondition(pattern: "harry", anchoring: CompositionAnchoring.contains)],
consequence: CompositionRuleConsequence(behavior: CompositionBehavior
.compositionInjectionBehavior(CompositionInjectionBehavior(injection: Injection(
main: InjectionMain(source: InjectionMainSource
.injectionMainSearchSource(
InjectionMainSearchSource(search: MainSearch(index: "my-index"))
)),
injectedItems: [InjectionInjectedItem(
key: "my-unique-injected-item-key",
source: InjectedItemSource
.injectedItemSearchSource(
InjectedItemSearchSource(search: InjectedItemSearch(index: "my-index"))
),
position: 0,
length: 3
)],
deduplication: Deduplication(positioning: DedupPositioning.highestInjected)
)))),
description: "my description",
enabled: true
)
)

// print the response
print(response)
{
  "taskID": 1514562690001
}
{
"message": "Invalid Application-Id or API-Key"
}
{
"message": "Invalid Application-Id or API-Key"
}
{
"message": "Invalid Application-Id or API-Key"
}
{
"message": "Invalid Application-Id or API-Key"
}
{
"message": "Invalid Application-Id or API-Key"
}
Otherwise, a new one is added. Required ACL: editSettings

Authorizations

x-algolia-application-id
string
header
required

Your Algolia application ID.

x-algolia-api-key
string
header
required

Your Algolia API key with the necessary permissions to make the request. Permissions are controlled through access control lists (ACL) and access restrictions. The required ACL to make a request is listed in each endpoint's reference.

Path Parameters

compositionID
string
required

Unique Composition ObjectID. Composition unique identifier.

Example:

"my_composition_object_id"

objectID
string
required

Unique identifier of a rule object.

Body

application/json
consequence
compositionRuleConsequence Ā· object
required

Effect of the rule.

objectID
string
required

Composition rule unique identifier.

Example:

"my_composition_rule_object_id"

conditions
object[]

Conditions that trigger a composition rule.

Maximum array length: 25
description
string

Description of the rule's purpose to help you distinguish between different rules.

Example:

"Display a promotional banner"

enabled
boolean
default:true

Whether the rule is active.

tags
string[]

A list of tags.

Example:
["conditional"]
validity
object[]

Time periods when the rule is active.

Response

OK

taskID
integer<int64>
required

Unique identifier of a task.

A successful API response means that a task was added to a queue. It might not run immediately. You can check the task's progress with the task operation and this task ID.

Example:

1514562690001

Last modified on March 23, 2026