YiiSolr 50

A wrapper for the pecl solr library that provides common Yii constructs, such as models, data providers etc

phpnode/YiiSolr

Written in PHP by 7 contributors

Contribute

Need Support?


we offer maintenance, support and development services for all our open source projects.

Contact Us

Introduction

A Yii package that provides a wrapper for the pecl solr library (http://www.php.net/manual/en/book.solr.php) to make fast searching with Yii really easy. The wrapper allows the use of familiar Yii constructs such as models, data providers etc with a solr index.

Installation

First install the pecl solr extension

pecl install solr
If this command fails, you may find it easier to compile the pecl solr extension from source. If you do not already have a packages directory and alias set up, first create a directory called "packages" in your application folder. Then add an alias to your main config, e.g.
"aliases" => array(
    "packages" => dirname(__DIR__)."/packages/",
),
...

Now extract the files to packages/solr

Running unit tests

The unit tests depend on sqlite to provide the test data, please ensure the php PDO sqlite module is installed before continuing.

The unit tests also depend on the example index that ships with solr. Go to your solr installation directory and in the "example" folder run

java -jar start.jar
This should start the solr server running on port 8983 by default. If you're using a different port, please configure it in the packages/solr/tests/common.php file. Now go to your application tests directory, usually protected/tests and run the following command:
phpunit --verbose ../packages/solr/tests

This will run the unit tests, if all went well they should all pass, otherwise please check your configuration.

Configuring your solr connection

Before we can use solr in our application, we must configure a connection to use. In the application config, add the following

"components" => array(
    ...
    "solr" => array(
         "class" => "packages.solr.ASolrConnection",
         "clientOptions" => array(
             "hostname" => "localhost",
             "port" => 8983,
         ),
     ),
),

This will configure an application component called "solr". If you're dealing with more than one index, define a new solr connection for each one, giving each a unique name.

Indexing a document with solr

To add a document to solr we use the {@link ASolrDocument} class. Example:

$doc = new ASolrDocument;
$doc->id = 123;
$doc->name = "test document";
$doc->save(); // adds the document to solr
Remember - Your changes won't appear in solr until a commit occurs. If you need your data to appear immediately, use the following syntax:
$doc->getSolrConnection()->commit();
If you need to deal with multiple solr indexes, it's often best to define a model for each index you're dealing with. To do this we extend ASolrDocument in the same way that we would extend CActiveRecord when defining a model For example:
class Job extends ASolrDocument {
    /**
     * Required for all ASolrDocument sub classes
     * @see ASolrDocument::model()
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }
    /**
     * @return ASolrConnection the solr connection to use for this model
     */
    public function getSolrConnection() {
        return Yii::app()->yourCustomSolrConnection;
    }
}

Searching solr

To find documents in solr, we use the following methods:

  • {@link ASolrDocument::find()}
  • {@link ASolrDocument::findAll()}
  • {@link ASolrDocument::findByAttributes()}
  • {@link ASolrDocument::findAllByAttributes()}
  • {@link ASolrDocument::findByPk()}
  • {@link ASolrDocument::findAllByPk()}

The most useful of these methods are find() and findAll(). Both these methods take a criteria parameter, this criteria parameter should be an instance of {@link ASolrCriteria}. Example: Find all documents with the name "test"

$criteria = new ASolrCriteria;
$criteria->query = "name:test"; // lucene query syntax
$docs = ASolrDocument::model()->findAll($criteria);
Alternative method:
$docs = ASolrDocument::model()->findAllByAttributes(array("name" => "test"));

Example: Find a job with the unique id of 123

$job = Job::model()->findByPk(123);
Example: Find the total number of jobs in the index
$criteria = new ASolrCriteria;
$criteria->query = "*"; // match everything
$total = Job::model()->count($criteria); // the total number of jobs in the index

Using data providers

Often we need to use a data provider to retrieve paginated lists of results. Example:

$dataProvider = new ASolrDataProvider(Job::model());
$dataProvider->criteria->query = "*";
foreach($dataProvider->getData() as $job) {
    echo $job->title."\n";
}

Removing items from the index

To remove an item from the index, use the following syntax:

$job = Job::model()->findByPk(234);
$job->delete();




43 other PHP projects


phpwkhtmltopdfPHP 716

A slim PHP wrapper around wkhtmltopdf with an easy to use and clean OOP interface

php-pdftkPHP 213

A PDF conversion and form utility based on pdftk

yii2-localeurlsPHP 189

Automatic locale/language management for URLs

YiiRedisPHP 131

A set of wrappers for different data types in redis

yiipasswordPHP 81

Password strategies for Yii

yii2-dockerizedPHP 64

A template for docker based Yii 2 applications

php-shellcommandPHP 48

A simple object oriented interface to execute shell commands in PHP

Yii-Docs-GeneratorPHP 47

Generates HTML documentation for Yii applications

YiiGitPHP 36

A git wrapper for Yii, allows access to all git commands programatically

YiiElasticSearchPHP 32

Elastic Search client for Yii

YiiCurlPHP 30

A curl library for Yii

YiiStateMachinePHP 28

A state machine behavior for Yii

yii2-streamlogPHP 19

A Yii 2 log target for streams in URL format

oauth2yiiPHP 18

An OAuth2 client / server extension for the Yii framework

packagecompressorPHP 16

A Javascript/CSS compressor based on Yii's package system

restyiiPHP 16

A RESTful extension for Yii.

localeurlsPHP 12

Automatic locale/language management for URLs

php-excel-readerPHP 12

It reads the binary format of XLS files directly and can return values and formats from any cell. This project is the fork of http://code.google.com/p/php-excel-reader/ that, apparently, is no longer maintained.

pdfablePHP 10

A Yii extension to create PDFs with PHPWkHtmlToPdf/wkhtmltopdf

yii-api-vimPHP 8

Yii API manual plugin for VIM

yii2-excel-messagePHP 7

Translate messages via Excel files

yii2-bs3activeformPHP 7

A Bootstrap 3 enhanced ActiveForm for Yii 2

xcrudcontrollerPHP 7

A base class to quickly build customized CRUD interfaces.

handlebarsphpPHP 6

Transpiles handlebars templates into native PHP templates

Yii-Package-ManagerPHP 6

A package manager for Yii

php-tmpfilePHP 6

A convenience class for temporary files

yii2-apidoc-vimPHP 5

Yii2 apidoc as Vim helpfiles

php-orientdbPHP 5

A fast PHP driver for the OrientDB binary protocol.

translatablePHP 4

Transparent attribute translation for ActiveRecords

YiiUsersPHP 4

User management for Yii

Yii-Resource-ManagerPHP 3

Helpers to allow Yii to interact with resources (files) of different types

flushablePHP 3

Yii dependency that allows to flush records from the cache

bs3activeformPHP 2

A lightweight utility to render Bootstrap 3 forms in Yii

YiiEmailerPHP 2

Emailing functions for Yii

AccessRestrictablePHP 2

A Yii ActiveRecordBehavior that automatically applies conditions for access restriction to every query.

YiiLinkablePHP 1

A simple extension for Yii allowing easy and consistent access to model URLs and appropriate anchor text.

defaultpersisterPHP 1

Yii extension to save and restore model values in user session.

hybridauthmanagerPHP 1

An AuthManager for Yii that stores the hierarchy in a flat PHP file and the assignments in DB

YiiYaaPHP 1

YAA is an additional abstraction layer for Yii that aggregates a number of child models into a clean single model that is easy to cache

YiiAbstractArrayModelPHP

Work with PHP files in Active Record way

twittonioPHP

simple useless twitter client

yii2-base-appPHP

An alternative Yii2 application template for purists

xreturnablePHP

Yii extension to create URLs that allow to return to a page by storing its GET Parameters on a stack.