दस्तावेज़ों में मौजूद किसी स्ट्रिंग को खोजने के लिए, 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();
जाएं
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();
जाएं
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();
जाएं
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();
जाएं
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 का इस्तेमाल करके, खोज स्टेज से मिले दस्तावेज़ों में फ़ील्ड जोड़े जा सकते हैं. सर्च स्टेज में, score() जैसे एक्सप्रेशन का इस्तेमाल किया जा सकता है. ये एक्सप्रेशन, सर्च स्टेज में कंप्यूट की गई वैल्यू दिखाते हैं. इन एक्सप्रेशन का इस्तेमाल, सर्च स्टेज में addFields के अंदर किया जा सकता है, ताकि उन `वैल्यू को आउटपुट दस्तावेज़ों में लिखा जा सके.
यहां दिए गए उदाहरण में, खोज के चरण में मिले दस्तावेज़ों में स्कोर फ़ील्ड जोड़ा गया है:
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();
जाएं
snapshot := client.Pipeline(). Collection("restaurants"). Search( firestore.WithSearchQuery(firestore.FieldOf("menu").RegexMatch("waffles")), firestore.WithSearchAddFields(firestore.Score().As("score")), ). Execute(ctx)