modulargaming / search
Modular Gaming 的搜索模块
dev-master
2014-03-22 13:40 UTC
Requires
- php: >=5.4
- modulargaming/installer: ~1.0
- ruflin/elastica: ~1.0.1
This package is not auto-updated.
Last update: 2020-01-10 15:22:14 UTC
README
Search 是一个为 Modular Gaming,一个模块化的 持久化基于浏览器的游戏 框架的模块。
它使用 ElasticSearch 实现了一个搜索系统。
要求
- PHP 5.4+
- ElasticSearch
- Composer(依赖管理器)
安装
使用 composer 安装 Search,只需将其添加到您的 composer.json 文件中的依赖项即可
{
"require": {
"modulargaming/search": "~0.1.0"
}
}
复制配置文件,config/search.php 到您的应用程序目录,并编辑它以匹配您的设置。
使用
模型,保存
可搜索的模型需要使用 Model_ElasticSearch 特性,并实现 get_search_document 和 send_search_mapping 函数。
class Model_User extends MG_Model_User { use Model_ElasticSearch; /** * @return \Elastica\Document */ public function get_search_document() { return new \Elastica\Document($this->id, array( 'id' => $this->id, 'username' => $this->username )); } /** * @return \Elastica\Type\Mapping */ public function send_search_mapping() { $type = ElasticSearch::instance()->get_type($this->_search_type()); $mapping = new \Elastica\Type\Mapping(); $mapping->setType($type); // Set mapping $mapping->setProperties(array( 'id' => array('type' => 'integer', 'include_in_all' => FALSE), 'username' => array('type' => 'string', 'include_in_all' => TRUE), )); // Send mapping to type $mapping->send(); } }
映射可以通过 Minion 任务 php ./minion Search:mapping --model=User 设置,或者手动输入到 ElasticSearch。
对可搜索模型的所有更改也将向 ElasticSearch 发送匹配的查询以保持匹配。
值得注意的是,所有更改都将触发搜索查询更新,这可以通过覆盖更新函数来更改,只调用 trait::update 如果相关属性已更改。
可以使用 Minion 任务 php ./minion Search:index --model=User 重建搜索索引。
搜索页面,检索
搜索页面目前搜索整个索引以找到匹配的记录。每个搜索结果都使用与搜索类型 (_type) 匹配的视图类进行解析。
示例
示例用户模型。注意我们仅在用户名属性更改时触发更新。
class Model_User extends MG_Model_User { use Model_ElasticSearch { update as _traitUpdate; } /** * @return \Elastica\Document */ public function get_search_document() { return new \Elastica\Document($this->id, array( 'id' => $this->id, 'username' => $this->username )); } /** * @return \Elastica\Type\Mapping */ public function send_search_mapping() { $type = ElasticSearch::instance()->get_type($this->_search_type()); $mapping = new \Elastica\Type\Mapping(); $mapping->setType($type); // Set mapping $mapping->setProperties(array( 'id' => array('type' => 'integer', 'include_in_all' => FALSE), 'username' => array('type' => 'string', 'include_in_all' => TRUE), )); // Send mapping to type $mapping->send(); } /** * Ensure we only update if the username was changed, to prevent useless queries at login (last_login). * * @param Validation $validation * @return $this|ORM */ public function update(Validation $validation = NULL) { if ($this->changed('username')) { $this->_traitUpdate($validation); } else { parent::update($validation); } return $this; } }