{"id":34650359,"url":"https://github.com/roaatech/php-es-mapper","last_synced_at":"2026-03-12T22:32:26.431Z","repository":{"id":33873839,"uuid":"37583164","full_name":"roaatech/php-es-mapper","owner":"roaatech","description":"An elasticsearch simple mapping ORM for php","archived":false,"fork":false,"pushed_at":"2017-12-06T07:58:44.000Z","size":72,"stargazers_count":33,"open_issues_count":1,"forks_count":8,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-12-14T09:56:29.553Z","etag":null,"topics":["builder","data-mapper","elastic","elasticsearch","orm","php","query-builder"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/roaatech.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-06-17T08:41:06.000Z","updated_at":"2024-03-05T11:35:50.000Z","dependencies_parsed_at":"2022-08-21T10:40:44.640Z","dependency_job_id":null,"html_url":"https://github.com/roaatech/php-es-mapper","commit_stats":null,"previous_names":[],"tags_count":30,"template":false,"template_full_name":null,"purl":"pkg:github/roaatech/php-es-mapper","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roaatech%2Fphp-es-mapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roaatech%2Fphp-es-mapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roaatech%2Fphp-es-mapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roaatech%2Fphp-es-mapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/roaatech","download_url":"https://codeload.github.com/roaatech/php-es-mapper/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roaatech%2Fphp-es-mapper/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30446445,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-12T21:31:01.033Z","status":"ssl_error","status_checked_at":"2026-03-12T21:30:43.161Z","response_time":114,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["builder","data-mapper","elastic","elasticsearch","orm","php","query-builder"],"created_at":"2025-12-24T17:55:33.174Z","updated_at":"2026-03-12T22:32:26.416Z","avatar_url":"https://github.com/roaatech.png","language":"PHP","readme":"# ElasticSearch PHP ORM and Query Builder (es-mapper)\nThis is a simple ORM mapper for ElasticSearch for PHP.\n\n[ElasticSearch DSL query builder for PHP](./query_builder_README.md).\n\n## Collaborators required\nIf you can join me in updating and maintaining this project, please send a message to\nmuhannad.shelleh@live.com\n\n## Requirements\n - PHP 5.4+\n - Elasticsearch PHP SDK v\u003e=1 and \u003c2\n - ElasticSearch server 1.6. ES2 is not tested, so use with care.\n\n## Installation\n### Composer\n```composer require itvisionsy/php-es-orm```\n\n### Manual download\nHead to the latest version [here](https://github.com/itvisionsy/php-es-mapper/releases/latest) then download using one download button.\n\n## How to use?\n\n**For the Query Builder, [read this README instead](./query_builder_README.md)**\n\nThat is simple:\n\n### Per index query:\n 1. Create a class extending the main query class (for general index use) .\n 1. Fill in the abstract methods. They are self-descriptive.\n 1. Use the created class `::find($type, $id)`, `::query($type, array $query =[])`, and `::all($type)`\n    You will get a list of Model objects where you can object-property access to get all the info.\n    i.e. `$model-\u003ename` to get the name property, ...\n\n### Per type query\n 1. Create a class extending the type query class.\n    OR create a class extending the main query class and implementing the `TypeQueryInterface` interface and use the `TypeQueryTrait` trait\n 1. Fill in the abstract methods. They are self-descriptive.\n 1. Use the methods: `::find($id)`, `::all()`, and `::query(array $query=[])`.\n    You will get a list of Model objects the same way described above.\n\n#### Please note\nMethods' parameters are mapped to original elasticsearch methods and parameters as follows:\n * `::find(scalar)` and `::find(scalar[])` methods are mapped to [get](https://github.com/elastic/elasticsearch-php/blob/master/src/Elasticsearch/Client.php# L167) and [mget](https://github.com/elastic/elasticsearch-php/blob/master/src/Elasticsearch/Client.php# L671) methods respectively.\n * `::query` method is mapped to the [search](https://github.com/elastic/elasticsearch-php/blob/master/src/Elasticsearch/Client.php# L1002) method, and the $query param will be passed as is after appending the index and type parameters to it.\n\n### Querying for data\nThe query class is just a simple interface allows you to send DSL queries, or perform other ElasticSearch requests.\nThe `::query()` method for example will expect to receive an assoc-array with a well-formed DSL query.\n\nHowever, you can use the query builder to builder the query and get a well-formed DSL array out of it. \n\nYou can use a type-query query builder to build the query and execute it directly:\n```PHP\n$result = TypeQuery::builder()\n    -\u003ewhere('key1','some value')\n    -\u003ewhere('key2',$intValue,'\u003e')\n    -\u003ewhere('key3','value','!=')\n    -\u003ewhere('key4', ['value1','value2'])\n    -\u003eexecute();\n//$result is a \\ItvisionSy\\EsMapper\\Result instance\n```\n\nOr you can use a generic query builder to build the query then you can modify it using other tools:\n```PHP\n//init a builder object\n$builder = \\ItvisionSy\\EsMapper\\QueryBuilder::make();\n\n//build the query using different methods\n$query = $builder\n                -\u003ewhere('key1','some value') //term clause\n                -\u003ewhere('key2',$intValue,'\u003e') //range clause\n                -\u003ewhere('key3','value','!=') //must_not term clause\n                -\u003ewhere('key4', ['value1','value2']) //terms clause\n                -\u003ewhere('email', '@hotmail.com', '*=') //wildcard search for all @hotmail.com emails\n                -\u003esort('key1','asc') //first sort option\n                -\u003esort('key2',['order'=\u003e'asc','mode'=\u003e'avg']) //second sort option\n                -\u003efrom(20)-\u003esize(20) //results from 20 to 39\n                -\u003etoArray();\n\n//modify the query as you need\n$query['aggs']=['company'=\u003e['terms'=\u003e['field'=\u003e'company']]];\n\n//then execute it against a type query\n$result = TypeQuery::query($query);\n//$result is a \\ItvisionSy\\EsMapper\\Result instance\n```\n\nPlease refer to [this file](./query_builder_README.md) for more detailed information.\n\n## Retrieving results\nThe returned result set implements the ArrayAccess interface to access specific document inside the result. i.e.\n```PHP\n$result = SomeQuery::all();\n```\nYou can then get a document like this:\n```PHP\n$doc = $result[1]; //gets the second document\n```\nOr you can use the dot notation like that:\n```\n$result-\u003efetch('hits.hits.0'); //for any absolute access\n```\n\n## Accessing document data\nOn the model object, you can access the results in many ways:\n 1. using the object attribute accessor `$object-\u003eattribute`\n    - if the attribute starts with underscore (_) then it will try to fetch it first from the meta information, then the attributes, and then from the internal object properties.\n    - if the attribute starts with two underscores (__) then it will try to fetch first from the internal object properties, then attributes, then meta.\n    - if not precedence underscores, then it will try to fetch from the attributes, then meta, then internal object properties.\n 1. using the `$object-\u003egetAttributes()[attribute]`, as the getAttributes() will return the document data as an array (first level only).\n 1. using the `$object-\u003egetAttributes($attribute1, $attribute2, ...)` which will return a single (or array) value[s] depending on the requested attributes\n\n## Creating new documents\nEither way will work:\n 1. Use the index query static method\n    ```php\n    IndexQuery::create(array $data, $type, $id=null, array $parameters=[])\n    ```\n    \n 1. Use the type query static method:\n   ```php\n   TypeQuery::create(array $data, $id=null, array $parameters=[])\n   ```\n\n## Updating a document\nYou can update an already indexed document by:\n 1. Either *Re-indexing* a document with the same type and id, OR\n 1. Or `update(array $data, array $parameters=[])` method on the model's object:\n   \n   ```php\n   TypeQuery::find(1)-\u003eupdate(['new_key'=\u003e'value','old_key'=\u003e'new value'],[]);\n   ```\n\n## Deleting a document\nThe same way you can update a document, you can delete it:\n 1. Calling the static method `::delete($type, $id)` on the index query\n 1. Calling the method `-\u003edelete()` on model's object.\n\n## Adding extra methods\nYou may need to add extra custom methods like `top($numOfDocs)` or anything else.\nTo do so, you need to create the method name you wish as protected in the query sub-class. The name should be prefixed with _ (i.e. `_top`) then, you can either\n * Call it prefixed with `get`, so to call the `_top(500)` method, just call `getTop(500)` and it will be mapped as public static and as public. \n * Override the `_allowPublicAccess` static protected method to add extra methods to expose. \n    Please note that when overriding, don't forget to merge with the parent results not to lose the old ones:\n    ```PHP\n    protected _allowPublicAccess(){\n        return array_merge(parent::_allowPublicAccess(), ['top','any',...]);\n    }\n    ```\n\n    This way you will save the allowed methods from the parent.\n\n### Extending the Model class\nYou can extend the Model class easily. Just extend it!\nIn case you were using the namespaces, you can set the models namespace in the query class by overriding the modelNamespace public method. This method should return a string ending with \\\nAfter that, you need to call the `-\u003esetModelClass($class)` on the query result object.\n\n## Examples\nPlease check [tests/](/tests) folder. Basically, the case1.php is the main file.\n\n```\n\\\n|\n+-- TestsIndexQuery (TestsIndexQuery.php)   Main index query class.\n|   |                                       Maps results to \\Models\\ namespace.\n|   |\n|   +-- \\FooTypeQuery (FooTypeQuery.php)    Type index query class.\n|   |\n|   +-- \\BarTypeQuery (BarTypeQuery.php)    Type index query class. \n|\n|-- Models\\\n    |\n    +-- Foo (FooMode.php)                   Foo model class\n    |\n    +-- Bar (BarModel.php)                  Bar model class\n```\n\n## License\nThis code is published under [MIT](LICENSE) license.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Froaatech%2Fphp-es-mapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Froaatech%2Fphp-es-mapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Froaatech%2Fphp-es-mapper/lists"}