1

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?

subu.purohit
  • 33
  • 1
  • 9

3 Answers3

1

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.

Sam Delaney
  • 1,305
  • 11
  • 10
  • Thank you Sam. I will try your both options and will get back to you once I will have some progress. Please keep me updated if you find something worthy. – subu.purohit Dec 18 '14 at 06:43
  • Hi Sam,I have implemented Solr in cakephp and will share my solution here very soon. However due to time problem I am using option 1. – subu.purohit Dec 30 '14 at 11:31
  • I have a query about solr. Can I restrict my search result to list only those items who have all search terms. For example if I search for "A B C" (order might be changed like "C A B") then only those items should be list who have all these three terms "A B C" not who have only "A" or "A B" or only "C" – subu.purohit Dec 30 '14 at 11:34
0

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.

subu.purohit
  • 33
  • 1
  • 9
0

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

Highstrike
  • 462
  • 3
  • 14
  • Thanks Highstrike. It is working well. But I am getting an issue with limit. When trying to get result order by id desc then it is returning me result from id = 999999. – subu.purohit May 27 '15 at 11:31
  • @subu.purohit, I don't understand your problem but maybe this will help you `$articles = $this->paginate('Solr', [], ['score']); // to make order work with score, either add it to whitelist (like i did here) or make a virtual field out of it in your Solr Model` – Highstrike May 28 '15 at 12:58
  • I got the problem resolved by adding a new field. Id field is type of string so can not implement ordering in it. So I had to add a new field with int type and sorting is working fine with this field. – subu.purohit May 28 '15 at 13:50
  • We can also add a new line $this->core = $config['core']; in SolrSource to specify collection, if anybody tries to use different collection. – subu.purohit May 28 '15 at 13:52
  • Glad you figured it out :) As for the solr collection you can change the SolrSource and config files yourself to reflect this and make a pull request on github. I'll gladly merge it :) – Highstrike May 28 '15 at 15:03