$meta - Amazon DocumentDB

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

$meta

$metaOperator digunakan untuk mengakses metadata yang terkait dengan eksekusi kueri saat ini. Operator ini terutama digunakan untuk operasi pencarian teks, di mana metadata dapat memberikan informasi tentang relevansi dokumen yang cocok.

Parameter

  • textScore: Mengambil skor pencarian teks untuk dokumen. Skor ini menunjukkan relevansi dokumen dengan kueri pencarian teks.

Contoh (MongoDB Shell)

Contoh berikut menunjukkan cara menggunakan $meta operator untuk mengambil skor pencarian teks untuk dokumen yang cocok dengan permintaan pencarian teks.

Buat dokumen sampel

db.documents.insertMany([ { _id: 1, title: "Coffee Basics", content: "Coffee is a popular beverage made from roasted coffee beans." }, { _id: 2, title: "Coffee Culture", content: "Coffee coffee coffee - the ultimate guide to coffee brewing and coffee preparation." }, { _id: 3, title: "Tea vs Coffee", content: "Many people prefer tea over coffee for its health benefits." } ]);

Buat indeks teks

db.documents.createIndex({ content: "text" });

Contoh kueri

db.documents.find( { $text: { $search: "coffee" } }, { _id: 0, title: 1, content: 1, score: { $meta: "textScore" } } ).sort({ score: { $meta: "textScore" } });

Keluaran

[ { title: 'Coffee Culture', content: 'Coffee coffee coffee - the ultimate guide to coffee brewing and coffee preparation.', score: 0.8897688388824463 }, { title: 'Coffee Basics', content: 'Coffee is a popular beverage made from roasted coffee beans.', score: 0.75990891456604 }, { title: 'Tea vs Coffee', content: 'Many people prefer tea over coffee for its health benefits.', score: 0.6079270839691162 } ]

Contoh kode

Untuk melihat contoh kode untuk menggunakan $meta perintah, pilih tab untuk bahasa yang ingin Anda gunakan:

Node.js
const { MongoClient } = require('mongodb'); async function findWithTextScore() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const collection = db.collection('documents'); const result = await collection.find( { $text: { $search: "coffee" } }, { projection: { _id: 0, title: 1, content: 1, score: { $meta: "textScore" } } } ).sort({ score: { $meta: "textScore" } }).toArray(); console.log(result); client.close(); } findWithTextScore();
Python
from pymongo import MongoClient client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client['test'] collection = db['documents'] for doc in collection.find( {'$text': {'$search': 'coffee'}}, {'_id': 0, 'title': 1, 'content': 1, 'score': {'$meta': 'textScore'}} ).sort([('score', {'$meta': 'textScore'})]): print(doc) client.close()