nqxcode/search-engine

Search engine for ORM based on Zend Lucene.

v1.0.6 2014-07-27 00:06 UTC

README

Latest Stable Version Total Downloads Latest Unstable Version License

Search engine gives opportunity of indexing of models (ActiveRecord) on demanded fields. Search engine uses the morphological filter for english and russian words. Morphological filter based on "phpMorphy" (morphological analyzer library).

In order that the model was available to indexation, it is necessary:

  1. Add name of class of model to list of model classes at initialization search engine.
  2. Declare model with ISearchable interface;

Usage

Initialization on search engine

$searchEngine = new SearchEngine\Engine('Product', $indexDirectory); // $indexDirectory path to index directory

Declare model with ISearchable interface

use SearchEngine\ISearchable;

class Product implements ISearchable
{
    // TODO ...
}
Example of realization getAttributesForIndexing method of ISearchable interface
use SearchEngine\ISearchable;

class Product implements ISearchable
{
    // ...

    public funtion getAttributesForIndexing()
    {
        // list of couples "field name - field value"
        return array(
            new Attribute('fieldName', $this->fieldName),
            new Attribute('otherFieldName', $this->otherFieldName)
        );
    }
}

Operation on index

Full update for search index
$searchEngine->fullUpdateIndex();
Update index for ISearchable model
$searchEngine->updateIndex($model);
Delete index for ISearchable model
$searchEngine->deleteIndex($model);

Execute search query

/**
 * @var ZendSearch\Lucene\Search\QueryHit[] $hits
 */
$queryHits = $searchEngine->search($query);
Get result for paginator
/**
 * @var SearchEngine\Result\Hit[] $hits
 */
$hits = $searchEngine->parseHitsByRange($queryHits, $elementsPerPage, $currentPage);

// get the found ISearchable model from each $hit
foreach ($hits as $hit):
    $model = $hit->getItem();
}
Get full result
/**
 * @var SearchEngine\Result\Hit[] $hits
 */
$hits = $searchEngine->parseHits($queryHits);

// get the found ISearchable model from each $hit
foreach ($hits as $hit):
    $model = $hit->getItem();
}

License

Search engine licenced under the MIT license.