MongoDB
 sql >> डेटाबेस >  >> NoSQL >> MongoDB

MongoDB में पूर्ण-पाठ खोज कैसे करें

अग्रणी नोएसक्यूएल डेटाबेस में से एक, मोंगोडीबी अपने तेज प्रदर्शन, बहुमुखी स्कीमा, स्केलेबिलिटी और इंडेक्सिंग के लिए महान क्षमताओं के लिए जाना जाता है। कुछ विवरणों में जाने से पहले आइए कुछ संदर्भ देखें। जब हम इंटरनेट पर सामग्री खोजने की बात करते हैं तो पूर्ण-पाठ खोज एक आवश्यक विशेषता है। जब हम वाक्यांशों या खोजशब्दों का उपयोग करते हुए सामग्री देखते हैं तो एक Google खोज इसका सबसे अच्छा उदाहरण है। इस लेख में, हम टेक्स्ट इंडेक्स के आधार पर MongoDB में पूर्ण-पाठ खोज क्षमताओं के बारे में जानेंगे।

नमूना डेटाबेस बनाएं

शुरू करने से पहले, हम एक नमूना डेटाबेस तैयार करेंगे जिसका उपयोग ट्यूटोरियल के दौरान किया जाएगा।

हम myDB . नाम से एक डेटाबेस तैयार करेंगे और किताबें . नाम से एक संग्रह बनाएं . इसके लिए कथन इस प्रकार होगा।

> use myDB
> db.createCollection("books")
>

आइए निम्नलिखित कथन का उपयोग करके कुछ दस्तावेज़ सम्मिलित करें।

> db.books.insert([
	{
      "title": "Eloquent JavaScript, Second Edition",
      "subtitle": "A Modern Introduction to Programming",
      "author": "Marijn Haverbeke",
      "publisher": "No Starch Press",
      "description": "JavaScript lies at the heart of almost every modern web application, from social apps to the newest browser-based games. Though simple for beginners to pick up and play with, JavaScript is a flexible, complex language that you can use to build full-scale applications."
    },
    {
      "title": "Learning JavaScript Design Patterns",
      "subtitle": "A JavaScript and jQuery Developer's Guide",
      "author": "Addy Osmani",
      "publisher": "O'Reilly Media",
      "description": "With Learning JavaScript Design Patterns, you'll learn how to write beautiful, structured, and maintainable JavaScript by applying classical and modern design patterns to the language. If you want to keep your code efficient, more manageable, and up-to-date with the latest best practices, this book is for you."
    },
    {
      "title": "Speaking JavaScript",
      "subtitle": "An In-Depth Guide for Programmers",
      "author": "Axel Rauschmayer",
      "publisher": "O'Reilly Media",
      "description": "Like it or not, JavaScript is everywhere these days, from browser to server to mobile and now you, too, need to learn the language or dive deeper than you have. This concise book guides you into and through JavaScript, written by a veteran programmer who once found himself in the same position."
    },
    {
      "title": "Programming JavaScript Applications",
      "subtitle": "Robust Web Architecture with Node, HTML5, and Modern JS Libraries",
      "author": "Eric Elliott",
      "publisher": "O'Reilly Media",
      "description": "Take advantage of JavaScript's power to build robust web-scale or enterprise applications that are easy to extend and maintain. By applying the design patterns outlined in this practical book, experienced JavaScript developers will learn how to write flexible and resilient code that's easier-yes, easier-to work with as your codebase grows."
    },
    {
      "title": "Understanding ECMAScript 6",
      "subtitle": "The Definitive Guide for JavaScript Developers",
      "author": "Nicholas C. Zakas",
      "publisher": "No Starch Press",
      "description": "ECMAScript 6 represents the biggest update to the core of JavaScript in the history of the language. In Understanding ECMAScript 6, expert developer Nicholas C. Zakas provides a complete guide to the object types, syntax, and other exciting changes that ECMAScript 6 brings to JavaScript."
    }
])

एक टेक्स्ट इंडेक्स बनाना

