Category: Models & ORM

Q: Using cakephp/elastic-search with pagination

How do you use the elastic-search plugin with cake's native pagination?

before filter:

$this->loadModel('Products', 'Elastic');

in controller:

$this->paginate = [
  'limit' => 10,
  'conditions' => array('or' => [
    'title in' => explode(" ", $query)
  ]),
];

$products = $this->paginate('Products');
$this->set('products', $products');

After setup, I found the routing params in view was not right?

-> paging (array)
  -> Products (array) 
     -> finder all  
     -> page 1 //these are not right
     -> current 1
     -> count 1
     -> perPage 1
     -> prevPage (false)
     -> nextPage (false)
     -> pageCount 1 //should have more than 1 page

Need someone's advice, thanks!

SOLVED elasticsearch plugin pagination
1 Answers

Version: 3

Accepted by M. Russell

I do not know much about the elastic search data type, but I think you can try this:

$products = $this->Product->find('all', array(
  'fields' => array('id'),
  'conditions' => array('title in' => explode(" ", $query))
)->toArray();

$ids = array();
foreach ($products as $product) {
  $ids[] = $product['id'];
}

$this->paginate = [
  'limit' => 10,
  'conditions' => array(
    'id IN' => array_unique($ids) //to eliminate duplicate ids
  ),
];

$products = $this->paginate('Products');
$this->set('products', $products');

Just a thought!