Skip to content

Commit ba3abbc

Browse files
committed
Contextual rules
1 parent 0fc12dd commit ba3abbc

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

src/commonMain/kotlin/com/algolia/search/model/rule/Condition.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import kotlinx.serialization.Serializable
1010

1111
@Serializable
1212
public data class Condition(
13-
@SerialName(KeyAnchoring) val anchoring: Anchoring,
14-
@SerialName(KeyPattern) val pattern: Pattern,
13+
@SerialName(KeyAnchoring) val anchoring: Anchoring? = null,
14+
@SerialName(KeyPattern) val pattern: Pattern? = null,
1515
/**
1616
* Rule context (format: [A-Za-z0-9_-]+). When specified, the rule is contextual and applies only when the
1717
* same context is specified at query time (using the ruleContexts parameter). When absent, the rule is generic

src/commonMain/kotlin/com/algolia/search/model/rule/Rule.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public data class Rule(
1515
/**
1616
* Condition of the rule.
1717
*/
18-
@SerialName(KeyCondition) val condition: Condition,
18+
@SerialName(KeyCondition) val condition: Condition? = null,
1919
/**
2020
* Consequence of the rule.
2121
*/

src/commonTest/kotlin/suite/TestSuiteRules.kt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@ package suite
22

33
import clientAdmin1
44
import com.algolia.search.helper.toAttribute
5+
import com.algolia.search.model.ObjectID
6+
import com.algolia.search.model.rule.Condition
7+
import com.algolia.search.model.rule.Consequence
58
import com.algolia.search.model.rule.Rule
9+
import com.algolia.search.model.search.Query
610
import com.algolia.search.model.settings.AttributeForFaceting
711
import com.algolia.search.model.settings.Settings
812
import com.algolia.search.model.task.Task
913
import com.algolia.search.model.task.TaskStatus
14+
import com.algolia.search.serialize.JsonDebug
15+
import com.algolia.search.serialize.JsonNoDefaults
1016
import io.ktor.client.features.ResponseException
1117
import io.ktor.http.HttpStatusCode
1218
import kotlinx.serialization.json.JsonObjectSerializer
@@ -24,6 +30,7 @@ internal class TestSuiteRules {
2430
private val suffix = "rules"
2531
private val indexName = testSuiteIndexName(suffix)
2632
private val brand = "brand".toAttribute()
33+
private val model = "model".toAttribute()
2734
private val index = clientAdmin1.initIndex(indexName)
2835

2936
@AfterTest
@@ -38,12 +45,19 @@ internal class TestSuiteRules {
3845
runBlocking {
3946
val objects = load(JsonObjectSerializer.list, "iphones.json")
4047
val rule = load(Rule.serializer(), "rule_brand.json")
41-
val rules = load(Rule.serializer().list, "rule_edits.json")
48+
val rules = load(Rule.serializer().list, "rule_batch.json")
4249
val tasks = mutableListOf<Task>()
4350

4451
index.apply {
4552
tasks += saveObjects(objects)
46-
tasks += setSettings(Settings(attributesForFaceting = listOf(AttributeForFaceting.Default(brand))))
53+
tasks += setSettings(
54+
Settings(
55+
attributesForFaceting = listOf(
56+
AttributeForFaceting.Default(brand),
57+
AttributeForFaceting.Default(model)
58+
)
59+
)
60+
)
4761
tasks += saveRule(rule)
4862
tasks += saveRules(rules)
4963

@@ -57,6 +71,7 @@ internal class TestSuiteRules {
5771
searches.find { it.objectID == rules.first().objectID }.shouldNotBeNull()
5872
deleteRule(rule.objectID).wait() shouldEqual TaskStatus.Published
5973

74+
search(Query(ruleContexts = listOf("summer"))).nbHits shouldEqual 1
6075
(shouldFailWith<ResponseException> {
6176
getRule(rule.objectID)
6277
}).response.status.value shouldEqual HttpStatusCode.NotFound.value

src/commonTest/resources/rule_edits.json renamed to src/commonTest/resources/rule_batch.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,24 @@
2323
}
2424
}
2525
}
26+
},
27+
{
28+
"objectID": "query_promo",
29+
"consequence": {
30+
"params": {
31+
"filters": "brand:OnePlus"
32+
}
33+
}
34+
},
35+
{
36+
"objectID": "query_promo_summer",
37+
"condition": {
38+
"context": "summer"
39+
},
40+
"consequence": {
41+
"params": {
42+
"filters": "model:One"
43+
}
44+
}
2645
}
2746
]

0 commit comments

Comments
 (0)