নমুনা

বর্ণনা

পূর্ববর্তী ধাপের ফলাফল থেকে একটি অনির্দিষ্ট নমুনা ফেরত দেয়।

দুটি সমর্থিত মোড রয়েছে:

  • 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)
);
সুইফট
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();
পাইথন
# 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()
)
জাভা
// 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();

মোড

ডকুমেন্টস মোড

documents মোড তার ইনপুট থেকে এলোমেলোভাবে সর্বোচ্চ n ডকুমেন্ট বেছে নেয়, যেখানে প্রতিটি ডকুমেন্ট (এবং ডকুমেন্টগুলোর ক্রম) নির্বাচিত হওয়ার সম্ভাবনা সমান থাকে। এটি করার জন্য, Cloud Firestore সমস্ত ডকুমেন্ট স্ক্যান এবং প্রসেস করতে হয়, ফলে এটি একটি ব্যয়বহুল প্রক্রিয়া হয়ে উঠতে পারে।

উদাহরণস্বরূপ, নিম্নলিখিত সংগ্রহটির জন্য:

নোড.জেএস

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

ডকুমেন্ট মোডের স্যাম্পল পর্যায়টি এই সংগ্রহ থেকে ফলাফলের একটি অনির্দিষ্ট উপসেট পুনরুদ্ধার করতে ব্যবহার করা যেতে পারে।

নোড.জেএস

const sampled = await db.pipeline()
    .collection("/cities")
    .sample(1)
    .execute();

এই উদাহরণে, দৈবচয়নের মাধ্যমে শুধুমাত্র ১টি ডকুমেন্ট ফেরত দেওয়া হবে।

  { name: "New York City", state: "New York" }

প্রদত্ত সংখ্যাটি ফেরত আসা মোট নথির সংখ্যার চেয়ে বেশি হলে, সমস্ত নথি এলোমেলো ক্রমে ফেরত দেওয়া হয়।

নোড.জেএস

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 এর মধ্যে একটি ডাবল ভ্যালু হতে হবে।

উদাহরণস্বরূপ, নিম্নলিখিত সংগ্রহটির জন্য:

নোড.জেএস

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

পার্সেন্ট মোডের স্যাম্পল পর্যায়টি ব্যবহার করে কালেকশন পর্যায় থেকে (গড়ে) ৫০% ডকুমেন্ট পুনরুদ্ধার করা যায়।

নোড.জেএস

  const sampled = await db.pipeline()
    .collection("/cities")
    .sample({ percent: 0.5 })
    .execute();

এর ফলে cities সংগ্রহ থেকে (গড়ে) ৫০% নথির একটি অনির্দিষ্ট নমুনা পাওয়া যাবে। নিম্নলিখিতটি একটি সম্ভাব্য আউটপুট।

  { 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 })
);
সুইফট
// 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();
পাইথন
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()
)
জাভা
// 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();