2017 © Pedro Peláez
 

modulargaming-module search

Search module for Modular Gaming

image

modulargaming/search

Search module for Modular Gaming

  • Saturday, March 22, 2014
  • by modulargaming
  • Repository
  • 3 Watchers
  • 0 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Modular Gaming Search

Search is a module for Modular Gaming, a modular persistent browser based game framework., (*1)

It implements a search system using ElasticSearch., (*2)

Requirements

  • PHP 5.4+
  • ElasticSearch
  • Composer (Dependency Manager)

Installation

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)

Using

Models, Saving

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)

Search page, Retrieving

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)

Examples

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;
    }
}

The Versions

22/03 2014

dev-master

9999999-dev https://github.com/modulargaming/search

Search module for Modular Gaming

  Sources   Download

BSD-3-Clause

The Requires

 

modulargaming