anyitsolutions/elastic-adapter

官方 PHP Elasticsearch 客户端适配器

1.2.1 2020-10-02 02:31 UTC

This package is auto-updated.

Last update: 2024-09-29 05:42:59 UTC


README

Latest Stable Version Total Downloads License Build Status Donate PayPal Donate Amazon

Elastic Adapter 是官方 PHP Elasticsearch 客户端的适配器。它旨在简化基本的索引和文档操作。

内容

兼容性

当前版本的 Elastic Adapter 已与以下配置进行测试

  • PHP 7.2-7.4
  • Elasticsearch 7.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\Index('my_index');

$indexManager->create($index);

或者根据您的需求进行相应配置

$mapping = (new \ElasticAdapter\Indices\Mapping())
    ->text('title', [
        'boost' => 2,
    ])
    ->keyword('tag', [
        'null_value' => 'NULL'
    ])
    ->geoPoint('location');

$settings = (new \ElasticAdapter\Indices\Settings())
    ->index([
        'number_of_replicas' => 2,
        'refresh_interval' => -1
    ]);

$index = new \ElasticAdapter\Indices\Index('my_index', $mapping, $settings);

$indexManager->create($index);

删除

删除一个索引

$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);

设置

更新索引设置

$settings = (new \ElasticAdapter\Indices\Settings())
    ->analysis([
        'analyzer' => [
            'content' => [
                'type' => 'custom',
                'tokenizer' => 'whitespace'    
            ]
        ]
    ]);

$indexManager->putSettings('my_index', $settings);

存在

检查索引是否存在

$indexManager->exists('my_index');

打开

打开一个索引

$indexManager->open('my_index');

关闭

关闭一个索引

$indexManager->close('my_index');

文档管理

IndexManager 类似,DocumentManager 类也依赖于 Elasticsearch 客户端

$client = \Elasticsearch\ClientBuilder::fromConfig([
  'hosts' => [
      'localhost:9200'
  ]
]);

$documentManager = new \ElasticAdapter\Documents\DocumentManager($client);

索引

将文档添加到索引中

$documents = [
    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);

删除

从索引中删除文档

$documents = [
    new ElasticAdapter\Documents\Document('1', ['title' => 'foo']),
    new ElasticAdapter\Documents\Document('2', ['title' => 'bar']),
];

$documentManager->delete('my_index', $documents);

如果您想立即刷新索引,请将第三个参数传递为 true

$documentManager->delete('my_index', $documents, true);

您还可以使用查询删除文档

$documentManager->deleteByQuery('my_index', ['match_all' => new \stdClass()]);

搜索

在一个索引中查找文档

$request = new \ElasticAdapter\Search\SearchRequest([
    'match' => [
        'message' => 'test'
    ]
]);

$request->setHighlight([
    'fields' => [
        'message' => [
            'type' => 'plain',
            'fragment_size' => 15,
            'number_of_fragments' => 3,
            'fragmenter' => 'simple'
        ]
    ]
]);

$request->setSuggest([
    'my_suggest' => [
        'text' => 'test',
        'term' => [
            'field' => 'message'
        ]
    ]
]);

$request->setSource(['message', 'post_date']);

$request->setCollapse([
    'field' => 'user'
]);

$request->setSort([
    ['post_date' => ['order' => 'asc']],
    '_score'
]);

$request->setFrom(0)->setSize(20);

$response = $documentManager->search('my_index', $request);

// total number of matched documents
$total = $response->getHitsTotal(); 

// corresponding hits
$hits = $response->getHits();

// document, highlight or raw representation of the hit
foreach ($hits as $hit) {
    $document = $hit->getDocument();
    $highlight = $hit->getHighlight();
    $raw = $hit->getRaw();
}

// suggestions
$suggestions = $response->getSuggestions();