ब्यौरा
यह फ़ंक्शन, पिछले चरण के नतीजों से एक नॉन-डिटरमिनिस्टिक सैंपल दिखाता है.
ये दो मोड काम करते हैं:
DOCUMENTSमोड में, तय संख्या में दस्तावेज़ों की सैंपलिंग की जा सकती है- यह मोड,
GoogleSQL.RESERVOIRके जैसा ही है. इसमेंnसाइज़ का सैंपल आउटपुट होता है. इसमेंnसाइज़ का कोई भी सैंपल मिल सकता है.
- यह मोड,
PERCENTमोड में, कुछ प्रतिशत दस्तावेज़ों की सैंपलिंग की जा सकती है- यह मोड,
GoogleSQL.BERNOULLIमोड की तरह ही होता है. इसमें हर दस्तावेज़ को स्वतंत्र रूप से चुना जाता है. साथ ही, हर दस्तावेज़ के चुने जाने की संभावनाpercentहोती है. इससे औसतन#documents * percent / 100दस्तावेज़ वापस मिलते हैं.
- यह मोड,
सिंटैक्स
Node.js
const sampled = await db.pipeline()
.database()
.sample(50)
.execute();
const sampled = await db.pipeline()
.database()
.sample({ percent: 0.5 })
.execute();
व्यवहार
दस्तावेज़ मोड
दस्तावेज़ मोड में, तय किए गए दस्तावेज़ों को बिना किसी क्रम के वापस पाया जाता है.
दी गई संख्या, नॉन-नेगेटिव INT64 वैल्यू होनी चाहिए.
उदाहरण के लिए, इस कलेक्शन के लिए:
Node.js
await db.collection('cities').doc('SF').set({name: 'San Francsico', state: 'California'});
await db.collection('cities').doc('NYC').set({name: 'New York City', state: 'New York'});
await db.collection('cities').doc('CHI').set({name: 'Chicago', state: 'Illinois'});
डॉक्यूमेंट मोड में सैंपल स्टेज का इस्तेमाल, इस कलेक्शन से नतीजों का नॉन-डिटरमिनिस्टिक सबसेट पाने के लिए किया जा सकता है.
Node.js
const sampled = await db.pipeline()
.collection("/cities")
.sample(1)
.execute();
इस उदाहरण में, सिर्फ़ एक दस्तावेज़ को रैंडम तरीके से दिखाया जाएगा.
{name: 'New York City', state: 'New York'}
अगर दिया गया नंबर, खोज के नतीजों में मिले दस्तावेज़ों की कुल संख्या से ज़्यादा है, तो सभी दस्तावेज़ों को किसी भी क्रम में दिखाया जाता है.
Node.js
const sampled = await db.pipeline()
.collection("/cities")
.sample(5)
.execute();
इससे ये दस्तावेज़ बनेंगे:
{name: 'New York City', state: 'New York'}
{name: 'Chicago', state: 'Illinois'}
{name: 'San Francisco', state: 'California'}
क्लाइंट के उदाहरण
Web
let results; // Get a sample of 100 documents in a database results = await execute(db.pipeline() .database() .sample(100) ); // Randomly shuffle a list of 3 documents results = await execute(db.pipeline() .documents([ doc(db, "cities", "SF"), doc(db, "cities", "NY"), doc(db, "cities", "DC"), ]) .sample(3) );
Swift
var results: Pipeline.Snapshot // Get a sample of 100 documents in a database results = try await db.pipeline() .database() .sample(count: 100) .execute() // Randomly shuffle a list of 3 documents results = try await db.pipeline() .documents([ db.collection("cities").document("SF"), db.collection("cities").document("NY"), db.collection("cities").document("DC"), ]) .sample(count: 3) .execute()
Kotlin
var results: Task<Pipeline.Snapshot> // Get a sample of 100 documents in a database results = db.pipeline() .database() .sample(100) .execute() // Randomly shuffle a list of 3 documents results = db.pipeline() .documents( db.collection("cities").document("SF"), db.collection("cities").document("NY"), db.collection("cities").document("DC") ) .sample(3) .execute()
Java
Task<Pipeline.Snapshot> results; // Get a sample of 100 documents in a database results = db.pipeline() .database() .sample(100) .execute(); // Randomly shuffle a list of 3 documents results = db.pipeline() .documents( db.collection("cities").document("SF"), db.collection("cities").document("NY"), db.collection("cities").document("DC") ) .sample(3) .execute();
Python
# Get a sample of 100 documents in a database results = client.pipeline().database().sample(100).execute() # Randomly shuffle a list of 3 documents results = ( client.pipeline() .documents( client.collection("cities").document("SF"), client.collection("cities").document("NY"), client.collection("cities").document("DC"), ) .sample(3) .execute() )
Java
// Get a sample of 100 documents in a database Pipeline.Snapshot results1 = firestore.pipeline().database().sample(100).execute().get(); // Randomly shuffle a list of 3 documents Pipeline.Snapshot results2 = firestore .pipeline() .documents( firestore.collection("cities").document("SF"), firestore.collection("cities").document("NY"), firestore.collection("cities").document("DC")) .sample(3) .execute() .get();
प्रतिशत मोड
प्रतिशत मोड में, हर दस्तावेज़ के लिए percent प्रतिशत संभावना तय की जाती है कि वह नतीजे के तौर पर दिखेगा. दस्तावेज़ मोड के उलट, यहां क्रम रैंडम नहीं होता. इसके बजाय, यह पहले से मौजूद दस्तावेज़ के क्रम को बनाए रखता है. यह प्रतिशत इनपुट, 0.0 और 1.0 के बीच की डबल वैल्यू होनी चाहिए.
हर दस्तावेज़ को अलग-अलग चुना जाता है. इसलिए, आउटपुट अलग-अलग हो सकता है. औसतन, #documents * percent / 100 दस्तावेज़ दिखाए जाएंगे.
उदाहरण के लिए, इस कलेक्शन के लिए:
Node.js
await db.collection('cities').doc('SF').set({name: 'San Francsico', state: 'California'});
await db.collection('cities').doc('NYC').set({name: 'New York City', state: 'New York'});
await db.collection('cities').doc('CHI').set({name: 'Chicago', state: 'Illinois'});
await db.collection('cities').doc('ATL').set({name: 'Atlanta', state: 'Georgia'});
प्रतिशत मोड में सैंपल स्टेज का इस्तेमाल, कलेक्शन स्टेज से (औसतन) 50% दस्तावेज़ों को वापस पाने के लिए किया जा सकता है.
Node.js
const sampled = await db.pipeline()
.collection("/cities")
.sample({ percent: 0.5 })
.execute();
इससे cities कलेक्शन से, औसतन 50% दस्तावेज़ों का एक ऐसा सैंपल मिलेगा जो गैर-निर्धारित होगा. यहां एक संभावित आउटपुट दिया गया है.
{name: 'New York City', state: 'New York'}
{name: 'Chicago', state: 'Illinois'}
प्रतिशत मोड में, हर दस्तावेज़ के चुने जाने की संभावना एक जैसी होती है. इसलिए, ऐसा हो सकता है कि कोई भी दस्तावेज़ न मिले या सभी दस्तावेज़ मिल जाएं.
क्लाइंट के उदाहरण
Web
// Get a sample of on average 50% of the documents in the database const results = await execute(db.pipeline() .database() .sample({ percentage: 0.5 }) );
Swift
// Get a sample of on average 50% of the documents in the database let results = try await db.pipeline() .database() .sample(percentage: 0.5) .execute()
Kotlin
// Get a sample of on average 50% of the documents in the database val results = db.pipeline() .database() .sample(SampleStage.withPercentage(0.5)) .execute()
Java
// Get a sample of on average 50% of the documents in the database Task<Pipeline.Snapshot> results = db.pipeline() .database() .sample(SampleStage.withPercentage(0.5)) .execute();
Python
from google.cloud.firestore_v1.pipeline_stages import SampleOptions # Get a sample of on average 50% of the documents in the database results = ( client.pipeline().database().sample(SampleOptions.percentage(0.5)).execute() )
Java
// Get a sample of on average 50% of the documents in the database Pipeline.Snapshot results = firestore.pipeline().database().sample(Sample.withPercentage(0.5)).execute().get();