How to implement solr and solarium in cakePHP

1.4k Views Asked by At

I have to integrate Solr search in one of my CakePHP project. I have installed Solr and Solarium using composer but could not find how to start searching using Solr. Is there any example code in CakePHP?

3

There are 3 best solutions below

1
On BEST ANSWER

I have integrated Solr with Option 1. However option 2 is doable but due to some time restriction I have to choose option 1. We can directly include Solarium vendor and include its class in our controller wherever required and use solr's add/get queries.

There are basically 3 major steps: 1: Install Solr. 2: Install Solarium using composer 3: User your scripts within controller or component files to get results.

You can get complete reference and example codebase from here:

http://findnerd.com/list/view/Solr-integration-in-CakePHP-with-solarium-client/1946/

Thanks.

3
On

First thing you need to figure out is how to expose the Solarium API in your CakePHP application. Typically this means saving some third-party PHP files into the Vendor directory of your application (take peek here for more information).

Once you've done this, you have two options:

  1. Interact with the Solarium API directly in your controller's actions.
  2. Implement a Solarium datasource so you can use CakePHP's model constructs.

Option 1

This option is less consistent with how the developers of CakePHP would like you to do MVC and you will have to generate a fair bit of code each time you want to put something in Solr or query it (e.g. connect to the Solr database). If you have minimal interaction with your Solr database, then I would recommend going down this route. Perhaps you could wrap up your access in separate helper class or function so instead of this:

public function void myControllerAction() {
  // create a client instance
  $client = new Solarium\Client($config);

  // get a select query instance
  $query = $client->createQuery($client::QUERY_SELECT);

  // this executes the query and returns the result
  $resultset = $client->execute($query);

  // expose the result set to your view
  $this->set('records', $resultset);
}

you could have this:

public function void myControllerAction() {
  $resultset = solarium_get_records();

  // expose the result set to your view
  $this->set('records', $resultset);
}

Option 2

This option is a bit more involved and requires you to write a Solarium datasource just like the developers have written for MySql and Postgres. This does require you to thoroughly understand the inner workings of CakePHP's model engine but by taking a look at how the other datasources work, it shouldn't be rocket science. Rest assured that if you did this and made your code open-source, other developers will love to use it in their own CakePHP applications!

The benefit of this approach is that you will successfully abstract your application from the specific database implementation. So if you decided you didn't fancy using Solr and preferred a different search engine, you could migrate your data, write a new datasource (if one didn't exist already) and you're all set.

This probably doesn't exactly answer your question but instead steer you in the right direction and highlight some aspects you should consider.

5
On

I've integrated Solr using Sam's Option 2 (as DataSource)

https://github.com/Highstrike/cakephp-solr-datasource

You can also find there instructions on how to use it with examples