sql >> Database >  >> NoSQL >> MongoDB

Zoeken in volledige tekst in MongoDB

MongoDB, een van de toonaangevende NoSQL-databases, staat bekend om zijn snelle prestaties, veelzijdige schema, schaalbaarheid en geweldige mogelijkheden voor indexering. Laten we wat context bekijken voordat we ingaan op enkele details. Zoeken in volledige tekst is een essentiële functie als we het hebben over het vinden van inhoud op internet. Een Google-zoekopdracht is hiervoor het beste voorbeeld wanneer we de inhoud zien met behulp van de woordgroepen of trefwoorden. In dit artikel zullen we leren over de zoekmogelijkheden in volledige tekst in MongoDB op basis van tekstindex.

Maak een voorbeelddatabase

Voordat we beginnen, zullen we een voorbeelddatabase maken die tijdens de tutorial zal worden gebruikt.

We zullen een database maken met de naam myDB en maak een verzameling aan met de naam boeken . Hiervoor zou de verklaring als volgt zijn.

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

Laten we wat documenten invoegen met behulp van de volgende verklaring.

> 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."
    }
])

Een tekstindex maken

We moeten een tekstindex op de velden maken om de tekstzoekopdracht uit te voeren. We kunnen dit op enkele of meerdere velden creëren. De volgende instructie maakt een tekstindex op een enkel veld.

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

We zullen een tekstindex maken op de beschrijving en ondertitel velden voor deze zelfstudie. We kunnen slechts één tekstindex per verzameling maken in MongoDB. We zullen dus een samengestelde tekstindex maken met behulp van de volgende verklaring.

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

Nu zullen we proberen documenten te zoeken die de trefwoorden 'ECMAScript' hebben in de beschrijving en ondertitel velden. Hiervoor kunnen we de onderstaande verklaring gebruiken.

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

Voorbeeld

>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."
	}
>

Zinnen

U kunt naar zinnen zoeken met behulp van de tekstindex. Tekst zoeken voert standaard een OF-zoekopdracht uit voor alle woorden in de zin. Als u 'moderne ontwerppatronen' wilt zoeken, zoekt het naar documenten met de trefwoorden modern, ontwerp of patronen.

Voorbeeld

>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."
	}
>

Als u wilt zoeken naar exacte woordgroepen, zoals documenten met 'moderne ontwerppatronen' samen, kunt u dit doen door dubbele aanhalingstekens op te geven in de zoektekst.

Voorbeeld

>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."
}

Negations

Als u de documenten met een bepaald woord wilt uitsluiten, kunt u een ontkenningszoekopdracht gebruiken. Als u bijvoorbeeld alle documenten gaat doorzoeken met 'JavaScript' maar niet met 'HTML5' of 'ECMAScript', kunt u zoeken zoals in het onderstaande voorbeeld.

Voorbeeld

>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."
	}

Tekstzoekscore

De tekstzoekopdracht geeft een score aan elk document die de relevantie van het document met de zoekopdracht weergeeft. Deze score kan worden gebruikt om alle records die in het zoekresultaat worden geretourneerd, te sorteren. Een hogere score geeft een meest relevante match aan.

Voorbeeld

>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
	}

Stopwoorden

De operator $text filtert de taalspecifieke stopwoorden uit, zoals a, an, the en in het Engels. De onderstaande zoekopdracht levert geen enkel document op in het resultaat.

Voorbeeld

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

Stemmed-woorden

De operator $text komt overeen met het volledige woord met stam. Dus als een documentveld het woord leren of leren bevat, zou een zoekopdracht op de term leren of leren hetzelfde opleveren.

Voorbeeld

>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."
	}

Conclusie

Ik hoop dat je vandaag iets nieuws hebt geleerd. Hier is een interessant artikel over Self-Hosted MongoDB. Ik nodig je ook uit om dingen zelf te proberen en je ervaringen te delen in het commentaargedeelte. Bovendien, als u problemen ondervindt met een van de bovenstaande definities, aarzel dan niet om het mij te vragen in de opmerkingen hieronder.


  1. Hoe voer ik het SQL Join-equivalent uit in MongoDB?

  2. Is het met Redis Cluster mogelijk om de hash-tags gewoon door te geven om te evalueren?

  3. Socket.io-gebruikers tellen op horizontale servers

  4. Is Redis TimeSeries de juiste tool om kandelaars vast te leggen in aandelenkoersen?