نمونه

توضیحات

یک نمونه غیرقطعی از نتایج مرحله قبل را برمی‌گرداند.

دو حالت پشتیبانی شده وجود دارد:

  • 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();