मुख्य सामग्री पर जाएं

Hybrid Search: Combining Vector and Keyword Queries in MongoDB

Learn about hybrid search and how to utilize it in MongoDB.
अपडेट किया गया 25 सित॰ 2026  · 6 मि॰ पढ़ें

AI के साथ खोजें

ChatGPTClaudePerplexity

कभी-कभी, साधारण फुल-टेक्स्ट सर्च या सिर्फ वेक्टर सर्च अकेले किसी डेटाबेस पर सही ढंग से क्वेरी करने और मनचाहे नतीजे पाने के लिए पर्याप्त नहीं होती। बड़े पैमाने पर मल्टीमॉडल, असंरचित डेटा से जूझ रहे डेवलपर्स के लिए दोनों का संयोजन शानदार है, क्योंकि ऐसे डेटा को दोनों तरह की सर्च से लाभ मिलता है। इसे हाइब्रिड सर्च कहा जाता है, और यह डेवलपर्स को एक कठिन चुनौती का बेहतरीन समाधान प्रदान करता है। 

हाइब्रिड सर्च वास्तव में है क्या? 

हाइब्रिड सर्च को सही ढंग से समझने के लिए, पहले हमें फुल-टेक्स्ट सर्च समझनी होगी। 

फुल-टेक्स्ट सर्च वह तरीका है जिसमें आपकी क्वेरी के शब्दों का सीधा मिलान आपके डॉक्यूमेंट्स से किया जाता है। इस तरह की पारंपरिक सर्च से कई डेवलपर्स भली-भांति परिचित हैं।

उदाहरण के लिए, अगर आप “आउटडोर सीटिंग वाला क्यूट कैफे” खोजते हैं, तो आपका सर्च इंजन डेटाबेस के अंदर इन्हीं सटीक शब्दों को ढूंढेगा। सरल शब्दों में, फुल-टेक्स्ट सर्च बेहद सटीक और कुशल है, लेकिन यह समानार्थी शब्द, पैराफ्रेशिंग, या क्वेरी में टाइपो होने पर समान नतीजे देने में अच्छी तरह काम नहीं करती। 

दूसरी ओर, वेक्टर सर्च सभी डेटा को संख्याओं या एम्बेडिंग्स में बदल देती है। यानी, सटीक शब्दों के मिलान के बजाय, वेक्टर सर्च आपकी क्वेरी के अर्थगत मतलब की तुलना डेटाबेस में संग्रहीत डॉक्यूमेंट्स से करती है। 

“आउटडोर सीटिंग वाला क्यूट कैफे” खोजने पर “पेस्ट्रीज़ और बाहर कॉफी” जैसे नतीजे आ सकते हैं, भले ही सटीक शब्द समान न हों। वेक्टर सर्च न केवल अर्थ-आधारित है; यह अत्यंत लचीली भी है, पर कभी-कभी निर्दिष्ट क्वेरी के आधार पर बहुत व्यापक नतीजे भी लौटा सकती है। 

तो हाइब्रिड सर्च कहाँ काम आती है? यह फुल-टेक्स्ट सर्च और वेक्टर सर्च—दोनों को जोड़ती है। इसका मतलब है कि डेवलपर्स वेक्टर की अर्थगत बुद्धिमत्ता का लाभ उठा सकते हैं और साथ ही फुल-टेक्स्ट सर्च की बेहद सटीक फ़िल्टरिंग क्षमताएँ भी बनाए रखते हैं। यानी वास्तव में दोनों दुनिया का सर्वश्रेष्ठ। बड़े असंरचित डेटासेट्स पर काम करते समय यह डेवलपर्स के लिए बेहद उपयोगी है। 

हाइब्रिड सर्च क्यों महत्वपूर्ण है 

हाइब्रिड सर्च कई वास्तविक दुनिया की स्थितियों में फायदेमंद है, जिनमें ई-कॉमर्स, स्वास्थ्य सेवा और यहां तक कि भर्ती भी शामिल हैं (पर सीमित नहीं)। 

