curl
curl --request POST \
--url https://algolia_application_id.algolia.net/1/compositions/my_composition_object_id/rules/batch \
--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 '
{
"requests": [
{
"action": "upsert",
"body": {
"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"
}
}
],
"resultCard": {
"enabled": true
}
}
},
"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.SaveRulesAsync(
"my-compo",
new CompositionRulesBatchParams
{
Requests = new List<RulesMultipleBatchRequest>
{
new RulesMultipleBatchRequest
{
Action = Enum.Parse<Action>("Upsert"),
Body = new RulesBatchCompositionAction(
new CompositionRule
{
ObjectID = "rule-with-deduplication",
Description = "my description",
Enabled = true,
Conditions = new List<Condition>
{
new Condition
{
Anchoring = Enum.Parse<Anchoring>("Contains"),
Pattern = "harry",
},
new Condition { SortBy = "price-low-to-high" },
},
Consequence = new CompositionRuleConsequence
{
Behavior = new CompositionBehavior(
new CompositionInjectionBehavior
{
Injection = new Injection
{
Main = new InjectionMain
{
Source = new InjectionMainSource(
new InjectionMainSearchSource
{
Search = new InjectionMainSearch { 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.saveRules(
compositionID: "my-compo",
rules: CompositionRulesBatchParams(
requests: [
RulesMultipleBatchRequest(
action: Action.fromJson("upsert"),
body: CompositionRule(
objectID: "rule-with-deduplication",
description: "my description",
enabled: true,
conditions: [
Condition(
anchoring: Anchoring.fromJson("contains"),
pattern: "harry",
),
Condition(
sortBy: "price-low-to-high",
),
],
consequence: CompositionRuleConsequence(
behavior: CompositionInjectionBehavior(
injection: Injection(
main: InjectionMain(
source: InjectionMainSearchSource(
search: InjectionMainSearch(
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.SaveRules(client.NewApiSaveRulesRequest(
"my-compo",
composition.NewEmptyCompositionRulesBatchParams().SetRequests(
[]composition.RulesMultipleBatchRequest{
*composition.NewEmptyRulesMultipleBatchRequest().SetAction(composition.Action("upsert")).SetBody(composition.CompositionRuleAsRulesBatchCompositionAction(
composition.NewEmptyCompositionRule().SetObjectID("rule-with-deduplication").SetDescription("my description").SetEnabled(true).SetConditions(
[]composition.Condition{*composition.NewEmptyCondition().SetAnchoring(composition.Anchoring("contains")).SetPattern("harry"), *composition.NewEmptyCondition().SetSortBy("price-low-to-high")}).SetConsequence(
composition.NewEmptyCompositionRuleConsequence().SetBehavior(composition.CompositionInjectionBehaviorAsCompositionBehavior(
composition.NewEmptyCompositionInjectionBehavior().SetInjection(
composition.NewEmptyInjection().SetMain(
composition.NewEmptyInjectionMain().SetSource(composition.InjectionMainSearchSourceAsInjectionMainSource(
composition.NewEmptyInjectionMainSearchSource().SetSearch(
composition.NewEmptyInjectionMainSearch().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
RulesMultipleBatchResponse response = client.saveRules(
"my-compo",
new CompositionRulesBatchParams().setRequests(
Arrays.asList(
new RulesMultipleBatchRequest().setAction(Action.UPSERT).setBody(
new CompositionRule()
.setObjectID("rule-with-deduplication")
.setDescription("my description")
.setEnabled(true)
.setConditions(
Arrays.asList(
new Condition().setAnchoring(Anchoring.CONTAINS).setPattern("harry"),
new Condition().setSortBy("price-low-to-high")
)
)
.setConsequence(
new CompositionRuleConsequence().setBehavior(
new CompositionInjectionBehavior().setInjection(
new Injection()
.setMain(
new InjectionMain().setSource(
new InjectionMainSearchSource().setSearch(new InjectionMainSearch().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.saveRules({
compositionID: 'my-compo',
rules: {
requests: [
{
action: 'upsert',
body: {
objectID: 'rule-with-deduplication',
description: 'my description',
enabled: true,
conditions: [{ anchoring: 'contains', pattern: 'harry' }, { sortBy: 'price-low-to-high' }],
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.saveRules(
compositionID = "my-compo",
rules =
CompositionRulesBatchParams(
requests =
listOf(
RulesMultipleBatchRequest(
action = Action.entries.first { it.value == "upsert" },
body =
CompositionRule(
objectID = "rule-with-deduplication",
description = "my description",
enabled = true,
conditions =
listOf(
Condition(
anchoring = Anchoring.entries.first { it.value == "contains" },
pattern = "harry",
),
Condition(sortBy = "price-low-to-high"),
),
consequence =
CompositionRuleConsequence(
behavior =
CompositionInjectionBehavior(
injection =
Injection(
main =
InjectionMain(
source =
InjectionMainSearchSource(
search = InjectionMainSearch(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->saveRules(
'my-compo',
['requests' => [
['action' => 'upsert',
'body' => ['objectID' => 'rule-with-deduplication',
'description' => 'my description',
'enabled' => true,
'conditions' => [
['anchoring' => 'contains',
'pattern' => 'harry',
],
['sortBy' => 'price-low-to-high',
],
],
'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.save_rules(
composition_id="my-compo",
rules={
"requests": [
{
"action": "upsert",
"body": {
"objectID": "rule-with-deduplication",
"description": "my description",
"enabled": True,
"conditions": [
{
"anchoring": "contains",
"pattern": "harry",
},
{
"sortBy": "price-low-to-high",
},
],
"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.save_rules(
"my-compo",
Algolia::Composition::CompositionRulesBatchParams.new(
requests: [
Algolia::Composition::RulesMultipleBatchRequest.new(
action: "upsert",
body: Algolia::Composition::CompositionRule.new(
algolia_object_id: "rule-with-deduplication",
description: "my description",
enabled: true,
conditions: [
Algolia::Composition::Condition.new(anchoring: "contains", pattern: "harry"),
Algolia::Composition::Condition.new(sort_by: "price-low-to-high")
],
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::InjectionMainSearch.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.saveRules(
compositionID = "my-compo",
rules = CompositionRulesBatchParams(
requests = Some(
Seq(
RulesMultipleBatchRequest(
action = Action.withName("upsert"),
body = CompositionRule(
objectID = "rule-with-deduplication",
description = Some("my description"),
enabled = Some(true),
conditions = Some(
Seq(
Condition(
anchoring = Some(Anchoring.withName("contains")),
pattern = Some("harry")
),
Condition(
sortBy = Some("price-low-to-high")
)
)
),
consequence = CompositionRuleConsequence(
behavior = CompositionInjectionBehavior(
injection = Injection(
main = InjectionMain(
source = Some(
InjectionMainSearchSource(
search = InjectionMainSearch(
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.saveRules(
compositionID: "my-compo",
rules: CompositionRulesBatchParams(requests: [RulesMultipleBatchRequest(
action: CompositionAction.upsert,
body: RulesBatchCompositionAction.compositionRule(CompositionRule(
objectID: "rule-with-deduplication",
conditions: [
CompositionCondition(pattern: "harry", anchoring: CompositionAnchoring.contains),
CompositionCondition(sortBy: "price-low-to-high"),
],
consequence: CompositionRuleConsequence(behavior: CompositionBehavior
.compositionInjectionBehavior(CompositionInjectionBehavior(injection: Injection(
main: InjectionMain(source: InjectionMainSource
.injectionMainSearchSource(
InjectionMainSearchSource(search: InjectionMainSearch(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"
}Composition Rules
Create or update or delete composition rules
Create or update or delete multiple composition rules.
POST
/
1
/
compositions
/
{compositionID}
/
rules
/
batch
curl
curl --request POST \
--url https://algolia_application_id.algolia.net/1/compositions/my_composition_object_id/rules/batch \
--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 '
{
"requests": [
{
"action": "upsert",
"body": {
"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"
}
}
],
"resultCard": {
"enabled": true
}
}
},
"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.SaveRulesAsync(
"my-compo",
new CompositionRulesBatchParams
{
Requests = new List<RulesMultipleBatchRequest>
{
new RulesMultipleBatchRequest
{
Action = Enum.Parse<Action>("Upsert"),
Body = new RulesBatchCompositionAction(
new CompositionRule
{
ObjectID = "rule-with-deduplication",
Description = "my description",
Enabled = true,
Conditions = new List<Condition>
{
new Condition
{
Anchoring = Enum.Parse<Anchoring>("Contains"),
Pattern = "harry",
},
new Condition { SortBy = "price-low-to-high" },
},
Consequence = new CompositionRuleConsequence
{
Behavior = new CompositionBehavior(
new CompositionInjectionBehavior
{
Injection = new Injection
{
Main = new InjectionMain
{
Source = new InjectionMainSource(
new InjectionMainSearchSource
{
Search = new InjectionMainSearch { 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.saveRules(
compositionID: "my-compo",
rules: CompositionRulesBatchParams(
requests: [
RulesMultipleBatchRequest(
action: Action.fromJson("upsert"),
body: CompositionRule(
objectID: "rule-with-deduplication",
description: "my description",
enabled: true,
conditions: [
Condition(
anchoring: Anchoring.fromJson("contains"),
pattern: "harry",
),
Condition(
sortBy: "price-low-to-high",
),
],
consequence: CompositionRuleConsequence(
behavior: CompositionInjectionBehavior(
injection: Injection(
main: InjectionMain(
source: InjectionMainSearchSource(
search: InjectionMainSearch(
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.SaveRules(client.NewApiSaveRulesRequest(
"my-compo",
composition.NewEmptyCompositionRulesBatchParams().SetRequests(
[]composition.RulesMultipleBatchRequest{
*composition.NewEmptyRulesMultipleBatchRequest().SetAction(composition.Action("upsert")).SetBody(composition.CompositionRuleAsRulesBatchCompositionAction(
composition.NewEmptyCompositionRule().SetObjectID("rule-with-deduplication").SetDescription("my description").SetEnabled(true).SetConditions(
[]composition.Condition{*composition.NewEmptyCondition().SetAnchoring(composition.Anchoring("contains")).SetPattern("harry"), *composition.NewEmptyCondition().SetSortBy("price-low-to-high")}).SetConsequence(
composition.NewEmptyCompositionRuleConsequence().SetBehavior(composition.CompositionInjectionBehaviorAsCompositionBehavior(
composition.NewEmptyCompositionInjectionBehavior().SetInjection(
composition.NewEmptyInjection().SetMain(
composition.NewEmptyInjectionMain().SetSource(composition.InjectionMainSearchSourceAsInjectionMainSource(
composition.NewEmptyInjectionMainSearchSource().SetSearch(
composition.NewEmptyInjectionMainSearch().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
RulesMultipleBatchResponse response = client.saveRules(
"my-compo",
new CompositionRulesBatchParams().setRequests(
Arrays.asList(
new RulesMultipleBatchRequest().setAction(Action.UPSERT).setBody(
new CompositionRule()
.setObjectID("rule-with-deduplication")
.setDescription("my description")
.setEnabled(true)
.setConditions(
Arrays.asList(
new Condition().setAnchoring(Anchoring.CONTAINS).setPattern("harry"),
new Condition().setSortBy("price-low-to-high")
)
)
.setConsequence(
new CompositionRuleConsequence().setBehavior(
new CompositionInjectionBehavior().setInjection(
new Injection()
.setMain(
new InjectionMain().setSource(
new InjectionMainSearchSource().setSearch(new InjectionMainSearch().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.saveRules({
compositionID: 'my-compo',
rules: {
requests: [
{
action: 'upsert',
body: {
objectID: 'rule-with-deduplication',
description: 'my description',
enabled: true,
conditions: [{ anchoring: 'contains', pattern: 'harry' }, { sortBy: 'price-low-to-high' }],
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.saveRules(
compositionID = "my-compo",
rules =
CompositionRulesBatchParams(
requests =
listOf(
RulesMultipleBatchRequest(
action = Action.entries.first { it.value == "upsert" },
body =
CompositionRule(
objectID = "rule-with-deduplication",
description = "my description",
enabled = true,
conditions =
listOf(
Condition(
anchoring = Anchoring.entries.first { it.value == "contains" },
pattern = "harry",
),
Condition(sortBy = "price-low-to-high"),
),
consequence =
CompositionRuleConsequence(
behavior =
CompositionInjectionBehavior(
injection =
Injection(
main =
InjectionMain(
source =
InjectionMainSearchSource(
search = InjectionMainSearch(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->saveRules(
'my-compo',
['requests' => [
['action' => 'upsert',
'body' => ['objectID' => 'rule-with-deduplication',
'description' => 'my description',
'enabled' => true,
'conditions' => [
['anchoring' => 'contains',
'pattern' => 'harry',
],
['sortBy' => 'price-low-to-high',
],
],
'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.save_rules(
composition_id="my-compo",
rules={
"requests": [
{
"action": "upsert",
"body": {
"objectID": "rule-with-deduplication",
"description": "my description",
"enabled": True,
"conditions": [
{
"anchoring": "contains",
"pattern": "harry",
},
{
"sortBy": "price-low-to-high",
},
],
"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.save_rules(
"my-compo",
Algolia::Composition::CompositionRulesBatchParams.new(
requests: [
Algolia::Composition::RulesMultipleBatchRequest.new(
action: "upsert",
body: Algolia::Composition::CompositionRule.new(
algolia_object_id: "rule-with-deduplication",
description: "my description",
enabled: true,
conditions: [
Algolia::Composition::Condition.new(anchoring: "contains", pattern: "harry"),
Algolia::Composition::Condition.new(sort_by: "price-low-to-high")
],
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::InjectionMainSearch.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.saveRules(
compositionID = "my-compo",
rules = CompositionRulesBatchParams(
requests = Some(
Seq(
RulesMultipleBatchRequest(
action = Action.withName("upsert"),
body = CompositionRule(
objectID = "rule-with-deduplication",
description = Some("my description"),
enabled = Some(true),
conditions = Some(
Seq(
Condition(
anchoring = Some(Anchoring.withName("contains")),
pattern = Some("harry")
),
Condition(
sortBy = Some("price-low-to-high")
)
)
),
consequence = CompositionRuleConsequence(
behavior = CompositionInjectionBehavior(
injection = Injection(
main = InjectionMain(
source = Some(
InjectionMainSearchSource(
search = InjectionMainSearch(
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.saveRules(
compositionID: "my-compo",
rules: CompositionRulesBatchParams(requests: [RulesMultipleBatchRequest(
action: CompositionAction.upsert,
body: RulesBatchCompositionAction.compositionRule(CompositionRule(
objectID: "rule-with-deduplication",
conditions: [
CompositionCondition(pattern: "harry", anchoring: CompositionAnchoring.contains),
CompositionCondition(sortBy: "price-low-to-high"),
],
consequence: CompositionRuleConsequence(behavior: CompositionBehavior
.compositionInjectionBehavior(CompositionInjectionBehavior(injection: Injection(
main: InjectionMain(source: InjectionMainSource
.injectionMainSearchSource(
InjectionMainSearchSource(search: InjectionMainSearch(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"
}Required ACL:
editSettingsAuthorizations
Path Parameters
Body
application/json
Response
OK
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
Was this page helpful?