title: Elasticsearch -- count number of keyword occurences in a document
body: <p>Database: Elasticsearch v7.2 <br>
  Application: Laravel v5.7 <br>
  Using Elasticsearch/Elasticsearch (<a href="https://github.com/elastic/elasticsearch-php" rel="nofollow noreferrer">https://github.com/elastic/elasticsearch-php</a>) Official PHP Library</p>
  
  <p>I have a query_string query for Elasticsearch with this code to retrieve documents that have a certain phrase as I search throughout my index</p>
  
  <pre><code>[
      "query_string" =&gt; [
          "default_field" =&gt; $content,
          "query" =&gt; $keywords
      ]
  ],
  </code></pre>
  
  <p>and the <code>$keywords</code> variable contains:</p>
  
  <pre><code>("MCU" OR "Marvel" OR "Spiderman")
  </code></pre>
  
  <p>Now, I want to count the <strong>NUMBER OF OCCURENCES</strong> of these words in the documents that I'm about to retrieve</p>
  
  <p>I used the aggs query with this:</p>
  
  <pre><code>'aggs' =&gt; [
      'count' =&gt; [
          'terms' =&gt; [
              'field' =&gt; 'content.keyword'
          ]
      ]
  ]
  </code></pre>
  
  <p>However, I have no idea how to associate these <code>doc_count</code> and display it in a matched manner with the hits -- because the key itself is the content, instead of the IDs</p>
  
  <p><a href="https://i.stack.imgur.com/OSxGk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OSxGk.png" alt="enter image description here"></a></p>
  
  <p>Im planning to display the whole document and pertain how many times the <code>$keywords</code> above have occurred in each document as <code>Mentions</code></p>
  
  <p><a href="https://i.stack.imgur.com/JoRg4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JoRg4.png" alt="enter image description here"></a>
  Is there other way to do the counting of occurrences without using the <code>aggs</code> in Elasticsearch?</p>