nqxcode/search-engine

基于 Zend Lucene 的 ORM 搜索引擎。

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

This package is auto-updated.

Last update: 2024-09-18 19:14:48 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

搜索引擎提供对模型(ActiveRecord)指定字段进行索引的机会。搜索引擎使用英语和俄语的形态学过滤器。形态学过滤器基于 "phpMorphy"(形态分析库)。

为了让模型可以被索引,需要

  1. 在搜索引擎初始化时,将模型类名添加到模型类列表中。
  2. 使用 ISearchable 接口声明模型;

使用

在搜索引擎中初始化

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

使用 ISearchable 接口声明模型

use SearchEngine\ISearchable;

class Product implements ISearchable
{
    // TODO ...
}

ISearchable 接口的 getAttributesForIndexing 方法的实现示例

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();

更新 ISearchable 模型的索引

$searchEngine->updateIndex($model);

删除 ISearchable 模型的索引

$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();
}

许可

搜索引擎采用 MIT 许可证授权。