dev-master
9999999-dev https://github.com/modulargaming/searchSearch module for Modular Gaming
BSD-3-Clause
The Requires
modulargaming
Search module for Modular Gaming
Search is a module for Modular Gaming, a modular persistent browser based game framework., (*1)
It implements a search system using ElasticSearch., (*2)
Search is installed using composer, simply add it as a dependency to your composer.json
file:, (*3)
{ "require": { "modulargaming/search": "~0.1.0" } }
Copy the configuration file, config/search.php to your application directory and edit it to match your settings., (*4)
Searchable models need to use the Model_ElasticSearch trait, and implement the functions get_search_document
and send_search_mapping
., (*5)
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(); } }
The mapping can either be set with the Minion Task php ./minion Search:mapping --model=User
or manually
entered into ElasticSearch., (*6)
All changes to a searchable model will also send the matching queries to ElasticSearch to keep them matching., (*7)
Worth noting is that ALL changes will trigger a search query update, this can be changed by overwriting the update function to only call the trait::update if related property was changed., (*8)
The search indexes can be rebuilt using the Minion Task php ./minion Search:index --model=User
., (*9)
The search page currently searches the whole index for matching records. Each search result gets parsed with a view class matching the search type (_type)., (*10)
The example User model. Notice we only trigger the update if the username property was changed., (*11)
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; } }
Search module for Modular Gaming
BSD-3-Clause
modulargaming