{"id":20963852,"url":"https://github.com/codemix/yiielasticsearch","last_synced_at":"2025-05-14T09:31:41.273Z","repository":{"id":8219750,"uuid":"9657154","full_name":"codemix/YiiElasticSearch","owner":"codemix","description":"Elastic Search client for Yii","archived":false,"fork":false,"pushed_at":"2017-02-17T16:02:10.000Z","size":77,"stargazers_count":33,"open_issues_count":8,"forks_count":17,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-04-17T17:42:50.219Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codemix.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-04-24T20:37:00.000Z","updated_at":"2022-11-06T15:38:28.000Z","dependencies_parsed_at":"2022-08-06T22:15:24.998Z","dependency_job_id":null,"html_url":"https://github.com/codemix/YiiElasticSearch","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codemix%2FYiiElasticSearch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codemix%2FYiiElasticSearch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codemix%2FYiiElasticSearch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codemix%2FYiiElasticSearch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codemix","download_url":"https://codeload.github.com/codemix/YiiElasticSearch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254112261,"owners_count":22016721,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-19T02:48:37.123Z","updated_at":"2025-05-14T09:31:36.259Z","avatar_url":"https://github.com/codemix.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# YiiElasticSearch\n\nElastic Search client for Yii.\n\n# Installation\n\nInstall via composer, requires php \u003e= 5.3\n\n# Configuration\n\nAdd the following to your application config:\n\n```php\n'components' =\u003e array(\n    'elasticSearch' =\u003e array(\n        'class' =\u003e 'YiiElasticSearch\\Connection',\n        'baseUrl' =\u003e 'http://localhost:9200/',\n    ),\n    ...\n)\n```\n\nAlso make sure, that you include the autoloader of composer. We recommend\nto add this line to your `index.php` and maybe also your `yiic.php`:\n\n```php\n// Include composer autoloader\nrequire_once(__DIR__.'/protected/vendor/autoload.php');\n```\n\nMake sure to modify the path so that it matches the location of your `vendor/`\ndirectory.\n\n# Usage\n\n## Index your ActiveRecords\n\nAttach the `YiiElasticSearch\\SearchableBehavior` to any of your ActiveRecords to make it easy\nto index and search your normal models with elasticsearch.\n\n```php\nclass MyModel extends CActiveRecord\n{\n    public function behaviors()\n    {\n        return array(\n            'searchable' =\u003e array(\n                'class' =\u003e 'YiiElasticSearch\\SearchableBehavior',\n            ),\n        );\n    }\n}\n```\n\nNow when MyModel instances are saved or deleted they will be automatically indexed or deleted in\nelasticsearch as appropriate.\n\n\n### Define an index for a record\n\nBy default your records will be stored in an index that uses your sanitized application name\n(`Yii::app()-\u003ename`). To change it you can define\n\n```php\nclass MyModel extends CActiveRecord\n{\n    public $elasticIndex = 'myindex';\n```\n\nor, if you need more control, create a method\n\n```php\nclass MyModel extends CActiveRecord\n{\n    public function getElasticIndex()\n    {\n        return 'myindex';\n    }\n```\n\n\n### Define a type for a record\n\nBy default the lower case class name will be used as type name in elasticsearch. If you want to\nchange that you can define\n\n```php\nclass MyModel extends CActiveRecord\n{\n    public $elasticType = 'mymodel';\n```\n\nor, again, if you need more control, create a method\n\n```php\nclass MyModel extends CActiveRecord\n{\n    public function getElasticType()\n    {\n        return 'mymodel';\n    }\n```\n\n### Customize indexed data\n\nBy default all attributes are stored in the index. If you need to customize the data\nthat should be indexed in elasticsearch, you can override these two methods.\n\n```php\nclass MyModel extends CActiveRecord\n{\n    /**\n     * @param DocumentInterface $document the document where the indexable data must be applied to.\n     */\n    public function populateElasticDocument(DocumentInterface $document)\n    {\n        $document-\u003esetId($this-\u003eid);\n        $document-\u003ename     = $this-\u003ename;\n        $document-\u003estreet   = $this-\u003estreet;\n    }\n\n    /**\n     * @param DocumentInterface $document the document that is providing the data for this record.\n     */\n    public function parseElasticDocument(DocumentInterface $document)\n    {\n        // You should always set the match score from the result document\n        if ($document instanceof SearchResult)\n            $this-\u003esetElasticScore($document-\u003egetScore());\n\n        $this-\u003eid       = $document-\u003egetId();\n        $this-\u003ename     = $document-\u003ename;\n        $this-\u003estreet   = $document-\u003estree;\n    }\n```\n\n\n## Query records\n\nYou can specify queries using the `YiiElasticSearch\\Search` object. This object provides a simple\nOO wrapper for the vanilla elasticsearch [search API](http://www.elasticsearch.org/guide/reference/api/search/).\n\nFor example:\n\n```php\n$search = new \\YiiElasticSearch\\Search(\"myindex\", \"mymodel\");\n$search-\u003equery = array(\n    \"match_all\" =\u003e array()\n);\n\n// start returning results from the 20th onwards\n$search-\u003eoffset = 20;\n```\n\nWith a search you can either perform a 'raw' query, e.g.\n\n```php\n$resultSet = Yii::app()-\u003eelasticSearch-\u003esearch($search);\n```\n\nThis will return a result set that is a very simple wrapper around the raw elastic search response.\n\nAlternatively, when combined with a `SearchableBehavior` you can use data providers, e.g.\n\n```php\n$dataProvider = new \\YiiElasticSearch\\DataProvider(MyModel::model(), array(\n        'search' =\u003e $search\n));\n```\n\nThe data from `$dataProvider-\u003edata` is a list of ActiveRecords, just like from an ordinary\n`CActiveDataProvider`. So you can use it in any list or grid view.\n\n## Raw requests\n\nYou can also use the connection component to send raw requests to elasticsearch.\n\n\n```php\n// Will be an instance of a Guzzle\\Http\\Client\n$client = Yii::app()-\u003eelasticSearch-\u003eclient;\n\n$mapping = array(\n   'country' =\u003e array(\n        'properties' =\u003e array(\n            'name' =\u003e array(\n                'type' =\u003e 'string',\n            ),\n        ),\n    ),\n\n// Create a mapping\n$request = $client-\u003eput('myindex', array(\"Content-type\" =\u003e \"application/json\"));\n$request-\u003esetBody(array('mapping' =\u003e $mapping));\n\n$response = $request-\u003esend();\n\n$result = $response-\u003egetBody();\n```\n\n## Console Maintenance\n\nThe extension comes with two simple maintenance commands that can be helpful to find out\nwhat's going on in your index. To configure them, add this to your `console.php` configuration:\n\n```php\n'commandMap' =\u003e array(\n    'elastic' =\u003e array(\n        'class' =\u003e 'YiiElasticSearch\\ConsoleCommand',\n    ),\n    'zerodowntimeelastic' =\u003e array(\n        'class' =\u003e 'YiiElasticSearch\\ZeroDowntimeConsoleCommand',\n    ),\n),\n```\n\nThis will allow you to use `yiic elastic` and `yiic zerodowntimeelastic` on the console. Here are the commands help:\n\n### Console Command\n\nThis is the maintenance command for the elasticSearch component.\n\n```\nACTIONS\n\n  index --model=\u003cmodel\u003e [--skipExisting]\n\n    Add all models \u003cmodel\u003e to the index. This will replace any previous\n    entries for this model in the index. Index and type will be auto-detected\n    from the model class unless --index or --type is set explicitely.\n    If --skipExisting is used, no action is performed if there are already\n    documents indexed under this type.\n\n\n  map --model=\u003cmodel\u003e --map=\u003cfilename\u003e [--skipExisting]\n  map --index=\u003cindex\u003e --map=\u003cfilename\u003e [--skipExisting]\n\n    Create a mapping in the index specified with the \u003cindex\u003e or implicitly\n    through the \u003cmodel\u003e parameter. The mapping must be available from a JSON\n    file in \u003cfilename\u003e where the JSON must have this form:\n\n        {\n            \"tweet\" : {\n                \"properties\": {\n                    \"name\" : {\"type\" : \"string\"},\n                    ...\n            },\n            ...\n        }\n\n    If --skipExisting is used, no action is performed if there's are already\n    a mapping for this index.\n\n\n  list [--limit=10] [--offset=0]\n  list [--model=\u003cname\u003e] [--limit=10] [--offset=0]\n  list [--index=\u003cname\u003e] [--type=\u003ctype\u003e] [--limit=10] [--offset=0]\n\n    List all entries in elasticsearch. If a model or an index (optionally with\n    a type) is specified only entries matching index and type of the model will be listed.\n\n\n  delete --model=\u003cname\u003e [--id=\u003cid\u003e]\n\n    Delete a document from an index. If no \u003cid\u003e is specified the whole\n    index will be deleted.\n\n  help\n\n    Show this help\n```\n\n### ZeroDowntime Command\n\nThis is a zero downtime maintenance command for the elasticSearch component.\nMore details: [https://www.elastic.co/blog/changing-mapping-with-zero-downtime](https://www.elastic.co/blog/changing-mapping-with-zero-downtime)\n\n```\nACTIONS\n\n  * index --models=\u003cmodel1\u003e,...\n    Add all models to the index.\n\n  * status --models=\u003cmodel1\u003e,...\n    Displays actual indexes and aliases\n\n  * schema --models=\u003cmodel1\u003e,...\n        [--version=201512121212] [--forceMigrate=false] [--bulkCopy=true] [--updateAlias=true] [--deleteIndexes=true]\n\n    Creates schema for the given models. Steps:\n     1, compare mapping\n     2, if migration is needed, create the new schema version, always create new if \u003cforceMigrate\u003e is true\n     3, bulk copy the previous data if \u003cbulkCopy\u003e is true\n     4, update aliases if \u003cupdateAlias\u003e is true\n     5, delete indexes if \u003cdeleteIndexes\u003e is true\n\n\n  * bulkCopy --from=index/type --to=index2/type [--properties=]\n    Bulk copy all data\n\n  * changeAlias --from=value --to=value [--old=]\n    Change alias\n\n  * deleteIndex --indexesToDelete=value\n    Delete index with all types\n    \n  help\n\n    Show this help\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodemix%2Fyiielasticsearch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodemix%2Fyiielasticsearch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodemix%2Fyiielasticsearch/lists"}