Skip to content

Commit 035ff34

Browse files
committed
feat(scala): more guides
1 parent ecec169 commit 035ff34

File tree

6 files changed

+128
-0
lines changed

6 files changed

+128
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import scala.concurrent.ExecutionContext.Implicits.global
2+
3+
{{> snippets/import}}
4+
import algoliasearch.search.{Anchoring, Condition, Consequence, Rule}
5+
6+
def enableFilterPromote(): Unit = {
7+
val condition = Condition(
8+
pattern = Some("{facet:brand}"),
9+
anchoring = Some(Anchoring.Is)
10+
)
11+
12+
val consequence = Consequence(
13+
filterPromotes = Some(true)
14+
)
15+
16+
val rule = Rule(
17+
objectID = "rule_with_filterPromotes",
18+
conditions = Some(Seq(condition)),
19+
consequence = consequence
20+
)
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import scala.concurrent.ExecutionContext.Implicits.global
2+
import scala.concurrent.{Await, Future}
3+
import scala.concurrent.duration.Duration
4+
5+
{{> snippets/import}}
6+
import algoliasearch.search.SearchParamsObject
7+
8+
def searchInReplicaIndex(): Future[Unit] = {
9+
{{> snippets/init}}
10+
11+
// 1. Change the sort dynamically based on the UI events
12+
val sortByPrice = false
13+
14+
// 2. Get the index name based on sortByPrice
15+
val indexName = if sortByPrice then "products_price_desc" else "products"
16+
17+
// 3. Search on dynamic index name (primary or replica)
18+
{{#dynamicSnippet}}searchWithIndexNameVar{{/dynamicSnippet}}
19+
.map { response =>
20+
println(response)
21+
}.recover {
22+
case ex: Exception =>
23+
println(s"An error occurred: ${ex.getMessage}")
24+
}
25+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import scala.concurrent.ExecutionContext.Implicits.global
2+
import scala.concurrent.Future
3+
4+
{{> snippets/import}}
5+
import algoliasearch.search.SearchParamsObject
6+
7+
def searchWithAnalyticsAndHeader(): Future[Unit] = {
8+
{{> snippets/init}}
9+
10+
/*
11+
'94.228.178.246' should be replaced with your user's IP address.
12+
Depending on your stack there are multiple ways to get this information.
13+
*/
14+
val ip = "94.228.178.246"
15+
val query = "query"
16+
17+
val searchParams = SearchParamsObject(
18+
query = Some(query),
19+
analytics = Some(true)
20+
)
21+
22+
{{#dynamicSnippet}}searchWithSearchParamsAndForwardedHeader{{/dynamicSnippet}}.map { response =>
23+
println(response)
24+
}.recover {
25+
case ex: Exception =>
26+
println(s"An error occurred: ${ex.getMessage}")
27+
}
28+
}
29+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import scala.concurrent.ExecutionContext.Implicits.global
2+
import scala.concurrent.Future
3+
4+
{{> snippets/import}}
5+
import algoliasearch.search.{OptionalWords, SearchParamsObject}
6+
7+
def searchWithLogicalOr(): Future[Unit] = {
8+
{{> snippets/init}}
9+
val query = "the query"
10+
val optionalWords = Seq("the", "query")
11+
val searchParams = SearchParamsObject(
12+
query = Some(query),
13+
optionalWords = Some(OptionalWords.SeqOfString(optionalWords)),
14+
)
15+
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}}.map { response =>
16+
println(response)
17+
}.recover {
18+
case ex: Exception =>
19+
println(s"An error occurred: ${ex.getMessage}")
20+
}
21+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import scala.concurrent.ExecutionContext.Implicits.global
2+
import scala.concurrent.Future
3+
4+
{{> snippets/import}}
5+
import algoliasearch.search.{Consequence, Rule, TimeRange}
6+
7+
def useConditionlessRule(): Future[Unit] = {
8+
{{> snippets/init}}
9+
10+
val objectID = "a-rule-id";
11+
12+
val rule = Rule(
13+
objectID = objectID,
14+
consequence = Consequence(
15+
// Set relevant consequence
16+
),
17+
// Set validity (optional)
18+
validity = Some(Seq(
19+
TimeRange(
20+
from = 1_688_774_400,
21+
until = 1_738_972_800
22+
)
23+
))
24+
)
25+
26+
{{#dynamicSnippet}}saveRule{{/dynamicSnippet}}.map { response =>
27+
println(response)
28+
}.recover {
29+
case ex: Exception =>
30+
println(s"An error occurred: ${ex.getMessage}")
31+
}
32+
}

0 commit comments

Comments
 (0)