टेक्स्ट सर्च करने के लिए हमें फ़ील्ड्स पर टेक्स्ट इंडेक्स बनाना होगा। हम इसे सिंगल या मल्टीपल फील्ड पर बना सकते हैं। निम्नलिखित कथन एक ही फ़ील्ड पर एक टेक्स्ट इंडेक्स बनाएगा।

>db.books.createIndex({"description":"text"})

हम विवरण . पर एक टेक्स्ट इंडेक्स बनाएंगे और उपशीर्षक इस ट्यूटोरियल के लिए फ़ील्ड। हम MongoDB में प्रति संग्रह केवल एक टेक्स्ट इंडेक्स बना सकते हैं। इसलिए हम निम्नलिखित कथन का उपयोग करके एक मिश्रित टेक्स्ट इंडेक्स बनाएंगे।

>db.books.createIndex({"subtitle":"text","description":"text"})

$खोज

अब हम उन दस्तावेज़ों को खोजने का प्रयास करेंगे जिनमें विवरण . में कीवर्ड 'ECMAScript' हैं और उपशीर्षक खेत। इसके लिए हम नीचे दिए गए कथन का उपयोग कर सकते हैं।

db.books.find({$text: {$search: "ECMAScript"}})

उदाहरण

>db.books.find({$text: {$search: "ECMAScript"}},{ subtitle: 1, description: 1 })
	{
    "_id" : ObjectId("602b09cb3cb6144ada1c62fe"),
    "subtitle" : "The Definitive Guide for JavaScript Developers",
    "description" : "ECMAScript 6 represents the biggest update to the core of JavaScript in the history of the language. In Understanding ECMAScript 6, expert developer Nicholas C. Zakas provides a complete guide to the object types, syntax, and other exciting changes that ECMAScript 6 brings to JavaScript."
	}
>

वाक्यांश

आप टेक्स्ट इंडेक्स का उपयोग करके वाक्यांश खोज सकते हैं। डिफ़ॉल्ट रूप से, पाठ खोज वाक्यांश में सभी शब्दों के लिए OR खोज करता है। यदि आप 'आधुनिक डिज़ाइन पैटर्न' खोजना चाहते हैं, तो यह आधुनिक, डिज़ाइन या पैटर्न कीवर्ड वाले दस्तावेज़ों की खोज करेगा।

उदाहरण

>db.books.find({$text: {$search: "modern design patterns"}},{ subtitle: 1, description: 1 })
	{
    "_id" : ObjectId("602b098f3cb6144ada1c2ea1"),
    "subtitle" : "A JavaScript and jQuery Developer's Guide",
    "description" : "With Learning JavaScript Design Patterns, you'll learn how to write beautiful, structured, and maintainable JavaScript by applying classical and modern design patterns to the language. If you want to keep your code efficient, more manageable, and up-to-date with the latest best practices, this book is for you."
	},
	{
    "_id" : ObjectId("602b09b93cb6144ada1c4bca"),
    "subtitle" : "Robust Web Architecture with Node, HTML5, and Modern JS Libraries",
    "description" : "Take advantage of JavaScript's power to build robust web-scale or enterprise applications that are easy to extend and maintain. By applying the design patterns outlined in this practical book, experienced JavaScript developers will learn how to write flexible and resilient code that's easier-yes, easier-to work with as your code base grows.",
	},
	{
    "_id" : ObjectId("602b095c3cb6144ada1c1028"),
    "subtitle" : "A Modern Introduction to Programming",
    "description" : "JavaScript lies at the heart of almost every modern web application, from social apps to the newest browser-based games. Though simple for beginners to pick up and play with, JavaScript is a flexible, complex language that you can use to build full-scale applications."
	}
>

यदि आप 'आधुनिक डिज़ाइन पैटर्न' वाले दस्तावेज़ों जैसे सटीक वाक्यांशों को एक साथ खोजना चाहते हैं, तो आप खोज टेक्स्ट में दोहरे उद्धरण निर्दिष्ट करके ऐसा कर सकते हैं।

उदाहरण

>db.books.find({$text: {$search: "\"modern design patterns\""}},{ subtitle: 1, description: 1 })
	{
    "_id" : ObjectId("602b098f3cb6144ada1c2ea1"),
    "subtitle" : "A JavaScript and jQuery Developer's Guide",
    "description" : "With Learning JavaScript Design Patterns, you'll learn how to write beautiful, structured, and maintainable JavaScript by applying classical and modern design patterns to the language. If you want to keep your code efficient, more manageable, and up-to-date with the latest best practices, this book is for you."
}

