<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Apurva Shastry on Medium]]></title>
        <description><![CDATA[Stories by Apurva Shastry on Medium]]></description>
        <link>https://medium.com/@apurvashastry?source=rss-c9443e61cb1e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*usTKAHEJRuSSnYq_.</url>
            <title>Stories by Apurva Shastry on Medium</title>
            <link>https://medium.com/@apurvashastry?source=rss-c9443e61cb1e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 20:37:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@apurvashastry/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Efficient Database Search using MongoDB’s Full Text Search (Mongoose)]]></title>
            <link>https://medium.com/@apurvashastry/build-a-cool-database-search-using-these-mongodb-full-text-search-features-on-mongoose-cf2803257f9?source=rss-c9443e61cb1e------2</link>
            <guid isPermaLink="false">https://medium.com/p/cf2803257f9</guid>
            <category><![CDATA[indexing]]></category>
            <category><![CDATA[mongodb]]></category>
            <category><![CDATA[elasticsearch]]></category>
            <category><![CDATA[rankings]]></category>
            <category><![CDATA[search]]></category>
            <dc:creator><![CDATA[Apurva Shastry]]></dc:creator>
            <pubDate>Tue, 02 May 2017 13:44:10 GMT</pubDate>
            <atom:updated>2018-01-11T20:00:58.471Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*92LccMj5wB4u0HcAM8iHrA.png" /></figure><p>We all know how important it is to have a great search feature for any app. As more and more apps are seen everyday, the standard for a reasonable search feature keeps rising. A lot of us end up using elastic search or any of its alternatives. This post is for those who want an easier way for a quick application without using external search engines.</p><p>Some of the things our search feature should cover are:</p><ol><li>search the database — duh</li><li>indexing database for speed — if it fits your application</li><li>ignore stop words like ‘this’, ‘and’ etc.</li><li>reduce words to its stem. e.g. ‘baking’ to ‘bake’</li><li>scoring — that is ranking results based on certain rules</li><li>rank the results according to the feilds they have matched up with</li><li>rank results according to the percentage match with the search phrase</li><li>search similar words, similar sounding words, similarly spelled words too.</li><li>display partial word matches in the end</li><li>Add auto-complete</li></ol><p>That is a lot to do, I will give some pointers on how to go about it in this post.</p><p>Mongo has this awesome feature called ‘full-text search’ which covers most of these features for us. We are using Mongoose to access Mongo.</p><h4><strong>Indexing</strong></h4><p>If you already know the fields which will hold the values which you may want to search, indexing the database leads us to faster search results with the drawback of a tiny overhead whenever we insert new records.</p><pre><em>recipeSchema.index({‘name’: ‘text’, ‘tags’: ‘text’, ‘ingredients.ingredient’: ‘text’});</em></pre><h4><strong>Stop words, Stemming and Scoring</strong></h4><p>Mongo’s full text takes care of these for us. By default, whenever we search a phrase using full-text-search, the results are ranked in decending order of matching scores.</p><pre>db.Recipe</pre><pre>.find( { $text: { $search : searchPhrase } },</pre><pre>  { score : { $meta: “textScore” } } )</pre><pre>.sort( {</pre><pre>  score: { $meta : ‘textScore’ }</pre><pre>} )</pre><h4><strong>Rank results according to the fields with which they have matched up</strong></h4><p>Also called as adding weight to the fields, this can be done at the time of indexing.</p><pre>recipeSchema.index({‘name’: ‘text’, ‘tags’: ‘text’, ‘ingredients.ingredient’: ‘text’}, {weights: {name: 3, tags: 2, ‘ingredients.ingredient’: 1}});</pre><h4><strong>Similar words, Similar sounding words, Similarly spelled words</strong></h4><p>There are a lot of APIs which can help us with this functionality. I prefer <a href="http://www.datamuse.com/api/">datamuse API</a>.</p><p>API paths:</p><p>words with a meaning similar to <em>ringing in the ears: </em><a href="https://api.datamuse.com/"><strong>https://api.datamuse.com</strong></a><a href="https://api.datamuse.com/words?ml=ringing+in+the+ears">/words?ml=ringing+in+the+ears</a></p><p>words that are spelled similarly to <em>coneticut: </em><a href="https://api.datamuse.com/"><strong>https://api.datamuse.com</strong></a><a href="https://api.datamuse.com/words?sp=coneticut">/words?sp=coneticut</a></p><p>words that sound like <em>elefint: </em><a href="https://api.datamuse.com/"><strong>https://api.datamuse.com</strong></a><a href="https://api.datamuse.com/words?sp=coneticut">/</a><a href="https://api.datamuse.com/words?sl=elefint">words?sl=elefint</a></p><p>words that are triggered by (strongly associated with) the word “cow”: <a href="https://api.datamuse.com/"><strong>https://api.datamuse.com</strong></a><a href="https://api.datamuse.com/words?rel_trg=cow">/words?rel_trg=cow</a></p><p>suggestions for the user if they have typed in <em>rawand</em> so far: <a href="https://api.datamuse.com/"><strong>https://api.datamuse.com</strong></a><a href="https://api.datamuse.com/sug?s=rawand">/sug?s=rawand</a></p><h4><strong>Auto-complete</strong></h4><p><a href="https://www.npmjs.com/package/mongoose-in-memory-autocomplete">Mongoose-In-Memory</a> Auto-Complete is a great package which can help you here.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cf2803257f9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>