ई-कॉमर्स में, कल्पना करें कि आप अपनी पसंदीदा वेबसाइट पर “$100 से कम की आरामदायक डेस्क चेयर” खोजते हैं। वेक्टर सर्च के साथ, आपको अर्थगत रूप से समान आइटम (जैसे एर्गोनोमिक ऑफिस चेयर्स) दिखेंगे, जबकि फुल-टेक्स्ट सर्च “chair” शब्द पर केंद्रित रहेगी और मूल्य सीमा लागू कराने में मदद करेगी। 

भर्ती में, यदि एक रिक्रूटर “NLP अनुभव वाले इंजीनियर” के लिए रिज़्यूमे खोज रहा है, तो हाइब्रिड सर्च “natural language processing” और सटीक कीवर्ड “engineer”—दोनों को पकड़ पाएगी।

इसका मतलब है कि आप किसी एक तरीके की तुलना में अधिक प्रासंगिक और भरोसेमंद नतीजे प्राप्त कर पाते हैं। 

MongoDB में हाइब्रिड सर्च

आइए MongoDB Atlas में हाइब्रिड सर्च करना सीखते हैं। इस ट्यूटोरियल में सफल होने के लिए, आपको कुछ पूर्वापेक्षाएँ चाहिए: 

  1. एक MongoDB Atlas खाता  
  2. एक फ्री-टियर MongoDB Atlas क्लस्टर
  3. mongosh आपके टर्मिनल में डाउनलोड हो

इस ट्यूटोरियल में, हम अपने sample_mflix डेटाबेस की embedded movies कलेक्शन पर केंद्रित रहेंगे, और इस $rankFusion हाइब्रिड सर्च ट्यूटोरियल के साथ चलेंगे, कुछ बातों का ध्यान रखते हुए। 

जहाँ नया $rankFusion ऑपरेटर 8.1+ वर्ज़न वाले क्लस्टर्स में उपलब्ध है और यह MongoDB में हाइब्रिड सर्च को बेहद आसान बना देता है, वहीं यह अभी Public Preview में है और फ्री टियर क्लस्टर्स अपने-अप 8.0 वर्ज़न पर होते हैं। तो, हम इस ट्यूटोरियल को फॉलो करेंगे, कुछ चीज़ें बदलेंगे, और अच्छे से पूरा करेंगे।

अपने क्लस्टर की प्रोविज़निंग करें

क्लस्टर प्रोविज़न करते समय, कृपया सुनिश्चित करें कि आप sample_mflix डेटासेट डाउनलोड कर रहे हैं। यही वह डेटासेट है जिस पर हम पूरे ट्यूटोरियल में काम करेंगे। हमारा फोकस sample_mflix.embedded_movies कलेक्शन पर है। 

जल्दी से जाँच लें कि आपके पास mongosh डाउनलोड है।

mongosh --version

इस ट्यूटोरियल के लिए, मैं 2.4.2 का उपयोग कर रहा/रही हूँ। 

अब, अपने MongoDB Atlas क्लस्टर से कनेक्ट करें:

mongosh "mongodb+srv://<yourclusterhere>.mongodb.net/" --apiVersion 1 --username <yourusername> 

आपसे पासवर्ड माँगा जाएगा, और कनेक्ट होने पर यह इस तरह दिखेगा:

अब, हम अपने डेटाबेस से कनेक्ट कर सकते हैं। कमांड चलाएँ:

use sample_mflix

आउटपुट होगा: 

switched to db sample_mflix

अपने इंडेक्स बनाएँ

हम इस ट्यूटोरियल के लिए आवश्यक दोनों इंडेक्स बना सकते हैं—एक वेक्टर इंडेक्स और एक फुल-टेक्स्ट सर्च इंडेक्स। ध्यान रखें कि हम ये दोनों इंडेक्स अपनी embedded_movies कलेक्शन पर बना रहे हैं। 