नकारात्मकता

यदि आप किसी विशेष शब्द वाले दस्तावेज़ों को बाहर करना चाहते हैं, तो आप एक निषेध खोज का उपयोग कर सकते हैं। उदाहरण के लिए यदि आप 'जावास्क्रिप्ट' के साथ सभी दस्तावेज़ों की खोज करने जा रहे हैं, लेकिन 'एचटीएमएल 5' या 'ईसीएमएस्क्रिप्ट' नहीं, तो आप नीचे दिए गए उदाहरण के रूप में खोज सकते हैं।

उदाहरण

>db.books.find({$text: {$search: "JavaScript -HTML5 -ECMAScript"}},{ subtitle: 1, description: 1 })
	{
    "_id" : ObjectId("602b098f3cb6144ada1c2ea1"),
    "subtitle" : "A JavaScript and jQuery Developer's Guide",
    "description" : "With Learning JavaScript Design Patterns, you'll learn how to write beautiful, structured, and maintainable JavaScript by applying classical and modern design patterns to the language. If you want to keep your code efficient, more manageable, and up-to-date with the latest best practices, this book is for you."
	},
	{
    "_id" : ObjectId("602b09a83cb6144ada1c4973"),
    "subtitle" : "An In-Depth Guide for Programmers",
    "description" : "Like it or not, JavaScript is everywhere these days, from browser to server to mobile and now you, too, need to learn the language or dive deeper than you have. This concise book guides you into and through JavaScript, written by a veteran programmer who once found himself in the same position."
	},
	{
    "_id" : ObjectId("602b095c3cb6144ada1c1028"),
    "subtitle" : "A Modern Introduction to Programming",
    "description" : "JavaScript lies at the heart of almost every modern web application, from social apps to the newest browser-based games. Though simple for beginners to pick up and play with, JavaScript is a flexible, complex language that you can use to build full-scale applications."
	}

टेक्स्ट सर्च स्कोर

पाठ खोज प्रत्येक दस्तावेज़ को खोज क्वेरी के साथ दस्तावेज़ की प्रासंगिकता का प्रतिनिधित्व करने के लिए एक अंक प्रदान करता है। इस स्कोर का उपयोग खोज परिणाम में लौटाए गए सभी रिकॉर्ड को सॉर्ट करने के लिए किया जा सकता है। एक उच्च स्कोर सबसे अधिक प्रासंगिक मैच का संकेत देगा।

उदाहरण

>db.books.find({$text: {$search: "JavaScript "}},{score: {$meta: "textScore"}, subtitle: 1, description: 1 }).sort({score:{$meta:"textScore"}})
	{
    "_id" : ObjectId("602b098f3cb6144ada1c2ea1"),
    "subtitle" : "A JavaScript and jQuery Developer's Guide",
    "description" : "With Learning JavaScript Design Patterns, you'll learn how to write beautiful, structured, and maintainable JavaScript by applying classical and modern design patterns to the language. If you want to keep your code efficient, more manageable, and up-to-date with the latest best practices, this book is for you.",
    "score" : 1.43269230769231
	},
	{
    "_id" : ObjectId("602b09cb3cb6144ada1c62fe"),
    "subtitle" : "The Definitive Guide for JavaScript Developers",
    "description" : "ECMAScript 6 represents the biggest update to the core of JavaScript in the history of the language. In Understanding ECMAScript 6, expert developer Nicholas C. Zakas provides a complete guide to the object types, syntax, and other exciting changes that ECMAScript 6 brings to JavaScript.",
    "score" : 1.42672413793103
	},
	{
    "_id" : ObjectId("602b09a83cb6144ada1c4973"),
    "subtitle" : "An In-Depth Guide for Programmers",
    "description" : "Like it or not, JavaScript is everywhere these days, from browser to server to mobile and now you, too, need to learn the language or dive deeper than you have. This concise book guides you into and through JavaScript, written by a veteran programmer who once found himself in the same position.",
    "score" : 0.818181818181818
	},
	{
    "_id" : ObjectId("602b095c3cb6144ada1c1028"),
    "subtitle" : "A Modern Introduction to Programming",
    "description" : "JavaScript lies at the heart of almost every modern web application, from social apps to the newest browser-based games. Though simple for beginners to pick up and play with, JavaScript is a flexible, complex language that you can use to build full-scale applications.",
    "score" : 0.801724137931034
	},
	{
    "_id" : ObjectId("602b09b93cb6144ada1c4bca"),
    "subtitle" : "Robust Web Architecture with Node, HTML5, and Modern JS Libraries",
    "description" : "Take advantage of JavaScript's power to build robust web-scale or enterprise applications that are easy to extend and maintain. By applying the design patterns outlined in this practical book, experienced JavaScript developers will learn how to write flexible and resilient code that's easier-yes, easier-to work with as your codebase grows.",
    "score" : 0.792857142857143
	}

