title: Elasticsearch and MongoDB n-gram indexing for each mongo document in collection
body: <p>I am an Elasticsearch newbie, and came across recommendations on using ES with MongoDB for text indexing/search capabilities.</p>
  <p>What I am trying to achieve though is analyze the text in my Mongo collection, then build an association between similar content based upon textual context.</p>
  <p>For example:</p>
  <pre><code>Collection A =&gt; 
  {
  id:1, 
  text: 'The sly Fox dug a tunnel under the Fence to snatch a Hen',
  
  }
      {
  id:2, 
  text: 'Jack the farmer built a fence to protect his Hens',
  associated: [id4,id1,...]
  }
      {
  id:3, 
  text: 'Jack listened to his radio and called it a night',
  }
  {
  id:4, 
  text: 'The farmer Jack plowed the field'
  }
  </code></pre>
  <p>The system should then associate these documents in the following order:</p>
  <ul>
  <li>id2 &lt;==&gt; id4 # &quot;The farmer Jack&quot; - n-gram</li>
  <li>id2 &lt;==&gt; id1 # Fence &amp; Hens</li>
  <li>... and so on.</li>
  </ul>
  <p>One inefficient way of solving this, is once I have created an index of Collection A in Elasticsearch, I can then write a batch process to cycle through each n-gram and search for it in ES, then push the results back into collection A (See association).</p>
  <p>I presume there must be an easier way of doing this, and that I might be missing something. Is there ? If so, what is it or where can I find good references about it ?</p>