वेक्टर इंडेक्स:

db.embedded_movies.createSearchIndex(
  "hybrid-vector-search",
  "vectorSearch",
  {
    fields: [
      { type: "vector", path: "plot_embedding_voyage_3_large", numDimensions: 2048, similarity: "dotProduct" }
    ]
  }
)

फुल-टेक्स्ट सर्च इंडेक्स:

db.embedded_movies.createSearchIndex(
  "hybrid-full-text-search",
  "search",
  { mappings: { dynamic: true } }
)

दोबारा जाँच लें और सुनिश्चित करें कि आपके MongoDB Atlas क्लस्टर के अंदर इंडेक्स तैयार हैं:

अपने डेटाबेस को क्वेरी करें

अब, हम अपनी sample_mflix.embedded_movies कलेक्शन में plot_embedding_voyage_3_large फ़ील्ड में “star wars” के लिए क्वेरी करना चाहते हैं। 

ट्यूटोरियल के अनुसार, चूँकि हमें कोई API इस्तेमाल नहीं करनी, हम सभी आवश्यक एम्बेडिंग्स को query_embeddings.js नाम की अलग फ़ाइल में सेव कर सकते हैं। 

अब हम क्वेरी में उपयोग के लिए अपनी एम्बेडिंग्स लोड कर सकते हैं। निम्न चलाएँ:

load('/Users/<PATH NAME>/query_embeddings.js')
const queryVec = STAR_WARS_EMBEDDING

यह सुनिश्चित करने के लिए कि एम्बेडिंग्स सफलतापूर्वक लोड हो गई हैं, कमांड चलाएँ:

STAR_WARS_EMBEDDING.length

2048 का आउटपुट सही है। 

एग्रीगेशन पाइपलाइन

अब, हम हाइब्रिड सर्च के लिए एक एग्रीगेशन पाइपलाइन बना सकते हैं। कृपया ध्यान रखें कि कुछ सीमाएँ और कारण हैं जिनकी वजह से हमें पाइपलाइन को इसी तरह बनाना पड़ा। 

जैसा ऊपर कहा गया, पहला कारण यह है कि MongoDB Atlas में $rankFusion मौजूद होने के बावजूद यह फिलहाल Public Preview में है, और इसके लिए 8.0 या उससे उच्च वर्ज़न का क्लस्टर चाहिए। फ्री टियर क्लस्टर के साथ आप इस समय $rankFusion का उपयोग नहीं कर सकते। इससे चीज़ें थोड़ी जटिल हो जाती हैं क्योंकि MongoDB Atlas हमारे लिए टेक्स्ट और वेक्टर रैंकिंग को मूल रूप से मर्ज नहीं करेगा। इसका मतलब है कि हमें अपनी खुद की फ्यूज़न करनी होगी, या दोनों रैंक्ड लिस्ट्स का वेटेड ब्लेंड बनाना होगा। 

एग्रीगेशन पाइपलाइन्स में स्टेज की प्लेसमेंट भी सख्त होती है:

  • $search किसी पाइपलाइन का पहला स्टेज होना चाहिए। 
  • $vectorSearch पाइपलाइन की शुरुआत में होना चाहिए और $facet के अंदर नहीं हो सकता।

एक वर्कअराउंड? $unionWith के अंदर हमारी सब-पाइपलाइन में $vectorSearch पहला स्टेज हो सकता है, क्योंकि यह सब-पाइपलाइन अपनी अलग पाइपलाइन मानी जाती है। 

इसी कारण, हमें फुल-टेक्स्ट सर्च के नतीजों का स्कोर बनाए रखने के लिए $search से शुरू करना होगा, और फिर $unionWith का उपयोग करके दूसरी मिनी-पाइपलाइन शुरू करनी होगी जो $vectorSearch से शुरू होती है, ताकि उसका स्कोर भी बना रहे। 