स्टॉप वर्ड्स

$text ऑपरेटर भाषा-विशिष्ट स्टॉप शब्दों को फ़िल्टर करता है, जैसे a, a, the और अंग्रेज़ी में। नीचे दी गई खोज परिणाम में कोई दस्तावेज़ नहीं लौटाएगी।

उदाहरण

>db.books.find({$text: {$search: "is"}},{subtitle: 1, description: 1 })
	Fetched 0 record(s)

तना हुआ शब्द

$text ऑपरेटर पूर्ण स्टेम वाले शब्द से मेल खाता है। इसलिए यदि किसी दस्तावेज़ फ़ील्ड में शब्द सीखना या सीखना शामिल है, तो शब्द सीखने या सीखने पर एक ही परिणाम होगा।

उदाहरण

>db.books.find({$text: {$search: " learn"}},{subtitle: 1, description: 1 }) or >db.books.find({$text: {$search: " learning"}},{subtitle: 1, description: 1 })
	{
    "_id" : ObjectId("602b098f3cb6144ada1c2ea1"),
    "subtitle" : "A JavaScript and jQuery Developer's Guide",
    "description" : "With Learning JavaScript Design Patterns, you'll learn how to write beautiful, structured, and maintainable JavaScript by applying classical and modern design patterns to the language. If you want to keep your code efficient, more manageable, and up-to-date with the latest best practices, this book is for you."
	},
	{
    "_id" : ObjectId("602b09a83cb6144ada1c4973"),
    "subtitle" : "An In-Depth Guide for Programmers",
    "description" : "Like it or not, JavaScript is everywhere these days, from browser to server to mobile and now you, too, need to learn the language or dive deeper than you have. This concise book guides you into and through JavaScript, written by a veteran programmer who once found himself in the same position."
	},
	{
    "_id" : ObjectId("602b09b93cb6144ada1c4bca"),
    "subtitle" : "Robust Web Architecture with Node, HTML5, and Modern JS Libraries",
    "description" : "Take advantage of JavaScript's power to build robust web-scale or enterprise applications that are easy to extend and maintain. By applying the design patterns outlined in this practical book, experienced JavaScript developers will learn how to write flexible and resilient code that's easier-yes, easier-to work with as your codebase grows."
	}

निष्कर्ष

मुझे आशा है कि आपने आज कुछ नया सीखा। यहाँ स्व-होस्टेड MongoDB पर एक दिलचस्प लेख है। मैं आपको अपने दम पर सामान आज़माने और टिप्पणी अनुभाग में अपना अनुभव साझा करने के लिए भी आमंत्रित करता हूं। इसके अलावा, यदि आपको उपरोक्त किसी भी परिभाषा में कोई समस्या आती है, तो कृपया नीचे टिप्पणी अनुभाग में मुझसे बेझिझक पूछें।


  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. सरणी के अंदर सरणी के लिए मानगो धक्का

  2. Mongodb, $lookup . के साथ कुल क्वेरी

  3. AWS पर MongoDB:अपने MongoDB सर्वर के लिए सही EC2 इंस्टेंस प्रकार कैसे चुनें?

  4. Mongoose कॉलबैक में कौन से पैरामीटर पास किए जाते हैं

  5. मोंगोडीबी $mul