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:
- Add name of class of model to list of model classes at initialization search engine.
- Declare model with
ISearchable
interface;
$searchEngine = new SearchEngine\Engine('Product', $indexDirectory); // $indexDirectory path to index directory
use SearchEngine\ISearchable;
class Product implements ISearchable
{
// TODO ...
}
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)
);
}
}
$searchEngine->fullUpdateIndex();
$searchEngine->updateIndex($model);
$searchEngine->deleteIndex($model);
/**
* @var ZendSearch\Lucene\Search\QueryHit[] $hits
*/
$queryHits = $searchEngine->search($query);
/**
* @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();
}
/**
* @var SearchEngine\Result\Hit[] $hits
*/
$hits = $searchEngine->parseHits($queryHits);
// get the found ISearchable model from each $hit
foreach ($hits as $hit):
$model = $hit->getItem();
}
Search engine licenced under the MIT license.