इसके बाद हमें _id के आधार पर ग्रुप करना होगा ताकि कोई डुप्लिकेट न रहे और हम $search और $vectorSearch—दोनों के सर्वश्रेष्ठ को उपयोग कर सकें। 

एक बार यह हो जाने पर, हमें हाइब्रिड सर्च निकालनी है—यानी हमारे $search से सर्वश्रेष्ठ, $vectorSearch से सर्वश्रेष्ठ, फिर उसे हमारी चुनी हुई वेट (मैंने 0.35 ली) से गुणा करना, और उसके अनुसार सॉर्ट करना। 

वेट यह निर्धारित करती है कि अंतिम नतीजों में फुल-टेक्स्ट सर्च की तुलना में वेक्टर सर्च का कितना प्रभाव होगा। 

हमारा हाइब्रिड फ़ॉर्मूला है: hybrid_score = text_score + (vector_score x weight)। तो, अधिक वेट (जैसे, 0.7–1.0) का मतलब है कि वेक्टर सर्च हावी रहेगी और अर्थगत समानता को प्राथमिकता मिलेगी। 

दूसरी ओर, कम वेट (0.1–0.3) का अर्थ है कि फुल-टेक्स्ट सर्च हावी रहेगी और सटीक कीवर्ड मैच को प्राथमिकता देगी। 

इस एग्रीगेशन पाइपलाइन को अपने टर्मिनल विंडो में कॉपी-पेस्ट करें:

const WEIGHT = 0.35;

db.embedded_movies.aggregate([
// Stage 1: full-text search
  { $search: { index: "hybrid-full-text-search", text: { query: "star wars", path: ["title","plot"] } } },
  { $set: { t: { $meta: "searchScore" } } },
  { $project: { _id: 1, title: 1, plot: 1, t: 1 } },
// Stage 2: Union with vector search
  { $unionWith: {
      coll: "embedded_movies",
      pipeline: [
// Vector search subpipeline
        { $vectorSearch: {
            index: "hybrid-vector-search",
            path: "plot_embedding_voyage_3_large",
            queryVector: queryVec,
            numCandidates: 200,
            limit: 150
        }},
        { $project: { _id: 1, title: 1, plot: 1, v: { $meta: "vectorSearchScore" } } }
      ]
  }},
// Stage 3: Combine and rank results
  { $group: { _id: "$_id", title: { $first: "$title" }, plot: { $first: "$plot" }, t: { $max: "$t" }, v: { $max: "$v" } } },
  { $set: { h: { $add: [ { $ifNull: ["$t", 0] }, { $multiply: [ { $ifNull: ["$v", 0] }, WEIGHT ] } ] } } },
  { $sort: { h: -1 } },
  { $limit: 20 }
]).toArray()

