title: Pagination Elasticsearch in Django
body: <p>I am a newbie in Elasticsearch, I just try to create a search engine using it in Django. Overall, the engine shows good results. Unfortunately, it loads a large number of the results. Then, I try to paginate it by regular pagination in Django, after that, the page load error <code>object of type 'Search' has no len()</code>.</p>
  
  <p>These are my codes:</p>
  
  <p>view.py</p>
  
  <pre><code>from django.shortcuts import render, redirect
  from elasticsearch import Elasticsearch
  from elasticsearch_dsl import Search
  from django.core.paginator import Paginator
  
  def search_es(request):
      return render(request,'search/search.html')
  
  def results(request):
      s = Search(using=Elasticsearch())
      keyword = request.GET.get('q') # keyword that want to be found
      print(keyword)
      if keyword:
          # posts = s.query('match_phrase_prefix',head_title=keyword)
          # if posts.count() == 0:
          posts = s.query(
              "multi_match",
              query=keyword,
              fields=['head_title^5', 'description^5', 'description.ngram'],
              # type="phrase_prefix",
          )
          posts = posts[0: 100]
      else:
          posts = ''
  
      page = request.GET.get('page', 1)
      paginator = Paginator(posts, 10)
      try:
          users = paginator.page(page)
      except PageNotAnInteger:
          users = paginator.page(1)
      except EmptyPage:
          users = paginator.page(paginator.num_pages)
  
      context = {
          'page_title': keyword,
          'posts': users,
          'count': posts.count(),
          'keyword': keyword,
      }
  
      return render(request,'search/results.html',context)
  </code></pre>
  
  <p>results.html</p>
  
  <pre><code>{% if posts.has_other_pages %}
    &lt;ul class="pagination"&gt;
      {% if posts.has_previous %}
        &lt;li&gt;&lt;a href="?page={{ posts.previous_page_number }}"&gt;«&lt;/a&gt;&lt;/li&gt;
      {% else %}
        &lt;li class="disabled"&gt;&lt;span&gt;«&lt;/span&gt;&lt;/li&gt;
      {% endif %}
      {% for i in posts.paginator.page_range %}
        {% if posts.number == i %}
          &lt;li class="active"&gt;&lt;span&gt;{{ i }} &lt;span class="sr-only"&gt;(current)&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
        {% else %}
          &lt;li&gt;&lt;a href="?page={{ i }}"&gt;{{ i }}&lt;/a&gt;&lt;/li&gt;
        {% endif %}
      {% endfor %}
      {% if posts.has_next %}
        &lt;li&gt;&lt;a href="?page={{ posts.next_page_number }}"&gt;»&lt;/a&gt;&lt;/li&gt;
      {% else %}
        &lt;li class="disabled"&gt;&lt;span&gt;»&lt;/span&gt;&lt;/li&gt;
      {% endif %}
    &lt;/ul&gt;
  {% endif %}
  </code></pre>
  
  <p>Hope for every possible solution.
  Thank you very much.</p>