Answers.Body: <p>Something like this should get you started, q is the querystring in your results url:</p>
  
  <pre><code>{% set query = craft.request.getParam('q') %}
  {% set entries = craft.entries.search(query).level(1).order('score') %}
  {% set grouped = entries|group('section') %}
  
          &lt;h1&gt;Results for &lt;em&gt;&amp;ldquo;{{ query }}&amp;rdquo;&lt;/em&gt;&lt;/h1&gt;
  
          {% if entries|length %}
          {% for section, entriesInGroup in grouped %}
          &lt;h3&gt;{{ section }}&lt;/h3&gt;
              &lt;ul&gt;
              {% for entry in entriesInGroup %}
                  &lt;li&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;&lt;/li&gt;
              {% endfor %}
              &lt;/ul&gt;
          {% endfor %}
  {% endif %} 
  </code></pre>
  
  <p>Not sure how pagination fits in here but I'm sure others will chime in.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-25T11:50:22.430Z
Answers.Text: Thanks but I did mention that I don't want to grab all results and then group by section.  Ideally the results won't be already grouped and ordered by section so that there would be not "hack" needed that would affect performance.
Answers.UserDisplayName: biko
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-24T11:08:58.803Z
Answers.LastActivityDate: 2017-03-24T11:08:58.803Z
Answers.UserDisplayName: erwinheiser
Body: <p>Is it possible to perform a search and have the results grouped/prioritized by sections?</p>
  
  <p>e.g. something like:</p>
  
  <pre><code>craft.entries.section(['sec3', 'sec5', 'sec1']).search(...).limit(6) as page_info, page_entries
  </code></pre>
  
  <p>I want <code>sec3</code> entries first, followed by <code>sec5</code> and <code>sec1</code> entries.</p>
  
  <p>Note that I want to use the built-in pagination mechanism and would not like to use hacks such as searching tons of entries and manually group the results by sections.</p>
  
Tags: <entries><search><sections><order><group>
Title: Search entries ordered by sections
ThreadText: # Search entries ordered by sections
  
  (Asked by: username_0 on 2017-03-23)
  
  Is it possible to perform a search and have the results grouped/prioritized by sections?
  
  e.g. something like:
  
  ```
  craft.entries.section(['sec3', 'sec5', 'sec1']).search(...).limit(6) as page_info, page_entries
  
  ```
  
  I want `sec3` entries first, followed by `sec5` and `sec1` entries.
  
  Note that I want to use the built-in pagination mechanism and would not like to use hacks such as searching tons of entries and manually group the results by sections.
  
  **Tags:** entries, search, sections, order, group
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-03-24. Score: 1
  
  Something like this should get you started, q is the querystring in your results url:
  
  ```
  {% set query = craft.request.getParam('q') %}
  {% set entries = craft.entries.search(query).level(1).order('score') %}
  {% set grouped = entries|group('section') %}
  
          <h1>Results for <em>&ldquo;{{ query }}&rdquo;</em></h1>
  
          {% if entries|length %}
          {% for section, entriesInGroup in grouped %}
          <h3>{{ section }}</h3>
              <ul>
              {% for entry in entriesInGroup %}
                  <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
              {% endfor %}
              </ul>
          {% endfor %}
  {% endif %} 
  
  ```
  
  Not sure how pagination fits in here but I'm sure others will chime in.
  
  > **Comments:**
  > **username_0**: Thanks but I did mention that I don't want to grab all results and then group by section.  Ideally the results won't be already grouped and ordered by section so that there would be not "hack" needed that would affect performance.  
  
  ---
  
  