हमारा आउटपुट इस प्रकार है:

  {
    _id: ObjectId('573a139af29313caabcf124d'),
    title: 'Star Wars: Episode III - Revenge of the Sith',
    plot: 'As the Clone Wars near an end, the Sith Lord Darth Sidious steps out of the shadows, at which time Anakin succumbs to his emotions, becoming Darth Vader and putting his relationships with Obi-Wan and Padme at risk.',
    t: 4.957413673400879,
    v: 0.756283700466156,
    h: 5.2221129685640335
  },
  {
    _id: ObjectId('573a13a6f29313caabd17d08'),
    title: 'Star',
    plot: 'The Driver now carries an arrogant rock star who is visiting a major city (not Pittsburgh as earlier believed). Played by Madonna, this title character wants to get away from her bodyguards...',
    t: 5.151784420013428,
    v: null,
    h: 5.151784420013428
  },
  {
    _id: ObjectId('573a1397f29313caabce8cdb'),
    title: 'Star Wars: Episode VI - Return of the Jedi',
    plot: 'After rescuing Han Solo from the palace of Jabba the Hutt, the rebels attempt to destroy the second Death Star, while Luke struggles to make Vader return from the dark side of the Force.',
    t: 4.726564407348633,
    v: 0.7782549858093262,
    h: 4.998953652381897
  },
  {
    _id: ObjectId('573a13aef29313caabd2da15'),
    title: 'Star Runner',
    plot: 'Get ready for the ultimate martial arts competition, where anything goes and lives are bought and sold. Tank is the celebrated Champion Star Runner and is deemed invincible among the ...',
    t: 4.702453136444092,
    v: 0.696668267250061,
    h: 4.9462870299816135
  },
  {
    _id: ObjectId('573a13c0f29313caabd62f62'),
    title: 'Star Wars: The Clone Wars',
    plot: 'Anakin Skywalker and Ahsoka Tano must rescue the kidnapped son of Jabba the Hutt, but political intrigue complicates their mission.',
    t: 4.537533760070801,
    v: 0.7561323642730713,
    h: 4.802180087566375
  },
  {
    _id: ObjectId('573a1397f29313caabce6f53'),
    title: 'Message from Space',
    plot: 'In this Star Wars take-off, the peaceful planet of Jillucia has been nearly wiped out by the Gavanas, whose leader takes orders from his mother (played a comic actor in drag) rather than ...',
    t: 4.401333808898926,
    v: 0.7913960218429565,
    h: 4.678322416543961
  },
  {
    _id: ObjectId('573a139df29313caabcfa90b'),
    title: 'Message from Space',
    plot: 'In this Star Wars take-off, the peaceful planet of Jillucia has been nearly wiped out by the Gavanas, whose leader takes orders from his mother (played a comic actor in drag) rather than ...',
    t: 4.401333808898926,
    v: 0.7913960218429565,
    h: 4.678322416543961
  },
  {
    _id: ObjectId('573a1398f29313caabce9851'),
    title: 'Gymkata',
    plot: 'Johnathan Cabot is a champion gymnast. In the tiny, yet savage, country of Parmistan, there is a perfect spot for a "star wars" site. For the US to get this site, they must compete in the ...',
    t: 4.284864902496338,
    v: 0.7193170189857483,
    h: 4.53662585914135
  },
  {
    _id: ObjectId('573a1397f29313caabce68f6'),
    title: 'Star Wars: Episode IV - A New Hope',
    plot: "Luke Skywalker joins forces with a Jedi Knight, a cocky pilot, a wookiee and two droids to save the universe from the Empire's world-destroying battle-station, while also attempting to rescue Princess Leia from the evil Darth Vader.",
    t: 2.9629626274108887,
    v: 0.7987099885940552,
    h: 3.242511123418808
  },
  {
    _id: ObjectId('573a139af29313caabcf0f5f'),
    title: 'Star Wars: Episode I - The Phantom Menace',
    plot: 'Two Jedi Knights escape a hostile blockade to find allies and come across a young boy who may bring balance to the Force, but the long dormant Sith resurface to reclaim their old glory.',
    t: 2.9629626274108887,
    v: 0.7771433591842651,
    h: 3.2349628031253816
  },
  {
    _id: ObjectId('573a1397f29313caabce77d9'),
    title: 'Star Wars: Episode V - The Empire Strikes Back',
    plot: 'After the rebels have been brutally overpowered by the Empire on their newly established base, Luke Skywalker takes advanced Jedi training with Master Yoda, while his friends are pursued by Darth Vader as part of his plan to capture Luke.',
    t: 2.7174296379089355,
    v: 0.7810181975364685,
    h: 2.9907860070466996
  },
  {
    _id: ObjectId('573a139af29313caabcf1258'),
    title: 'Star Wars: Episode II - Attack of the Clones',
    plot: 'Ten years after initially meeting, Anakin Skywalker shares a forbidden romance with Padmè, while Obi-Wan investigates an assassination attempt on the Senator and discovers a secret clone army crafted for the Jedi.',
    t: 2.7174296379089355,
    v: 0.7400004267692566,
    h: 2.9764297872781755
  },
  {
    _id: ObjectId('573a1394f29313caabcdf65b'),
    title: 'Ugetsu',
    plot: 'A fantastic tale of war, love, family and ambition set in the midst of the Japanese Civil Wars of the sixteenth century.',
    t: 2.858368396759033,
    v: null,
    h: 2.858368396759033
  },
  {
    _id: ObjectId('573a13a4f29313caabd1137f'),
    title: 'S1m0ne',
    plot: "A producer's film is endangered when his star walks off, so he decides to digitally create an actress to substitute for the star, becoming an overnight sensation that everyone thinks is a real person.",
    t: 2.842216730117798,
    v: null,
    h: 2.842216730117798
  },
  {
    _id: ObjectId('573a13b8f29313caabd4c3c3'),
    title: 'Star Trek',
    plot: "The brash James T. Kirk tries to live up to his father's legacy with Mr. Spock keeping him in check as a vengeful, time-traveling Romulan creates black holes to destroy the Federation one planet at a time.",
    t: 2.577816963195801,
    v: 0.7295459508895874,
    h: 2.8331580460071564
  },
  {
    _id: ObjectId('573a13b5f29313caabd42e99'),
    title: 'Sars Wars',
    plot: "The fourth generation of the virus SARS is found in Africa! It's more dangerous and causes the patients to transform into bloodthirsty zombies. The virus quickly lands to Thailand, Dr. ...",
    t: 2.826810598373413,
    v: null,
    h: 2.826810598373413
  },
  {
    _id: ObjectId('573a13b5f29313caabd42e1b'),
    title: 'Sars Wars',
    plot: "The fourth generation of the virus SARS is found in Africa! It's more dangerous and causes the patients to transform into bloodthirsty zombies. The virus quickly lands to Thailand, Dr. ...",
    t: 2.826810598373413,
    v: null,
    h: 2.826810598373413
  }
]).toArray()

