نموذج

الوصف

تعرض هذه الدالة عيّنة غير قطعية من نتائج المرحلة السابقة.

هناك وضعان متاحان:

  • يتيح وضع 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();

سيؤدي ذلك إلى الحصول على عيّنة غير حتمية من (في المتوسط) %50 من المستندات من المجموعة 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 })
);
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();