Sep 25, 2025
Upsert a composition
Upsert a composition in the current Algolia application.
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
91
import com.algolia.api.CompositionClient;
import com.algolia.config.*;
CompositionClient client = new CompositionClient("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY");
client.putComposition(
"my-metadata-compo",
new Composition()
.setObjectID("my-metadata-compo")
.setName("my composition")
.setBehavior(
new CompositionBehavior().setInjection(
new Injection()
.setMain(
new Main().setSource(
new CompositionSource().setSearch(
new CompositionSourceSearch().setIndex("foo").setParams(new MainInjectionQueryParameters().setFilters("brand:adidas"))
)
)
)
.setInjectedItems(
Arrays.asList(
new InjectedItem()
.setKey("injectedItem1")
.setSource(
new SearchSource().setSearch(
new Search().setIndex("foo").setParams(new BaseInjectionQueryParameters().setFilters("brand:adidas"))
)
)
.setPosition(2)
.setLength(1)
.setMetadata(
new InjectedItemMetadata().setHits(
new InjectedItemHitsMetadata()
.setAddItemKey(true)
.setExtra(
new HashMap() {
{
put("my-string", "string");
put("my-bool", true);
put("my-number", 42);
put(
"my-object",
new HashMap() {
{
put("sub-key", "sub-value");
}
}
);
}
}
)
)
),
new InjectedItem()
.setKey("externalItem")
.setSource(
new SearchSource().setSearch(
new Search().setIndex("foo").setParams(new BaseInjectionQueryParameters().setFilters("brand:puma"))
)
)
.setPosition(5)
.setLength(5)
.setMetadata(
new InjectedItemMetadata().setHits(
new InjectedItemHitsMetadata()
.setAddItemKey(true)
.setExtra(
new HashMap() {
{
put("my-string", "string");
put("my-bool", true);
put("my-number", 42);
put(
"my-object",
new HashMap() {
{
put("sub-key", "sub-value");
}
}
);
}
}
)
)
)
)
)
)
)
);