दस्तावेज़ों में मौजूद खास स्ट्रिंग को खोजने के लिए, Cloud Firestore में टेक्स्ट खोजने की सुविधाओं का इस्तेमाल करें.
एडिशन से जुड़ी ज़रूरी शर्तें
टेक्स्ट खोजने के लिए, Firestore Enterprise एडिशन के डेटाबेस की ज़रूरत होती है.
शुरू करने से पहले
टेक्स्ट खोजने के लिए, आपको उन फ़ील्ड के लिए टेक्स्ट इंडेक्स बनाने होंगे जिनमें आपको खोजना है.
टेक्स्ट खोजना
टेक्स्ट खोजने के लिए, search(...) स्टेज के query पैरामीटर में documentMatches एक्सप्रेशन का इस्तेमाल करें.
इस कार्रवाई से, सिर्फ़ उन फ़ील्ड में खोज की जाती है जिन्हें टेक्स्ट इंडेक्स के साथ इंडेक्स किया गया है. अगर एक से ज़्यादा इंडेक्स उपलब्ध हैं, तो Cloud Firestore कार्रवाई के लिए किसी एक इंडेक्स को चुनता है.
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)
कोई सटीक शब्द खोजना
कोई सटीक शब्द खोजने के लिए, उस शब्द को कोट (") में रखें:
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)
शब्दों का कोई कॉम्बिनेशन खोजना
शब्दों का कोई कॉम्बिनेशन (लॉजिकल AND) खोजने के लिए, शब्दों के बीच स्पेस डालें:
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)
किसी शब्द को शामिल न करना
किसी शब्द को शामिल न करने के लिए, उस शब्द से पहले हाइफ़न (-) लगाएं:
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)
आपके पास किसी वाक्यांश को भी शामिल न करने का विकल्प होता है. उदाहरण के लिए, pizza -"New York".
नतीजों को क्रम से लगाना
डिफ़ॉल्ट रूप से, Cloud Firestore नतीजों को दस्तावेज़ बनाए जाने के समय के हिसाब से क्रम से लगाता है. इसमें सबसे नए दस्तावेज़ पहले और सबसे पुराने दस्तावेज़ बाद में दिखते हैं. आपके पास खोज के स्कोर के हिसाब से क्रम से लगाने का विकल्प होता है. हालांकि, इसके लिए हर दस्तावेज़ के सटीक स्कोर की तुलना करने और उसे कैलकुलेट करने के लिए, ज़्यादा कंप्यूटेशन की ज़रूरत होती है:
नतीजों को खोज के स्कोर के हिसाब से क्रम से लगाने के लिए:
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();
खोज के स्टेज से मिले दस्तावेज़ों में फ़ील्ड जोड़ना
खोज के स्टेज से मिले दस्तावेज़ों में फ़ील्ड जोड़ने के लिए, addFields का इस्तेमाल किया जा सकता है. खोज के स्टेज में, addFields में ऐसे एक्सप्रेशन इस्तेमाल किए जा सकते हैं जो खोज के स्टेज से कैलकुलेट की गई वैल्यू दिखाते हैं. जैसे, score(). इससे, उन `values` को आउटपुट दस्तावेज़ों में लिखा जा सकता है.
यहां दिए गए उदाहरण में, खोज के स्टेज से मिले दस्तावेज़ों में स्कोर फ़ील्ड जोड़ा गया है:
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)