legalthings / elasticsearch-php
PHP版的Elasticsearch
Requires
- php: >=5.6.0
- elasticsearch/elasticsearch: ^5.3
Requires (Dev)
- legalthings/php-code-quality: ^0.1.2
This package is not auto-updated.
Last update: 2024-09-20 21:52:42 UTC
README
此库旨在提供一个简化的接口来使用Elasticsearch。内部使用原始的 elastic/elasticsearch-php 库。
如果需要,可以公开原始库的客户端并使用它。
需求
- PHP >= 5.6.0
- Elasticsearch ~5.3.0
所需的PHP扩展由composer标记
安装
可以使用composer安装此库。
composer require legalthings/elasticsearch-php
客户端
要创建客户端,您需要传递 配置选项。如果没有提供任何选项,则自动使用 localhost:9200 作为主机。
use LegalThings/Elasticsearch; $config = ['hosts' => 'elasticsearch.example.com:9200']; $es = new Elasticsearch($config);
如果需要,您可以直接使用原始的 elastic/elasticsearch-php 客户端。
$info = $es->client->info();
配置
配置传递给Elasticsearch的配置构建器,这意味着您可以提供它接受的任何配置选项。有关更多信息,请参阅 此链接。
如果要在配置中添加自定义键,请将 ["quiet" => true] 添加到配置中。如果没有提供quiet,当Elasticsearch遇到与Elasticsearch客户端无关的键时,将会抛出异常。
[
'hosts' => ['localhost:9200'],
'retries' => 2
]
搜索
使用 search() 方法在Elasticsearch中执行常见的、基本的搜索操作。
此方法自动 转换筛选器和文本搜索 到正确的Elasticsearch等效项,因此您无需手动进行此操作。
use LegalThings/Elasticsearch; $es = new Elasticsearch($config); $index = 'books'; $type = 'ancient'; $text = 'My book'; $fields = ['name']; $filter = [ 'id' => '0001', 'updated(max)' => '2017-01-01T00:00:00', 'year(min)' => 1973, 'published' => false ]; $sort = ['^year']; $limit = 15; $offset = 0; $result = $es->search($index, $type, $text, $fields, $filter, $sort, $limit, $offset);
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": null,
"hits": [{
"_index": "books",
"_type": "ancient",
"_id": "0001",
"_score": null,
"_source": {
"id": "0001",
"updated": "2017-01-01T00:00:00",
"year": 1980,
"published": false,
"name": "My book two"
},
"sort": [1980]
}]
}
}
索引
使用 index() 方法在Elasticsearch中执行常见的、基本的索引操作。
use LegalThings/Elasticsearch; $es = new Elasticsearch($config); $index = 'books'; $type = 'ancient'; $id = '0001'; $data = [ 'id' => '0001', 'updated' => '2017-01-01T00:00:00', 'year' => 1980, 'published' => false, 'name' => 'My book two' ]; $result = $es->index($index, $type, $id, $data);
{
"_index": "books",
"_type": "ancient",
"_id": "0001",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
更新
使用 update() 方法在Elasticsearch中执行常见的、基本的更新操作,该操作部分更新文档。
use LegalThings/Elasticsearch; $es = new Elasticsearch($config); $index = 'books'; $type = 'ancient'; $id = '0001'; $data = [ 'name' => 'My book three' ]; $result = $es->update($index, $type, $id, $data);
{
"_index": "books",
"_type": "ancient",
"_id": "0001",
"_version": 2,
"result": "updated",
"_shards": {
"total": 0,
"successful": 0,
"failed": 0
}
}
获取
使用 get() 方法在Elasticsearch中执行常见的、基本的获取操作。
use LegalThings/Elasticsearch; $es = new Elasticsearch($config); $index = 'books'; $type = 'ancient'; $id = '0001'; $result = $es->get($index, $type, $id);
{
"_index": "books",
"_type": "ancient",
"_id": "0001",
"_version": 1,
"found": true,
"_source": {
"id": "0001",
"updated": "2017-01-01T00:00:00",
"year": 1980,
"published": false,
"name": "My book two"
}
}
删除
使用 delete() 方法在Elasticsearch中执行常见的、基本的删除操作。
use LegalThings/Elasticsearch; $es = new Elasticsearch($config); $index = 'books'; $type = 'ancient'; $id = '0001'; $result = $es->delete($index, $type, $id);
{
"found": true,
"_index": "books",
"_type": "ancient",
"_id": "0001",
"_version": 1,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
筛选器
此库使筛选Elasticsearch中的数据变得容易,因为您不需要将筛选器转换为特定的结构。有关更多示例,请参阅 测试。有关语法信息,请参阅 jasny筛选器。
use LegalThings/ElasticFilter; $filter = [ 'id' => '0001', 'authors' => ['John', 'Jane'], 'deleted' => null, 'start_date(min)' => '2017-01-01T00:00:00', 'end_date(max)' => '2018-01-01T00:00:00', 'age(min)' => 25, 'tags(not)' => ['foo', 'bar'], 'published(not)' => null, 'colors(any)' => ['blue', 'green'], 'colors(none)' => ['red'], 'category(all)' => ['A', 'B', 'C'] ]; $ef = new ElasticFilter($filter); $query = $ef->transform(); $this->assertEquals([ 'bool' => [ 'must' => [ [ 'term' => [ 'id' => '0001' ] ], [ 'terms' => [ 'authors' => ['John', 'Jane'] ] ], [ 'missing' => [ 'field' => 'deleted' ] ], [ 'range' => [ 'start_date' => [ 'gte' => '2017-01-01T00:00:00' ] ] ], [ 'range' => [ 'end_date' => [ 'lte' => '2018-01-01T00:00:00' ] ] ], [ 'range' => [ 'age' => [ 'gte' => 25 ] ] ], [ 'terms' => [ 'colors' => [ 'blue', 'green' ] ] ], [ 'term' => [ 'category' => [ 'A', 'B', 'C' ] ] ] ], 'must_not' => [ [ 'terms' => [ 'tags' => [ 'foo', 'bar' ] ] ], [ 'missing' => [ 'field' => 'published' ] ], [ 'term' => [ 'colors' => [ 'red' ] ] ] ] ] ], $query);
或者,可以使用 流畅的接口 组合筛选器。这将输出与上面示例相同的查询。
use LegalThings/ElasticFilter; $ef = new ElasticFilter(); $query = $ef->addDefaultFilter('id', '0001') ->addDefaultFilter('authors', ['John', 'Jane']) ->addDefaultFilter('deleted', null) ->addMinFilter('start_date', '2017-01-01T00:00:00') ->addMaxFilter('end_date', '2018-01-01T00:00:00') ->addMinFilter('age', 25) ->addNotFilter('tags', ['foo', 'bar']) ->addNotFilter('published', null) ->addAnyFilter('colors', ['blue', 'green']) ->addNoneFilter('colors', ['red']) ->addAllFilter('category', ['A', 'B', 'C']) ->transform(); $this->assertEquals([...], $query); // same output as first example
映射
此库包含预定义的 Elasticsearch映射,您可以使用它。有关更多信息,请参阅 此文件。
use LegalThings/ElasticMap; $es = new Elasticsearch($config); $result = $es->client->indices()->create([ 'index' => 'my_index', 'body' => ElasticMap::getFullTextSearchMapping() ]);