Gunakan fitur penelusuran teks di Cloud Firestore untuk menelusuri string tertentu dalam dokumen.
Persyaratan edisi
Penelusuran teks memerlukan database edisi Firestore Enterprise.
Sebelum memulai
Untuk melakukan penelusuran teks, Anda harus membuat indeks teks terlebih dahulu untuk kolom yang perlu Anda telusuri.
Menjalankan penelusuran teks
Untuk melakukan penelusuran teks, gunakan ekspresi documentMatches dalam
parameter query pada tahap search(...).
Operasi ini hanya menelusuri kolom yang diindeks dengan indeks teks. Jika beberapa indeks tersedia, Cloud Firestore akan memilih satu indeks untuk digunakan dalam operasi.
Web
const result = await execute(db.pipeline().collection('restaurants') .search({ query: documentMatches('waffles') }));
iOS
let snapshot = try await db.pipeline().collection("restaurants") .search(query: DocumentMatches("waffles")) .execute()
Kotlin
val pipeline = db.pipeline().collection("restaurants") .search(SearchStage.withQuery(documentMatches("waffles")))
Java
Pipeline pipeline = db.pipeline().collection("restaurants") .search(SearchStage.withQuery(documentMatches("waffles")));
Node.js
await db.pipeline().collection('restaurants') .search({ query: documentMatches('waffles') }) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import DocumentMatches results = ( client.pipeline() .collection("restaurants") .search(DocumentMatches("waffles")) .execute() )
Java
Pipeline.Snapshot results1 = firestore.pipeline().collection("restaurants") .search(Search.withQuery(documentMatches("waffles"))) .execute().get();
Go
snapshot := client.Pipeline(). Collection("restaurants"). Search(firestore.WithSearchQuery(firestore.DocumentMatches("waffles"))). Execute(ctx)
Menelusuri istilah yang sama persis
Untuk menelusuri istilah yang sama persis, masukkan istilah tersebut ke dalam tanda kutip ("):
Web
const result = await execute(db.pipeline().collection('restaurants') .search({ query: documentMatches('"belgian waffles"') }));
iOS
let snapshot = try await db.pipeline().collection("restaurants") .search(query: DocumentMatches("\"belgian waffles\"")) .execute()
Kotlin
val pipeline = db.pipeline().collection("restaurants") .search(SearchStage.withQuery(documentMatches("\"belgian waffles\"")))
Java
Pipeline pipeline = db.pipeline().collection("restaurants") .search(SearchStage.withQuery(documentMatches("\"belgian waffles\"")));
Node.js
await db.pipeline().collection('restaurants') .search({ query: documentMatches('"belgian waffles"') }) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import DocumentMatches results = ( client.pipeline() .collection("restaurants") .search(DocumentMatches('"belgian waffles"')) .execute() )
Java
Pipeline.Snapshot results2 = firestore.pipeline().collection("restaurants") .search(Search.withQuery(documentMatches("\"belgian waffles\""))) .execute().get();
Go
snapshot := client.Pipeline(). Collection("restaurants"). Search(firestore.WithSearchQuery(firestore.DocumentMatches("\"belgian waffles\""))). Execute(ctx)
Menelusuri kombinasi istilah
Untuk menelusuri kombinasi istilah (logika AND), pisahkan istilah dengan spasi:
Web
const result = await execute(db.pipeline().collection('restaurants') .search({ query: documentMatches('waffles eggs') }));
iOS
let snapshot = try await db.pipeline().collection("restaurants") .search(query: DocumentMatches("waffles eggs")) .execute()
Kotlin
val pipeline = db.pipeline().collection("restaurants") .search(SearchStage.withQuery(documentMatches("waffles eggs")))
Java
Pipeline pipeline = db.pipeline().collection("restaurants") .search(SearchStage.withQuery(documentMatches("waffles eggs")));
Node.js
await db.pipeline().collection('restaurants') .search({ query: documentMatches('waffles eggs') }) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import DocumentMatches results = ( client.pipeline() .collection("restaurants") .search(DocumentMatches("waffles eggs")) .execute() )
Java
firestore.collection("restaurants").add(new HashMap<String, Object>() {{ put("name", "Morning Diner"); put("description", "Start your day with waffles and eggs."); }}); Pipeline.Snapshot results3 = firestore.pipeline().collection("restaurants") .search(Search.withQuery(documentMatches("waffles eggs"))) .execute().get();
Go
snapshot := client.Pipeline(). Collection("restaurants"). Search(firestore.WithSearchQuery(firestore.DocumentMatches("waffles eggs"))). Execute(ctx)
Mengecualikan istilah
Untuk mengecualikan istilah, awali istilah dengan tanda hubung (-):
Web
const result = await execute(db.pipeline().collection('restaurants') .search({ query: documentMatches('coffee -waffles') }));
iOS
let snapshot = try await db.pipeline().collection("restaurants") .search(query: DocumentMatches("coffee -waffles")) .execute()
Kotlin
val pipeline = db.pipeline().collection("restaurants") .search(SearchStage.withQuery(documentMatches("waffles eggs")))
Java
Pipeline pipeline = db.pipeline().collection("restaurants") .search(SearchStage.withQuery(documentMatches("coffee -waffles")));
Node.js
await db.pipeline().collection('restaurants') .search({ query: documentMatches('-waffles') }) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import DocumentMatches results = ( client.pipeline() .collection("restaurants") .search(DocumentMatches("-waffles")) .execute() )
Java
firestore.collection("restaurants").add(new HashMap<String, Object>() {{ put("name", "City Coffee"); put("description", "Premium coffee and pastries."); }}); Pipeline.Snapshot results4 = firestore.pipeline().collection("restaurants") .search(Search.withQuery(documentMatches("-waffles"))) .execute().get();
Go
snapshot := client.Pipeline(). Collection("restaurants"). Search(firestore.WithSearchQuery(firestore.DocumentMatches("-waffles"))). Execute(ctx)
Anda juga dapat mengecualikan frasa, misalnya, pizza -"New York".
Urutkan hasil
Secara default, Cloud Firestore mengurutkan hasil menurut waktu pembuatan dokumen, dari yang terbaru ke terlama. Anda dapat mengurutkan berdasarkan skor penelusuran, tetapi hal ini memerlukan lebih banyak komputasi untuk menghitung dan membandingkan skor persis setiap dokumen:
Untuk mengurutkan hasil menurut skor penelusuran:
Web
const result = await execute(db.pipeline().collection('restaurants') .search({ query: documentMatches('waffles'), sort: score().descending() }));
iOS
let snapshot = try await db.pipeline().collection("restaurants")
.search(
query: DocumentMatches("waffles"),
sort: [Score().descending()]
)
.execute()Kotlin
val pipeline = db.pipeline().collection("restaurants") .search(SearchStage .withQuery(documentMatches("waffles")) .withSort(score().descending()) )
Node.js
await db.pipeline().collection('restaurants') .search({ query: documentMatches('waffles'), sort: score().descending() }) .execute();
Menambahkan kolom ke dokumen yang ditampilkan oleh tahap penelusuran
Anda dapat menggunakan addFields untuk menambahkan kolom ke dokumen yang ditampilkan oleh
tahap penelusuran. Ekspresi yang menampilkan nilai yang dihitung oleh tahap penelusuran, seperti score(), dapat digunakan dalam addFields di tahap penelusuran untuk menulis `nilai tersebut ke dokumen output.
Contoh berikut menambahkan kolom skor ke dokumen yang ditampilkan oleh tahap penelusuran:
Web
const result = await execute(db.pipeline().collection('restaurants') .search({ query: 'menu:waffles', addFields: [ score().as('score'), ] }));
iOS
let snapshot = try await db.pipeline().collection("restaurants") .search( query: DocumentMatches("waffles"), addFields: [ Score().as("score") ] ) .execute()
Kotlin
val pipeline = db.pipeline().collection("restaurants") .search(SearchStage.withQuery(documentMatches("waffles eggs")))
Java
Pipeline pipeline = db.pipeline().collection("restaurants") .search( SearchStage.withQuery(documentMatches("menu:waffles")) .withAddFields(score().alias("score")));
Node.js
await db.pipeline().collection('restaurants') .search({ query: field('menu').matches('waffles'), addFields: [ score().as('score'), ] }).execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import DocumentMatches, Score from google.cloud.firestore_v1.pipeline_stages import SearchOptions results = ( client.pipeline() .collection("restaurants") .search( SearchOptions( query=DocumentMatches("menu:waffles"), add_fields=[Score().as_("score")], ) ) .execute() )
Java
Pipeline.Snapshot results5 = firestore.pipeline().collection("restaurants") .search(Search.withQuery(field("menu").regexMatch("waffles")) .withAddFields(score().as("score"))) .execute().get();
Go
snapshot := client.Pipeline(). Collection("restaurants"). Search( firestore.WithSearchQuery(firestore.FieldOf("menu").RegexMatch("waffles")), firestore.WithSearchAddFields(firestore.Score().As("score")), ). Execute(ctx)