title: MongoDB Full and Partial Text Search
body: <p><strong>Env:</strong></p>
  <ul>
  <li>MongoDB (3.2.0) with Mongoose</li>
  </ul>
  <hr />
  <p><strong>Collection:</strong></p>
  <ul>
  <li>users</li>
  </ul>
  <hr />
  <p><strong>Text Index creation:</strong></p>
  <pre><code>  BasicDBObject keys = new BasicDBObject();
    keys.put(&quot;name&quot;,&quot;text&quot;);
  
    BasicDBObject options = new BasicDBObject();
    options.put(&quot;name&quot;, &quot;userTextSearch&quot;);
    options.put(&quot;unique&quot;, Boolean.FALSE);
    options.put(&quot;background&quot;, Boolean.TRUE);
    
    userCollection.createIndex(keys, options); // using MongoTemplate
  </code></pre>
  <hr />
  <p><strong>Document:</strong></p>
  <ul>
  <li>{&quot;name&quot;:&quot;LEONEL&quot;}</li>
  </ul>
  <hr />
  <p><strong>Queries:</strong></p>
  <ul>
  <li><code>db.users.find( { &quot;$text&quot; : { &quot;$search&quot; : &quot;LEONEL&quot; } } )</code> =&gt; FOUND</li>
  <li><code>db.users.find( { &quot;$text&quot; : { &quot;$search&quot; : &quot;leonel&quot; } } )</code> =&gt; FOUND (search caseSensitive is false)</li>
  <li><code>db.users.find( { &quot;$text&quot; : { &quot;$search&quot; : &quot;LEONÉL&quot; } } )</code> =&gt; FOUND (search with diacriticSensitive is false)</li>
  <li><code>db.users.find( { &quot;$text&quot; : { &quot;$search&quot; : &quot;LEONE&quot; } } )</code> =&gt; FOUND (Partial search)</li>
  <li><code>db.users.find( { &quot;$text&quot; : { &quot;$search&quot; : &quot;LEO&quot; } } )</code> =&gt; NOT FOUND (Partial search)</li>
  <li><code>db.users.find( { &quot;$text&quot; : { &quot;$search&quot; : &quot;L&quot; } } )</code> =&gt; NOT FOUND (Partial search)</li>
  </ul>
  <p>Any idea why I get 0 results using as query &quot;LEO&quot; or &quot;L&quot;?</p>
  <p>Regex with Text Index Search is not allowed.</p>
  <pre><code>db.getCollection('users')
       .find( { &quot;$text&quot; : { &quot;$search&quot; : &quot;/LEO/i&quot;, 
                            &quot;$caseSensitive&quot;: false, 
                            &quot;$diacriticSensitive&quot;: false }} )
       .count() // 0 results
  
  db.getCollection('users')
       .find( { &quot;$text&quot; : { &quot;$search&quot; : &quot;LEO&quot;, 
                            &quot;$caseSensitive&quot;: false, 
                            &quot;$diacriticSensitive&quot;: false }} )
  .count() // 0 results
  </code></pre>
  <hr />
  <p><strong>MongoDB Documentation:</strong></p>
  <ul>
  <li><a href="https://docs.mongodb.com/v3.2/text-search/" rel="noreferrer">Text Search</a></li>
  <li><a href="https://docs.mongodb.com/manual/reference/operator/query/text/" rel="noreferrer">$text</a></li>
  <li><a href="https://docs.mongodb.com/manual/core/index-text/" rel="noreferrer">Text Indexes</a></li>
  <li><a href="https://jira.mongodb.org/browse/SERVER-15090" rel="noreferrer">Improve Text Indexes to support partial word match</a></li>
  </ul>