%{ nbPages = _caller.count.page(controllers.ElasticSearchController.getPageSize()) }%

<p class="elasticsearchCount">
    &{'elasticsearch.search.size', _caller.count, _caller.type.name}
</p>

#{if nbPages > 1}
    <p class="elasticsearchPages">

        %{
            limit = (_limit != null ? _limit : 10)
            start = _caller.page - (int)(limit/2) + (limit % 2 ? 0 : 1)
            end = _caller.page + (int)(limit/2)

            if(start < 1){
                extra = 1 - start
                end = (end + extra < nbPages ? end + extra : nbPages)
                start = 1
            }else if(end > nbPages){
                extra = end - nbPages
                start = (start - extra > 1 ? start - extra : 1)
                end = nbPages
            }
        }%

        #{if _caller.page > 1}
            <span class="previous">
                #{a @search().add('page', _caller.page-1).add('search', _caller.params.search).add('orderBy', _caller.orderBy).add('order', _caller.order)}&{'elasticsearch.pagination.previous'}#{/a}
            </span>
        #{/if}

        #{if start > 1}
            <span class="first">
                #{a @search().add('page', 1).add('search', _caller.params.search).add('orderBy', _caller.orderBy).add('order', _caller.order)}&{'elasticsearch.pagination.first'}#{/a}
            </span>
        #{/if}

        %{ for (i in start..end) { }%
            <span class="page">
                #{if _caller.page == i}
                    <strong>${i}</strong>
                #{/if}
                #{else}
                    #{a @search().add('page', i).add('search', _caller.params.search).add('orderBy', _caller.orderBy).add('order', _caller.order)}${i}#{/a}
                #{/else}
            </span>
        %{ } }%

        #{if end < nbPages}
            <span class="last">
                #{a @search().add('page', nbPages).add('search', _caller.params.search).add('orderBy', _caller.orderBy).add('order', _caller.order)}&{'elasticsearch.pagination.last'}#{/a}
            </span>
        #{/if}

        #{if _caller.page < nbPages}
            <span class="next">
                #{a @search().add('page', _caller.page+1).add('search', _caller.params.search).add('orderBy', _caller.orderBy).add('order', _caller.order)}&{'elasticsearch.pagination.next'}#{/a}
            </span>
        #{/if}

    </p>
#{/if}