As we can see, we have some results that are identical to our query, “star wars,” and other results that are clearly off of meaning.

Conclusion 

Congratulations! You have successfully completed hybrid search in MongoDB. While $rankFusion will simplify this process, this method shows a workaround while the operator is still in Public Preview. Through this tutorial, we have successfully incorporated both full-text search and vector search into one pipeline to retrieve the most optimal results for our given query. For more information on hybrid search in MongoDB, please refer to the MongoDB documentation. If you’re still getting up to speed with MongoDB, I recommend the Introduction to MongoDB in Python course.

MongoDB Hybrid Search FAQs

Do I need MongoDB 8.1 and `$rankFusion` to do hybrid search

No. On 8.0, you can actually fuse the scores yourself. Do this by running $search and $vectorSearch in two pipelines and combine them with $unionWith + $group and a weighted formula. On 8.1+, $rankFusion will do this for you.

What is hybrid search?

It’s combining full-text search (exact words) and vector search (meaning) for the most optimal results possible from a given query.

When should I use hybrid search?

Use hybrid search when your text is varied (synonyms, paraphrases, typos) but you still want exact terms.

How do I pick the weight I want to use?

It’s best practice to begin around 0.2-0.5. The lower is more for full-text search influence, and higher is for semantic influence. It’s important to tune the weight after testing and viewing the results provided.

Can hybrid search work with image and audio data?

Yes! As long as your data can be turned into vector embeddings and you can combine them with any specific wording constraints, you can perform hybrid search on a dataset.

विषय
MongoDB
वेक्टर डेटाबेस

Top DataCamp Courses

{count, plural, one {कोर्स} other {कोर्स}}

Python में MongoDB परिचय

{hours} घंटा
24.4K
MongoDB के साथ लचीले ढंग से संरचित डेटा को हेरफेर और विश्लेषण करना सीखें।
विवरण देखेंRight Arrow
पाठ्यक्रम शुरू करें
और देखेंRight Arrow