{"id":6581,"date":"2023-08-10T12:23:58","date_gmt":"2023-08-10T19:23:58","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cosmosdb\/?p=6581"},"modified":"2023-08-14T04:46:17","modified_gmt":"2023-08-14T11:46:17","slug":"introducing-text-indexes-in-azure-cosmos-db-for-mongodb-vcore","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cosmosdb\/introducing-text-indexes-in-azure-cosmos-db-for-mongodb-vcore\/","title":{"rendered":"Introducing Text Indexes in Azure Cosmos DB for MongoDB vCore"},"content":{"rendered":"<p>In the realm of modern databases, efficient querying of text-based data is pivotal to deliver smooth user experiences and provide valuable insights from textual content. To address this need, <a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/cosmos-db\/mongodb\/vcore\/introduction\">Azure Cosmos DB for MongoDB vCore<\/a> now offers text indexing. In this blog, we&#8217;ll delve into the ins and outs of text indexes in Azure Cosmos DB for MongoDB vCore, exploring their significance, implementation, and capabilities.<\/p>\n<h2><span style=\"font-size: 18pt;\"><strong>What are Text Indexes<\/strong><\/span><\/h2>\n<p><a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/cosmos-db\/mongodb\/vcore\/how-to-text-indexes\">Text indexes<\/a> are a specialized data structure within Azure Cosmos DB for MongoDB vCore that vastly improve the performance of text-based queries. These indexes optimize the querying process for textual data such as articles, documents, comments, and other content-rich sources. Employing techniques like tokenization, stemming, and stop words, text indexes build an index that accelerates text-based searches, making them faster and more efficient.<\/p>\n<h2><span style=\"font-size: 18pt;\"><strong>Defining a Text Index<\/strong><\/span><\/h2>\n<p>Let&#8217;s envision a scenario where we are developing a job search platform. The job listings are structured as JSON documents, each containing fields such as job_title, company, location, description, skills_required, employment_type, and posted_date. Let&#8217;s walk through the process of creating a text index for this job search platform.<\/p>\n<p>Consider the following example job listing:<\/p>\n<pre class=\"prettyprint language-json\"><code class=\"language-json\">{\r\n\"_id\": ObjectId(\"628b45f8e91234cdef567890\"),\r\n\"job_title\": \"Senior Software Engineer\",\r\n\"company\": \"Big Tech Innovations Inc.\",\r\n\"location\": \"San Francisco, CA\",\r\n\"description\": \"We're seeking a skilled Senior Software Engineer...\",\r\n\"skills_required\": [\"Java\", \"JavaScript\", \"React\", \"Node.js\"],\r\n\"employment_type\": \"Full-time\",\r\n\"posted_date\": \"2023-08-08T00:00:00Z\"\r\n}<\/code><\/pre>\n<p>To create a text index for the fields: &#8220;job_title,&#8221; &#8220;company,&#8221; &#8220;location,&#8221; and &#8220;description,&#8221; you can use the following syntax:<\/p>\n<pre class=\"prettyprint language-json\"><code class=\"language-json\">db.job_listings.createIndex({job_title: \"text\", company: \"text\", location: \"text\", description: \"text\"});<\/code><\/pre>\n<p>With this text index, text searches will search across all of these job-related fields. Only one text index is allowed per collection.<\/p>\n<h2><span style=\"font-size: 18pt;\"><strong>Text Index: Options and Weights<\/strong><\/span><\/h2>\n<p>Text indexes in <a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/cosmos-db\/mongodb\/vcore\/introduction\">Azure Cosmos DB for MongoDB vCore<\/a> come with a range of customizable options that allow you to fine-tune their behavior. You can define the default language for text analysis, assign weights to fields to prioritize certain attributes, and configure case-insensitive searches, similar to the previous example.<\/p>\n<p>In our job search platform scenario, you could create a text index with options like this:<\/p>\n<pre class=\"prettyprint language-json\"><code class=\"language-json\">db.job_listings.createIndex(\r\n{\r\njob_title: \"text\",\r\ncompany: \"text\",\r\nlocation: \"text\",\r\ndescription: \"text\"\r\n},\r\n{\r\ndefault_language: \"english\",\r\nweights: { job_title: 10, description: 5 },\r\ncaseSensitive: false\r\n});<\/code><\/pre>\n<p>This example demonstrates a text index spanning multiple fields, with weighted importance assigned to the job title and description fields. By doing so, you enhance the relevance of search results, ensuring that job titles hold higher significance in search scoring.<\/p>\n<p>To look at the score of documents in the query result, you can use the $meta projection operator along with the textScore field in your query projection.<\/p>\n<pre class=\"prettyprint language-json\"><code class=\"language-json\">db.job_listings.find(\r\n{ $text: { $search: \"Senior Software Engineer\" } },\r\n{ score: { $meta: \"textScore\" } }\r\n)<\/code><\/pre>\n<h2><span style=\"font-size: 18pt;\"><strong>Executing Text Searches<\/strong><\/span><\/h2>\n<p>The text operator, integrated into queries, matches a search string against all fields in the text index, promptly retrieving pertinent job listings in our example.<\/p>\n<p>Consider this query as an example:<\/p>\n<pre class=\"prettyprint language-json\"><code class=\"language-json\">db.job_listings.find({ $text: { $search: \"Senior Software Engineer\" } });<\/code><\/pre>\n<p>This query will return documents from the &#8220;job_listings&#8221; collection containing the terms &#8220;Senior,&#8221; &#8220;Software,&#8221; and &#8220;Engineer&#8221; in any order.<\/p>\n<h2><span style=\"font-size: 18pt;\"><strong>Conclusion<\/strong><\/span><\/h2>\n<p>Text indexes in Azure Cosmos DB for MongoDB vCore optimize text-based searches. Whether you&#8217;re developing a job search platform, a content-rich application, or any use case involving textual data, the power of text indexes is at your disposal within Azure Cosmos DB for MongoDB vCore.<\/p>\n<h2><strong><span style=\"font-size: 18pt;\">About Azure Cosmos DB<\/span><\/strong><\/h2>\n<p><a href=\"https:\/\/azure.microsoft.com\/en-us\/products\/cosmos-db\/\">Azure Cosmos DB<\/a> is a fully managed NoSQL and relational database for modern app development with SLA-backed speed and availability, automatic and instant scalability, and support for open-source PostgreSQL, MongoDB, and Apache Cassandra. To stay in the loop on Azure Cosmos DB updates, follow us on <a href=\"https:\/\/twitter.com\/AzureCosmosDB\">Twitter<\/a>, <a href=\"https:\/\/www.youtube.com\/AzureCosmosDB\">YouTube<\/a>, and <a href=\"https:\/\/www.linkedin.com\/company\/azure-cosmos-db\/\">LinkedIn<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the realm of modern databases, efficient querying of text-based data is pivotal to deliver smooth user experiences and provide valuable insights from textual content. To address this need, Azure Cosmos DB for MongoDB vCore now offers text indexing. In this blog, we&#8217;ll delve into the ins and outs of text indexes in Azure Cosmos [&hellip;]<\/p>\n","protected":false},"author":125125,"featured_media":6606,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[15,14],"tags":[499,287,1246,1872,1881,1880],"class_list":["post-6581","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mongodb-api","category-core-sql-api","tag-azure-cosmos-db","tag-cosmos-db","tag-mongodb","tag-nosql","tag-text-indexes","tag-text-search"],"acf":[],"blog_post_summary":"<p>In the realm of modern databases, efficient querying of text-based data is pivotal to deliver smooth user experiences and provide valuable insights from textual content. To address this need, Azure Cosmos DB for MongoDB vCore now offers text indexing. In this blog, we&#8217;ll delve into the ins and outs of text indexes in Azure Cosmos [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/6581","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/users\/125125"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/comments?post=6581"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/6581\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media\/6606"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media?parent=6581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/categories?post=6581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/tags?post=6581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}