# Please help me change the php code from pagination from + size to search\_after

**URL:** <https://discuss.elastic.co/t/please-help-me-change-the-php-code-from-pagination-from-size-to-search-after/360323>\
**Category:** Elasticsearch\
**Created:** [May 28, 2024, 1:14am UTC](https://discuss.elastic.co/t/please-help-me-change-the-php-code-from-pagination-from-size-to-search-after/360323 "2024-05-28T01:14:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![chongshengdz](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@chongshengdz](https://discuss.elastic.co/u/chongshengdz)\
**Post date:** [May 28, 2024, 1:14am UTC](https://discuss.elastic.co/t/please-help-me-change-the-php-code-from-pagination-from-size-to-search-after/360323/1 "2024-05-28T01:14:17Z")

</div>

please help me change the php code from pagination from + size to search\_after, product\_id is the unique id for sorting.

```auto
$start = ($params['page'] - 1) * $params['limit'];
$body = [
'_source' => ['product_id','name','categories','category_0_name','category_1_name','model','manufacturer'],
'query' => $query,
'from' => $start,
'size' => $params['limit'],
'sort' => ['product_id' => ['order' => asc]]
];

$search = [
    'index' => $index,
    //'type' => $type,
    'type' => '_doc',
    'body' => $body
];

$return = [
    'hits' => [],
    'total' => 0,
    'took' => null,
    'filters' => [],
];

try{
    $results = $this->client->search($search);
}catch(\Exception $e){
    $this->registry->get('log')->write('FATAL: Search failed. Error: '.$e->getMessage());
    return $return;
}

$return['took'] = $results['took'];
$return['timed_out'] = $results['timed_out'];

$language = $this->session->data['language'];

if (isset($results['hits']) && count($results['hits'])):
    $return['total'] = $results['hits']['total'];
    $return['hits'] = [];
    $position = 1;
    foreach ($results['hits']['hits'] as $hit):
        $return['hits'][] = $hit['_id'];
        $return['products'][$hit['_id']] = [
            'id' => (int) $hit['_id'],
            'product_id' => $hit['_source']['product_id'],
            'name' => $hit['_source']['name'][$language],
            'category_0_name' => isset($hit['_source']['category_0_name'][$language]) ? $hit['_source']['category_0_name'][$language] : '',
            'category_1_name' => isset($hit['_source']['category_1_name'][$language]) ? $hit['_source']['category_1_name'][$language] : '',
            'position' => $position,
            'model' => $hit['_source']['model'],
            'position_overall' => $position + $start,
            'manufacturer' => isset($hit['_source']['manufacturer']) ? $hit['_source']['manufacturer']['name'] : '_none_',
        ];
        $position++;
    endforeach;
endif;

```

---

<div class="post-metadata">

**Author:** ![chongshengdz](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@chongshengdz](https://discuss.elastic.co/u/chongshengdz)\
**Post date:** [May 28, 2024, 11:10am UTC](https://discuss.elastic.co/t/please-help-me-change-the-php-code-from-pagination-from-size-to-search-after/360323/2 "2024-05-28T11:10:47Z")

</div>

someone in github suggested to post the question here and said this is an active forum. but the question has been posted for hours without any reply. maybe it is only a dead forum.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [May 28, 2024, 9:30pm UTC](https://discuss.elastic.co/t/please-help-me-change-the-php-code-from-pagination-from-size-to-search-after/360323/3 "2024-05-28T21:30:22Z")

</div>

Please be patient in waiting for responses to your question and refrain from pinging multiple times asking for a response or opening multiple topics for the same question. This is a community forum, it may take time for someone to reply to your question. For more information please refer to the [Community Code of Conduct](https://www.elastic.co/community/codeofconduct) specifically the section "Be patient". Also, please refrain from pinging folks directly, this is a forum and anyone that participates might be able to assist you.

If you are in need of a service with an SLA that covers response times for questions then you may want to consider talking to us about a [subscription](https://www.elastic.co/subscriptions).

It's fine to answer on your own thread after 2 or 3 days (not including weekends) if you don't have an answer.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [May 28, 2024, 9:37pm UTC](https://discuss.elastic.co/t/please-help-me-change-the-php-code-from-pagination-from-size-to-search-after/360323/4 "2024-05-28T21:37:19Z")

</div>

I don't really know PHP but I guess you need to mimic what the REST API is expecting. [Paginate search results | Elasticsearch Guide [8.13] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/paginate-search-results.html#search-after)

Could you try and may be share the code you wrote and the problem you are facing?

Please [read this](https://discuss.elastic.co/t/about-the-elasticsearch-category/21) about how to format.

---

<div class="post-metadata">

**Author:** ![chongshengdz](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@chongshengdz](https://discuss.elastic.co/u/chongshengdz)\
**Post date:** [May 28, 2024, 10:05pm UTC](https://discuss.elastic.co/t/please-help-me-change-the-php-code-from-pagination-from-size-to-search-after/360323/5 "2024-05-28T22:05:02Z")

</div>

that website is useless, it only teaches people how to search but nothing about writing php coding.

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [May 29, 2024, 5:59am UTC](https://discuss.elastic.co/t/please-help-me-change-the-php-code-from-pagination-from-size-to-search-after/360323/6 "2024-05-29T05:59:18Z")

</div>

Similar to what is shown in the search example within the documentation provided by @dadoonet, you need to include the `search_after` parameter in the body segment of your request instead of size. There is a note about validation on the PHP client in [this issue](https://github.com/elastic/elasticsearch-php/issues/556) here that, while old, is still relevant.

If that doesn't work please share the code and the behaviour/ error that you see.

---

<div class="post-metadata">

**Author:** ![chongshengdz](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@chongshengdz](https://discuss.elastic.co/u/chongshengdz)\
**Post date:** [May 29, 2024, 6:40am UTC](https://discuss.elastic.co/t/please-help-me-change-the-php-code-from-pagination-from-size-to-search-after/360323/7 "2024-05-29T06:40:46Z")

</div>

I am nothing about PHP, below is the code, can you @carly.richmond help me change it?

```
public function findProducts($params) {

    $searchConfig = $this->getSearchConfigs();
    $searchFields = $this->getFieldsWeights($searchConfig['fields']);

    $index = $this->getPrimaryIndexName();
    $type = $this->indexType();

    $params['filter_include'] = isset($params['filter_include']) ? $params['filter_include'] : [];

    $this->load->library('search_plus');
    $this->client = $this->search_plus->client;

    $query = ['bool' => ['must' => []]];

    /**
     * @todo Escape query string
     */
    if ($params['search'] != ''):

        $queryString['fields'] = $searchFields;
        $queryString['analyzer'] = 'custom_analyzer';

        $words = explode(' ',$this->search_plus->escape($params['search']));
        if($searchConfig['fuzziness'] > 0):
            /**
             * Append fuzzy operator ~ to words
             */
            array_walk($words, function (&$value, $key){
                $value.='~';
            });
            $queryString['fuzziness'] = $searchConfig['fuzziness'];
        endif;

        $queryWords = implode(' ', $words);
        
        if($params['auto_complete'] > 0):

            $words = $this->search_plus->escape($params['search']).'';
            $queryString['query'] = '('.$queryWords.')^10 OR ('.$words.'*)';
        else:
            $queryString['query'] = $queryWords;
        endif;
        
        $query['bool']['must']['query_string'] = $queryString;
        $query['bool']['should']['query_string'] = $queryString;
        $query['bool']['must']['query_string']['default_operator'] = 'OR';
        $query['bool']['should']['query_string']['default_operator'] = 'AND';
        $query['bool']['should']['query_string']['boost'] = 100;

    endif;

    $filters = [];
    // status
    $filters[] = ['term' => ['status' => 1]];

    if ($params['category_id'] > 0):
        $filters[] = ['term' => ['categories' => $params['category_id']]];
    endif;

    if ($params['manufacturer_id'] > 0):
        $filters[] = ['term' => ['manufacturer.id' => $params['manufacturer_id']]];
    endif;

    if ($params['location'] != ''):
        $filters[] = ['term' => ['location.keyword' => $params['location']]];
    endif;

    $priceFilter = [];
    if ($params['price_max'] > 0):
        $priceFilter['lte'] = $params['price_max'];
    endif;

    if ($params['price_min'] > 0):
        $priceFilter['gte'] = $params['price_min'];
    endif;

    if(count($priceFilter)):
        $filters[]['range']['price'] = $priceFilter;
    endif;

    if ($params['discount'] > 0):
        $filters[]['range']['discount_percent'] = ['gte' => $params['discount']];
    endif;

    $query['bool']['filter'] = $filters;
    $start = ($params['page'] - 1) * $params['limit'];
    $body = [
        '_source' => ['name','categories','category_0_name','category_1_name','model','manufacturer'],
        'query' => $query,
        'from' => $start,
        'size' => $params['limit'],
        'sort' => ['_score' => ['order' => $params['order']]]
    ];

    if($params['return_filters']):

        $body['aggregations'] = $this->getAggregationsCols($params['filter_include']);

    endif;

    $search = [
        'index' => $index,
        //'type' => $type,
        //'type' => '_doc',
        'body' => $body
    ];

    $return = [
        'hits' => [],
        'total' => 0,
        'took' => null,
        'filters' => [],
    ];

    try{
        $results = $this->client->search($search);
    }catch(\Exception $e){
        $this->registry->get('log')->write('FATAL: Search failed. Error: '.$e->getMessage());
        return $return;
    }

    $return['took'] = $results['took'];
    $return['timed_out'] = $results['timed_out'];

    $language = $this->session->data['language'];

    if (isset($results['hits']) && count($results['hits'])):
        $return['total'] = $results['hits']['total'];
        $return['hits'] = [];
        $position = 1;
        foreach ($results['hits']['hits'] as $hit):
            $return['hits'][] = $hit['_id'];
            $return['products'][$hit['_id']] = [
                'id' => (int) $hit['_id'],
                'name' => $hit['_source']['name'][$language],
                'category_0_name' => isset($hit['_source']['category_0_name'][$language]) ? $hit['_source']['category_0_name'][$language] : '',
                'category_1_name' => isset($hit['_source']['category_1_name'][$language]) ? $hit['_source']['category_1_name'][$language] : '',
                'position' => $position,
                'model' => $hit['_source']['model'],
                'position_overall' => $position + $start,
                'manufacturer' => isset($hit['_source']['manufacturer']) ? $hit['_source']['manufacturer']['name'] : '_none_',
            ];
            $position++;
        endforeach;
    endif;

    $return['products'] = isset($return['products']) ? array_values($return['products']) : [];

    if(isset($results['aggregations'])):

        $return['filters'] = $this->processFacets($results['aggregations']);
    endif;
    return $return;
}

```

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [May 29, 2024, 3:06pm UTC](https://discuss.elastic.co/t/please-help-me-change-the-php-code-from-pagination-from-size-to-search-after/360323/9 "2024-05-29T15:06:49Z")

</div>

Hi @chongshengdz,

Thanks for your response. Just a couple of points:

1. We're not paid consulting here on this forum. It's best effort. We can provide examples if possible, but "please fix this code" is not how this forum works. If you have tried specific things already that would be important information to share. Also if you are stuck on something specific, or are seeing a particular error, let us know what you have tried already and what the error is and we'll do our best to help.
2. Please ensure you strike the right tone in your messages. Adhere to the [Community Code of Conduct](https://www.elastic.co/community/codeofconduct), specifically _be patient_ and _be nice_. If you continue to post inappropriate messages you will be banned.

---

<div class="post-metadata">

**Author:** ![chongshengdz](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@chongshengdz](https://discuss.elastic.co/u/chongshengdz)\
**Post date:** [May 29, 2024, 3:26pm UTC](https://discuss.elastic.co/t/please-help-me-change-the-php-code-from-pagination-from-size-to-search-after/360323/10 "2024-05-29T15:26:41Z")

</div>

ectransistors.\com/index.php?route=product/search&language=en-gb&search=Digital-to-Analog  
@carly.richmond it looks like it works now, but it shows all the results on one page and each scroll page is the same (showing all results).  
can you check it and see what is wrong?  
'size' =\> 10, but it shows all results
