En af de førende NoSQL-databaser, MongoDB er kendt for sin hurtige ydeevne, alsidige skema, skalerbarhed og fantastiske muligheder for indeksering. Lad os se på noget kontekst, før vi kommer ind på nogle detaljer. Fuldtekstsøgning er en væsentlig funktion, når vi taler om at finde indhold på internettet. En google-søgning er det bedste eksempel på dette, når vi ser indholdet ved hjælp af sætninger eller søgeord. I denne artikel lærer vi om fuldtekstsøgningsmuligheder i MongoDB baseret på tekstindeks.
Opret en prøvedatabase
Før vi begynder, vil vi oprette en eksempeldatabase, som vil blive brugt under selvstudiet.
Vi vil oprette en database med navnet myDB og opret en samling med navnet bøger . Til dette vil erklæringen være som følger.
> use myDB
> db.createCollection("books")
>
Lad os indsætte nogle dokumenter ved at bruge følgende sætning.
> 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."
}
])
Oprettelse af et tekstindeks
Vi skal oprette et tekstindeks på felterne for at udføre tekstsøgningen. Vi kan oprette dette på enkelte eller flere felter. Følgende sætning vil oprette et tekstindeks på et enkelt felt.
>db.books.createIndex({"description":"text"})
Vi vil oprette et tekstindeks på beskrivelsen og undertekst felter til denne øvelse. Vi kan kun oprette ét tekstindeks pr. samling i MongoDB. Så vi vil oprette et sammensat tekstindeks ved hjælp af følgende sætning.
>db.books.createIndex({"subtitle":"text","description":"text"})
$search
Nu vil vi prøve at søge i dokumenter, der har nøgleordene 'ECMAScript' i beskrivelsen og undertekst felter. Til dette kan vi bruge nedenstående erklæring.
db.books.find({$text: {$search: "ECMAScript"}})
Eksempel
>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."
}
>
Sætninger
Du kan søge efter sætninger ved hjælp af tekstindekset. Som standard udfører tekstsøgning en ELLER-søgning efter alle ord i sætningen. Hvis du vil søge efter 'moderne designmønstre', vil den søge efter dokumenter med nøgleordene enten moderne, design eller mønstre.
Eksempel
>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."
}
>
Hvis du vil søge efter nøjagtige sætninger som dokumenter med 'moderne designmønstre' sammen, kan du gøre det ved at angive dobbelte anførselstegn i søgeteksten.
Eksempel
>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."
}
Negationer
Hvis du vil udelukke dokumenter, der indeholder et bestemt ord, kan du bruge en negationssøgning. Hvis du f.eks. vil søge i alle dokumenter med 'JavaScript' men ikke 'HTML5' eller 'ECMAScript', kan du søge som nedenstående eksempel.
Eksempel
>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."
}
Score for tekstsøgning
Tekstsøgningen giver en score til hvert dokument, der repræsenterer relevansen af dokumentet med søgeforespørgslen. Denne score kan bruges til at sortere alle de poster, der returneres i søgeresultatet. En højere score vil indikere det mest relevante match.
Eksempel
>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
}
Stopord
$text-operatoren bortfiltrerer de sprogspecifikke stopord, såsom a, an, the og på engelsk. Nedenstående søgning vil ikke returnere noget dokument i resultatet.
Eksempel
>db.books.find({$text: {$search: "is"}},{subtitle: 1, description: 1 })
Fetched 0 record(s)
Stammede ord
$text-operatoren matcher det komplette stammeord. Så hvis et dokumentfelt indeholder ordet lære eller lære, ville en søgning på termen lære eller lære resultere i det samme.
Eksempel
>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."
}
Konklusion
Jeg håber, du har lært noget nyt i dag. Her er en interessant artikel om Self-Hosted MongoDB. Jeg inviterer dig også til at prøve ting på egen hånd og dele din oplevelse i kommentarfeltet. Desuden, hvis du står over for problemer med nogen af ovenstående definitioner, er du velkommen til at spørge mig i kommentarfeltet nedenfor.