Skip to content

Commit 95e5f4e

Browse files
committed
add snippets for search
1 parent ce43b47 commit 95e5f4e

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

firestore/app/src/main/java/com/google/example/firestore/DocSnippets.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,14 @@
6262
import com.google.firebase.firestore.pipeline.Expression;
6363
import com.google.firebase.firestore.pipeline.FindNearestStage;
6464
import com.google.firebase.firestore.pipeline.SampleStage;
65+
import com.google.firebase.firestore.pipeline.SearchStage;
6566
import com.google.firebase.firestore.pipeline.UnnestOptions;
6667

6768
import static com.google.firebase.firestore.pipeline.Expression.field;
6869
import static com.google.firebase.firestore.pipeline.Expression.constant;
6970
import static com.google.firebase.firestore.pipeline.Expression.variable;
71+
import static com.google.firebase.firestore.pipeline.Expression.documentMatches;
72+
import static com.google.firebase.firestore.pipeline.Expression.score;
7073
import static com.google.firebase.firestore.pipeline.AggregateFunction.average;
7174
import static com.google.firebase.firestore.pipeline.AggregateFunction.countAll;
7275

@@ -4048,4 +4051,41 @@ void toScalarExpressionStage() {
40484051
// [END to_scalar_expression]
40494052
}
40504053

4054+
void searchBasicQuery() {
4055+
// [START search_basic_query]
4056+
Pipeline pipeline = db.pipeline().collection("restaurants")
4057+
.search(SearchStage.withQuery(documentMatches("waffles")));
4058+
// [END search_basic_query]
4059+
}
4060+
4061+
void searchExactMatch() {
4062+
// [START search_exact_match]
4063+
Pipeline pipeline = db.pipeline().collection("restaurants")
4064+
.search(SearchStage.withQuery(documentMatches("\"belgian waffles\"")));
4065+
// [END search_exact_match]
4066+
}
4067+
4068+
void searchTwoTerms() {
4069+
// [START search_two_terms]
4070+
Pipeline pipeline = db.pipeline().collection("restaurants")
4071+
.search(SearchStage.withQuery(documentMatches("waffles eggs")));
4072+
// [END search_two_terms]
4073+
}
4074+
4075+
void searchExcludeTerm() {
4076+
// [START search_exclude_term]
4077+
Pipeline pipeline = db.pipeline().collection("restaurants")
4078+
.search(SearchStage.withQuery(documentMatches("coffee -waffles")));
4079+
// [END search_exclude_term]
4080+
}
4081+
4082+
void searchAddScore() {
4083+
// [START search_add_score]
4084+
Pipeline pipeline = db.pipeline().collection("restaurants")
4085+
.search(
4086+
SearchStage.withQuery(documentMatches("menu:waffles"))
4087+
.withAddFields(score().alias("score")));
4088+
// [END search_add_score]
4089+
}
4090+
40514091
}

firestore/app/src/main/java/com/google/example/firestore/kotlin/DocSnippets.kt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@ import com.google.firebase.firestore.pipeline.AggregateFunction.Companion.countA
3232
import com.google.firebase.firestore.pipeline.Expression.Companion.constant
3333
import com.google.firebase.firestore.pipeline.Expression.Companion.field
3434
import com.google.firebase.firestore.pipeline.Expression.Companion.concat
35+
import com.google.firebase.firestore.pipeline.Expression.Companion.documentMatches
3536
import com.google.firebase.firestore.pipeline.Expression.Companion.length
37+
import com.google.firebase.firestore.pipeline.Expression.Companion.score
3638
import com.google.firebase.firestore.pipeline.Expression.Companion.type
3739
import com.google.firebase.firestore.pipeline.Expression.Companion.variable
3840
import com.google.firebase.firestore.pipeline.FindNearestStage
3941
import com.google.firebase.firestore.pipeline.SampleStage
42+
import com.google.firebase.firestore.pipeline.SearchStage
4043
import com.google.firebase.firestore.pipeline.UnnestOptions
4144
import com.google.firebase.firestore.toObject
4245
import java.util.Date
@@ -3656,4 +3659,42 @@ abstract class DocSnippets(val db: FirebaseFirestore) {
36563659
// [END to_scalar_expression]
36573660
}
36583661

3662+
fun searchBasicQuery() {
3663+
// [START search_basic_query]
3664+
val pipeline = db.pipeline().collection("restaurants")
3665+
.search(SearchStage.withQuery(documentMatches("waffles")))
3666+
// [END search_basic_query]
3667+
}
3668+
3669+
fun searchExactMatch() {
3670+
// [START search_exact_match]
3671+
val pipeline = db.pipeline().collection("restaurants")
3672+
.search(SearchStage.withQuery(documentMatches("\"belgian waffles\"")))
3673+
// [END search_exact_match]
3674+
}
3675+
3676+
fun searchTwoTerms() {
3677+
// [START search_two_terms]
3678+
val pipeline = db.pipeline().collection("restaurants")
3679+
.search(SearchStage.withQuery(documentMatches("waffles eggs")))
3680+
// [END search_two_terms]
3681+
}
3682+
3683+
fun searchExcludeTerm() {
3684+
// [START search_exclude_term]
3685+
val pipeline = db.pipeline().collection("restaurants")
3686+
.search(SearchStage.withQuery(documentMatches("coffee -waffles")))
3687+
// [END search_exclude_term]
3688+
}
3689+
3690+
fun searchAddScore() {
3691+
// [START search_add_score]
3692+
val pipeline = db.pipeline().collection("restaurants")
3693+
.search(
3694+
SearchStage.withQuery(documentMatches("menu:waffles"))
3695+
.withAddFields(score().alias("score"))
3696+
)
3697+
// [END search_add_score]
3698+
}
3699+
36593700
}

0 commit comments

Comments
 (0)