{"id":20002828,"url":"https://github.com/babenkoivan/elastic-adapter","last_synced_at":"2025-05-16T13:08:07.012Z","repository":{"id":36467149,"uuid":"215644365","full_name":"babenkoivan/elastic-adapter","owner":"babenkoivan","description":"An adaptor for official PHP Elasticsearch client","archived":false,"fork":false,"pushed_at":"2025-04-07T07:01:58.000Z","size":212,"stargazers_count":40,"open_issues_count":0,"forks_count":31,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-19T16:27:02.354Z","etag":null,"topics":["adapter","client","elasticsearch","php"],"latest_commit_sha":null,"homepage":"","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/babenkoivan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"ko_fi":"ivanbabenko","custom":["https://paypal.me/babenkoi"]}},"created_at":"2019-10-16T21:05:00.000Z","updated_at":"2025-04-07T07:00:40.000Z","dependencies_parsed_at":"2025-02-24T16:06:16.676Z","dependency_job_id":"82c6ebe6-30bb-4163-a8c0-8c8026559f74","html_url":"https://github.com/babenkoivan/elastic-adapter","commit_stats":{"total_commits":145,"total_committers":6,"mean_commits":"24.166666666666668","dds":"0.11724137931034484","last_synced_commit":"e7a853526e040a8b8e89977c3f304dc3d5cbd882"},"previous_names":[],"tags_count":36,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/babenkoivan%2Felastic-adapter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/babenkoivan%2Felastic-adapter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/babenkoivan%2Felastic-adapter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/babenkoivan%2Felastic-adapter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/babenkoivan","download_url":"https://codeload.github.com/babenkoivan/elastic-adapter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254535829,"owners_count":22087399,"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":["adapter","client","elasticsearch","php"],"created_at":"2024-11-13T05:23:14.296Z","updated_at":"2025-05-16T13:08:06.994Z","avatar_url":"https://github.com/babenkoivan.png","language":"PHP","funding_links":["https://ko-fi.com/ivanbabenko","https://paypal.me/babenkoi"],"categories":[],"sub_categories":[],"readme":"# Elastic Adapter\n\n[![Latest Stable Version](https://poser.pugx.org/babenkoivan/elastic-adapter/v/stable)](https://packagist.org/packages/babenkoivan/elastic-adapter)\n[![Total Downloads](https://poser.pugx.org/babenkoivan/elastic-adapter/downloads)](https://packagist.org/packages/babenkoivan/elastic-adapter)\n[![License](https://poser.pugx.org/babenkoivan/elastic-adapter/license)](https://packagist.org/packages/babenkoivan/elastic-adapter)\n[![Tests](https://github.com/babenkoivan/elastic-adapter/workflows/Tests/badge.svg)](https://github.com/babenkoivan/elastic-adapter/actions?query=workflow%3ATests)\n[![Code style](https://github.com/babenkoivan/elastic-adapter/workflows/Code%20style/badge.svg)](https://github.com/babenkoivan/elastic-adapter/actions?query=workflow%3A%22Code+style%22)\n[![Static analysis](https://github.com/babenkoivan/elastic-adapter/workflows/Static%20analysis/badge.svg)](https://github.com/babenkoivan/elastic-adapter/actions?query=workflow%3A%22Static+analysis%22)\n[![Donate PayPal](https://img.shields.io/badge/donate-paypal-blue)](https://paypal.me/babenkoi)\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://ko-fi.com/ivanbabenko\" target=\"_blank\"\u003e\u003cimg src=\"https://ko-fi.com/img/githubbutton_sm.svg\" alt=\"Support the project!\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n---\n\nElastic Adapter is an adapter for the official PHP Elasticsearch client. It's designed to simplify basic index and document\noperations.\n\n## Contents\n\n* [Compatibility](#compatibility)\n* [Installation](#installation)\n* [Configuration](#configuration)\n* [Index Management](#index-management)\n* [Document Management](#document-management)\n* [Point in Time Management](#point-in-time-management)\n\n## Compatibility\n\nThe current version of Elastic Adapter has been tested with the following configuration:\n\n* PHP 8.2\n* Elasticsearch 8.x\n* Laravel 11.x\n\nIf your project uses older Laravel (or PHP) version check [the previous major version](https://github.com/babenkoivan/elastic-adapter/tree/v3.5.0#compatibility) of the package.\n\n## Installation\n\nThe library can be installed via Composer:\n\n```bash\ncomposer require babenkoivan/elastic-adapter\n```\n\n## Configuration\n\nElastic Adapter uses [babenkoivan/elastic-client](https://github.com/babenkoivan/elastic-client) as a dependency.\nTo change the client settings you need to publish the configuration file first:\n\n```bash\nphp artisan vendor:publish --provider=\"Elastic\\Client\\ServiceProvider\"\n```\n\nIn the newly created `config/elastic.client.php` file you can define the default connection name and describe multiple\nconnections using configuration hashes. Please, refer to\nthe [elastic-client documentation](https://github.com/babenkoivan/elastic-client) for more details.\n\n## Index Management\n\n`\\Elastic\\Adapter\\Indices\\IndexManager` is used to manipulate indices.\n\n### Create\n\nCreate an index, either with the default settings and mapping:\n\n```php\n$index = new \\Elastic\\Adapter\\Indices\\Index('my_index');\n\n$indexManager-\u003ecreate($index);\n```\n\nor configured according to your needs:\n\n```php\n$mapping = (new \\Elastic\\Adapter\\Indices\\Mapping())\n    -\u003etext('title', [\n        'boost' =\u003e 2,\n    ])\n    -\u003ekeyword('tag', [\n        'null_value' =\u003e 'NULL'\n    ])\n    -\u003egeoPoint('location')\n    -\u003edynamic(true)\n    -\u003edynamicTemplate('no_doc_values', [\n        'match_mapping_type' =\u003e '*',\n        'mapping' =\u003e [\n            'type' =\u003e '{dynamic_type}',\n            'doc_values' =\u003e false,\n        ],\n    ]);\n\n$settings = (new \\Elastic\\Adapter\\Indices\\Settings())\n    -\u003eindex([\n        'number_of_replicas' =\u003e 2,\n        'refresh_interval' =\u003e -1\n    ]);\n\n$index = new \\Elastic\\Adapter\\Indices\\Index('my_index', $mapping, $settings);\n\n$indexManager-\u003ecreate($index);\n```\n\nAlternatively, you can create an index using raw input:\n\n```php\n$mapping = [\n    'properties' =\u003e [\n        'title' =\u003e [\n            'type' =\u003e 'text'\n        ]   \n    ]\n];\n\n$settings = [\n    'number_of_replicas' =\u003e 2\n];\n\n$indexManager-\u003ecreateRaw('my_index', $mapping, $settings);\n```\n\n### Drop\n\nDelete an index:\n\n```php\n$indexManager-\u003edrop('my_index');\n```\n\n### Put Mapping\n\nUpdate an index mapping using builder:\n\n```php\n$mapping = (new \\Elastic\\Adapter\\Indices\\Mapping())\n    -\u003etext('title', [\n        'boost' =\u003e 2,\n    ])\n    -\u003ekeyword('tag', [\n        'null_value' =\u003e 'NULL'\n    ])\n    -\u003egeoPoint('location');\n\n$indexManager-\u003eputMapping('my_index', $mapping);\n```\n\nor using raw input:\n\n```php\n$mapping = [\n    'properties' =\u003e [\n        'title' =\u003e [\n            'type' =\u003e 'text'\n        ]   \n    ]\n];\n\n$indexManager-\u003eputMappingRaw('my_index', $mapping);\n```\n\n### Put Settings\n\nUpdate an index settings using builder:\n\n```php\n$settings = (new \\Elastic\\Adapter\\Indices\\Settings())\n    -\u003eanalysis([\n        'analyzer' =\u003e [\n            'content' =\u003e [\n                'type' =\u003e 'custom',\n                'tokenizer' =\u003e 'whitespace'    \n            ]\n        ]\n    ]);\n\n$indexManager-\u003eputSettings('my_index', $settings);\n```\n\nor using raw input:\n\n```php\n$settings = [\n    'number_of_replicas' =\u003e 2\n];\n\n$indexManager-\u003eputSettingsRaw('my_index', $settings);\n```\n\n### Exists\n\nCheck if an index exists:\n\n```php\n$indexManager-\u003eexists('my_index');\n```\n\n### Open\n\nOpen an index:\n\n```php\n$indexManager-\u003eopen('my_index');\n```\n\n### Close\n\nClose an index:\n\n```php\n$indexManager-\u003eclose('my_index');\n```\n\n### Put Alias\n\nCreate an alias:\n\n```php\n$alias = new \\Elastic\\Adapter\\Indices\\Alias('my_alias', true, [\n    'term' =\u003e [\n        'user_id' =\u003e 12,\n    ],\n]);\n\n$indexManager-\u003eputAlias('my_index', $alias);\n```\n\nThe same with raw input:\n\n```php\n$settings = [\n    'is_write_index' =\u003e true,\n    'filter' =\u003e [\n        'term' =\u003e [\n            'user_id' =\u003e 12,\n        ],\n    ],\n];\n\n$indexManager-\u003eputAliasRaw('my_index', 'my_alias', $settings);\n```\n\n### Get Aliases\n\nGet index aliases:\n\n```php\n$indexManager-\u003egetAliases('my_index');\n```\n\n### Delete Alias\n\nDelete an alias:\n\n```php\n$indexManager-\u003edeleteAlias('my_index', 'my_alias');\n```\n\n### Connection\n\nSwitch Elasticsearch connection:\n\n```php\n$indexManager-\u003econnection('my_connection');\n```\n\n## Document Management\n\n`\\Elastic\\Adapter\\Documents\\DocumentManager` is used to manage and search documents. \n\n### Index\n\nAdd a document to the index:\n\n```php\n$documents = collect([\n    new \\Elastic\\Adapter\\Documents\\Document('1', ['title' =\u003e 'foo']),\n    new \\Elastic\\Adapter\\Documents\\Document('2', ['title' =\u003e 'bar']),\n]);\n\n$documentManager-\u003eindex('my_index', $documents);\n```\n\nThere is also an option to refresh index immediately:\n\n```php\n$documentManager-\u003eindex('my_index', $documents, true);\n```\n\nFinally, you can set a custom routing:\n\n```php\n$routing = (new \\Elastic\\Adapter\\Documents\\Routing())\n    -\u003eadd('1', 'value1')\n    -\u003eadd('2', 'value2');\n\n$documentManager-\u003eindex('my_index', $documents, false, $routing);\n```\n\n### Delete\n\nRemove a document from the index:\n\n```php\n$documentIds = ['1', '2'];\n\n$documentManager-\u003edelete('my_index', $documentIds);\n```\n\nIf you want the index to be refreshed immediately pass `true` as the third argument:\n\n```php\n$documentManager-\u003edelete('my_index', $documentIds, true);\n```\n\nYou can also set a custom routing:\n\n```php\n$routing = (new \\Elastic\\Adapter\\Documents\\Routing())\n    -\u003eadd('1', 'value1')\n    -\u003eadd('2', 'value2');\n\n$documentManager-\u003edelete('my_index', $documentIds, false, $routing);\n```\n\nFinally, you can delete documents using query:\n\n```php\n$documentManager-\u003edeleteByQuery('my_index', ['match_all' =\u003e new \\stdClass()]);\n```\n\n### Search\n\nSearch documents in the index:\n\n```php\n// configure search parameters\n$searchParameters = new \\Elastic\\Adapter\\Search\\SearchParameters();\n\n// specify indices to search in\n$searchParameters-\u003eindices(['my_index1', 'my_index2']);\n\n// define the query\n$searchParameters-\u003equery([\n    'match' =\u003e [\n        'message' =\u003e 'test'\n    ]\n]);\n\n// configure highlighting\n$searchParameters-\u003ehighlight([\n    'fields' =\u003e [\n        'message' =\u003e [\n            'type' =\u003e 'plain',\n            'fragment_size' =\u003e 15,\n            'number_of_fragments' =\u003e 3,\n            'fragmenter' =\u003e 'simple'\n        ]\n    ]\n]);\n\n// add suggestions\n$searchParameters-\u003esuggest([\n    'message_suggest' =\u003e [\n        'text' =\u003e 'test',\n        'term' =\u003e [\n            'field' =\u003e 'message'\n        ]\n    ]\n]);\n\n// enable source filtering\n$searchParameters-\u003esource(['message', 'post_date']);\n\n// collapse fields\n$searchParameters-\u003ecollapse([\n    'field' =\u003e 'user'\n]);\n\n// aggregate data\n$searchParameters-\u003eaggregations([\n    'max_likes' =\u003e [\n        'max' =\u003e [\n            'field' =\u003e 'likes'\n        ]\n    ]\n]);\n\n// sort documents\n$searchParameters-\u003esort([\n    ['post_date' =\u003e ['order' =\u003e 'asc']],\n    '_score'\n]);\n\n// rescore documents\n$searchParameters-\u003erescore([\n    'window_size' =\u003e 50,\n    'query' =\u003e [\n        'rescore_query' =\u003e [\n            'match_phrase' =\u003e [\n                'message' =\u003e [\n                    'query' =\u003e 'the quick brown',\n                    'slop' =\u003e 2,\n                ],\n            ],\n        ],\n        'query_weight' =\u003e 0.7,\n        'rescore_query_weight' =\u003e 1.2,\n    ]\n]);\n\n// add a post filter\n$searchParameters-\u003epostFilter([\n    'term' =\u003e [\n        'cover' =\u003e 'hard'\n    ]\n]);\n\n// track total hits\n$searchParameters-\u003etrackTotalHits(true);\n\n// track scores\n$searchParameters-\u003etrackScores(true);\n\n// script fields\n$searchParameters-\u003escriptFields([\n    'my_doubled_field' =\u003e [\n        'script' =\u003e [\n            'lang' =\u003e 'painless',\n            'source' =\u003e 'doc[params.field] * params.multiplier',\n            'params' =\u003e [\n                'field' =\u003e 'my_field',\n                'multiplier' =\u003e 2,\n            ],\n        ],\n    ],\n]);\n\n// boost indices\n$searchParameters-\u003eindicesBoost([\n    ['my-alias' =\u003e 1.4],\n    ['my-index' =\u003e 1.3],\n]);\n\n// define the search type\n$searchParameters-\u003esearchType('query_then_fetch');\n\n// set the preference\n$searchParameters-\u003epreference('_local');\n\n// use pagination\n$searchParameters-\u003efrom(0)-\u003esize(20);\n\n// search after\n$searchParameters-\u003epointInTime([\n    'id' =\u003e '46ToAwMDaWR5BXV1',\n    'keep_alive' =\u003e '1m',\n]);\n\n$searchParameters-\u003esearchAfter([\n    '2021-05-20T05:30:04.832Z',\n    4294967298,\n]);\n\n// use custom routing\n$searchParameters-\u003erouting(['user1', 'user2']);\n\n// enable explanation\n$searchParameters-\u003eexplain(true);\n\n// set maximum number of documents to collect for each shard\n$searchParameters-\u003eterminateAfter(10);\n\n// enable caching\n$searchParameters-\u003erequestCache(true);\n\n// perform the search and get the result\n$searchResult = $documentManager-\u003esearch($searchParameters);\n\n// get the total number of matching documents\n$total = $searchResult-\u003etotal(); \n\n// get the corresponding hits\n$hits = $searchResult-\u003ehits();\n\n// every hit provides access to the related index name, the score, the document, the highlight and more\n// in addition, you can get a raw representation of the hit\nforeach ($hits as $hit) {\n    $indexName = $hit-\u003eindexName();\n    $score = $hit-\u003escore();\n    $document = $hit-\u003edocument();\n    $highlight = $hit-\u003ehighlight();\n    $innerHits = $hit-\u003einnerHits();\n    $innerHitsTotal = $hit-\u003einnerHitsTotal();\n    $raw = $hit-\u003eraw();\n    \n    // get an explanation \n    $explanation = $searchResult-\u003eexplanation();\n    \n    // every explanation includes a value, a description and details\n    // it is also possible to get its raw representation\n    $value = $explanation-\u003evalue();\n    $description = $explanation-\u003edescription();\n    $details = $explanation-\u003edetails();\n    $raw = $explanation-\u003eraw();\n}\n\n// get suggestions\n$suggestions = $searchResult-\u003esuggestions();\n\n// get aggregations\n$aggregations = $searchResult-\u003eaggregations();\n```\n\n### Connection\n\nSwitch Elasticsearch connection:\n\n```php\n$documentManager-\u003econnection('my_connection');\n```\n\n## Point in Time Management\n\n`\\Elastic\\Adapter\\Search\\PointInTimeManager` is used to control points in time.\n\n### Open\n\nOpen a point in time:\n\n```php\n$pointInTimeId = $pointInTimeManager-\u003eopen('my_index', '1m');\n```\n### Close\n\nClose a point in time:\n\n```php\n$pointInTimeManager-\u003eclose($pointInTimeId);\n```\n\n### Connection\n\nSwitch Elasticsearch connection:\n\n```php\n$pointInTimeManager-\u003econnection('my_connection');\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbabenkoivan%2Felastic-adapter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbabenkoivan%2Felastic-adapter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbabenkoivan%2Felastic-adapter/lists"}