नमूना

ब्यौरा

यह फ़ंक्शन, पिछले चरण के नतीजों से एक नॉन-डिटरमिनिस्टिक सैंपल दिखाता है.

ये दो मोड काम करते हैं:

  • documents: n दस्तावेज़ों को रैंडम तरीके से चुनें.
  • percent: n प्रतिशत दस्तावेज़ों को रैंडम तरीके से चुनें.

उदाहरण

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();
जाएं
// Get a sample of 100 documents in a database
results1, err := client.Pipeline().Database().Sample(firestore.WithDocLimit(100)).Execute(ctx).Results().GetAll()
if err != nil {
	fmt.Fprintf(w, "GetAll failed: %v", err)
	return err
}

// Randomly shuffle a list of 3 documents
results2, err := client.Pipeline().
	Documents([]*firestore.DocumentRef{
		client.Collection("cities").Doc("SF"),
		client.Collection("cities").Doc("NY"),
		client.Collection("cities").Doc("DC"),
	}).
	Sample(firestore.WithDocLimit(3)).
	Execute(ctx).Results().GetAll()
if err != nil {
	fmt.Fprintf(w, "GetAll failed: %v", err)
	return err
}

मोड

दस्तावेज़ मोड

documents मोड, इनपुट से n दस्तावेज़ों को रैंडम तरीके से चुनता है. इसमें हर दस्तावेज़ (साथ ही, दस्तावेज़ों का क्रम) को चुने जाने की संभावना बराबर होती है. इसके लिए, Cloud Firestore को अब भी सभी दस्तावेज़ों को स्कैन और प्रोसेस करना होगा. इसलिए, यह अब भी एक महंगा ऑपरेशन हो सकता है.

उदाहरण के लिए, इस कलेक्शन के लिए:

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" }

प्रतिशत मोड

percent मोड, इनपुट के तौर पर दिए गए सभी दस्तावेज़ों में से n प्रतिशत दस्तावेज़ों को चुनने की कोशिश करता है. इस वजह से, स्टेज में करीब # documents * percent / 100 दस्तावेज़ जनरेट होते हैं. documents मोड की तरह ही, Cloud Firestore यह पक्का करता है कि हर दस्तावेज़ को बराबर प्राथमिकता दी जाए. इसके लिए, Cloud Firestore को सभी दस्तावेज़ों को स्कैन और प्रोसेस करना होगा. इसलिए, भले ही नतीजों का सेट छोटा हो, लेकिन यह एक महंगी प्रोसेस हो सकती है.

documents मोड के उलट, यहां क्रम को रैंडम नहीं किया जाता. इसके बजाय, दस्तावेज़ों के मौजूदा क्रम को बनाए रखा जाता है. यह प्रतिशत इनपुट, 0.0 और 1.0 के बीच की डबल वैल्यू होनी चाहिए.

उदाहरण के लिए, इस कलेक्शन के लिए:

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"});

प्रतिशत मोड में सैंपल स्टेज का इस्तेमाल, collection(...) स्टेज से (औसतन) 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();
जाएं
// Get a sample of on average 50% of the documents in the database
snapshot := client.Pipeline().
	Database().
	Sample(firestore.WithPercentage(0.5)).
	Execute(ctx)