aman-rawat / elastic-adapter
Elasticsearch官方PHP客户端的适配器
1.0.1
2022-08-01 13:14 UTC
Requires
- php: ^7.3 || ^8.0
- elasticsearch/elasticsearch: ^7.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- orchestra/testbench: ^6.20
- phpstan/phpstan: ^0.12.32
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-09-29 06:31:06 UTC
README
Elastic Adapter是Elasticsearch官方PHP客户端的适配器。它旨在简化基本的索引和文档操作。
内容
兼容性
Elastic Adapter当前版本与以下配置进行了测试:
- PHP 7.3-8.0
- Elasticsearch 7.x
- Laravel 6.x-8.x
安装
可以通过Composer安装此库
composer require babenkoivan/elastic-adapter
索引管理
IndexManager
可用于操作索引。它依赖于Elasticsearch客户端,因此您在创建IndexManager
实例之前需要初始化客户端
$client = \Elasticsearch\ClientBuilder::fromConfig([ 'hosts' => [ 'localhost:9200' ] ]); $indexManager = new \ElasticAdapter\Indices\IndexManager($client);
管理器提供了一系列有用的方法,如下所示。
创建
创建索引,可以带有默认设置和映射
$index = new \ElasticAdapter\Indices\IndexBlueprint('my_index'); $indexManager->create($index);
或根据您的需求进行配置
$mapping = (new \ElasticAdapter\Indices\Mapping()) ->text('title', [ 'boost' => 2, ]) ->keyword('tag', [ 'null_value' => 'NULL' ]) ->geoPoint('location') ->dynamicTemplate('no_doc_values', [ 'match_mapping_type' => '*', 'mapping' => [ 'type' => '{dynamic_type}', 'doc_values' => false, ], ]); $settings = (new \ElasticAdapter\Indices\Settings()) ->index([ 'number_of_replicas' => 2, 'refresh_interval' => -1 ]); $index = new \ElasticAdapter\Indices\IndexBlueprint('my_index', $mapping, $settings); $indexManager->create($index);
或者,您可以使用原始输入创建索引
$mapping = [ 'properties' => [ 'title' => [ 'type' => 'text' ] ] ]; $settings = [ 'number_of_replicas' => 2 ]; $indexManager->createRaw('my_index', $mapping, $settings);
删除
删除索引
$indexManager->drop('my_index');
设置映射
使用构建器更新索引映射
$mapping = (new \ElasticAdapter\Indices\Mapping()) ->text('title', [ 'boost' => 2, ]) ->keyword('tag', [ 'null_value' => 'NULL' ]) ->geoPoint('location'); $indexManager->putMapping('my_index', $mapping);
或使用原始输入
$mapping = [ 'properties' => [ 'title' => [ 'type' => 'text' ] ] ]; $indexManager->putMappingRaw('my_index', $mapping);
设置设置
使用构建器更新索引设置
$settings = (new \ElasticAdapter\Indices\Settings()) ->analysis([ 'analyzer' => [ 'content' => [ 'type' => 'custom', 'tokenizer' => 'whitespace' ] ] ]); $indexManager->putSettings('my_index', $settings);
或使用原始输入
$settings = [ 'number_of_replicas' => 2 ]; $indexManager->putSettingsRaw('my_index', $settings);
存在
检查索引是否存在
$indexManager->exists('my_index');
打开
打开索引
$indexManager->open('my_index');
关闭
关闭索引
$indexManager->close('my_index');
设置别名
创建别名
$alias = new \ElasticAdapter\Indices\Alias('my_alias', [ 'term' => [ 'user_id' => 12, ], ]); $indexManager->putAlias('my_index', $alias);
获取别名
获取索引别名
$indexManager->getAliases('my_index');
删除别名
删除别名
$indexManager->deleteAlias('my_index', 'my_alias');
文档管理
与IndexManager
类似,DocumentManager
类也依赖于Elasticsearch客户端
$client = \Elasticsearch\ClientBuilder::fromConfig([ 'hosts' => [ 'localhost:9200' ] ]); $documentManager = new \ElasticAdapter\Documents\DocumentManager($client);
索引
将文档添加到索引中
$documents = collect([ new ElasticAdapter\Documents\Document('1', ['title' => 'foo']), new ElasticAdapter\Documents\Document('2', ['title' => 'bar']), ]); $documentManager->index('my_index', $documents);
还可以立即刷新索引
$documentManager->index('my_index', $documents, true);
最后,您可以设置自定义路由
$routing = (new ElasticAdapter\Documents\Routing()) ->add('1', 'value1') ->add('2', 'value2'); $documentManager->index('my_index', $documents, false, $routing);
删除
从索引中删除文档
$documentIds = ['1', '2']; $documentManager->delete('my_index', $documentIds);
如果希望立即刷新索引,则将true
作为第三个参数传递
$documentManager->delete('my_index', $documentIds, true);
您还可以设置自定义路由
$routing = (new ElasticAdapter\Documents\Routing()) ->add('1', 'value1') ->add('2', 'value2'); $documentManager->delete('my_index', $documentIds, false, $routing);
最后,您可以使用查询删除文档
$documentManager->deleteByQuery('my_index', ['match_all' => new \stdClass()]);
搜索
在索引中搜索文档
// create a search request $request = new \ElasticAdapter\Search\SearchRequest([ 'match' => [ 'message' => 'test' ] ]); // configure highlighting $request->highlight([ 'fields' => [ 'message' => [ 'type' => 'plain', 'fragment_size' => 15, 'number_of_fragments' => 3, 'fragmenter' => 'simple' ] ] ]); // add suggestions $request->suggest([ 'message_suggest' => [ 'text' => 'test', 'term' => [ 'field' => 'message' ] ] ]); // enable source filtering $request->source(['message', 'post_date']); // collapse fields $request->collapse([ 'field' => 'user' ]); // aggregate data $request->aggregations([ 'max_likes' => [ 'max' => [ 'field' => 'likes' ] ] ]); // sort documents $request->sort([ ['post_date' => ['order' => 'asc']], '_score' ]); // rescore documents $request->rescore([ 'window_size' => 50, 'query' => [ 'rescore_query' => [ 'match_phrase' => [ 'message' => [ 'query' => 'the quick brown', 'slop' => 2, ], ], ], 'query_weight' => 0.7, 'rescore_query_weight' => 1.2, ] ]); // add a post filter $request->postFilter([ 'term' => [ 'cover' => 'hard' ] ]); // track total hits $request->trackTotalHits(true); // track scores $request->trackScores(true); // script fields $request->scriptFields([ 'my_doubled_field' => [ 'script' => [ 'lang' => 'painless', 'source' => 'doc[params.field] * params.multiplier', 'params' => [ 'field' => 'my_field', 'multiplier' => 2, ], ], ], ]); // boost indices $request->indicesBoost([ ['my-alias' => 1.4], ['my-index' => 1.3], ]); // define the search type $request->searchType('query_then_fetch'); // set the preference $request->preference('_local'); // use pagination $request->from(0)->size(20); // execute the search request and get the response $response = $documentManager->search('my_index', $request); // get the total number of matching documents $total = $response->total(); // get the corresponding hits $hits = $response->hits(); // every hit provides access to the related index name, the score, the document, the highlight and the inner hits // in addition, you can get a raw representation of the hit foreach ($hits as $hit) { $indexName = $hit->indexName(); $score = $hit->score(); $document = $hit->document(); $highlight = $hit->highlight(); $innerHits = $hit->innerHits(); $raw = $hit->raw(); } // get the suggestions $suggestions = $response->suggestions(); // get the aggregations $aggregations = $response->aggregations();