boyfoo / elasticsearch-query
elasticsearch query build
0.0.2
2021-01-23 06:29 UTC
Requires
- php: >=5.6
- ext-json: *
Requires (Dev)
- phpunit/phpunit: ~5
- symfony/var-dumper: 3.4
This package is auto-updated.
Last update: 2024-10-23 15:23:02 UTC
README
🌈 ElasticSearch DSL 查询语句构建组件
安装
$ composer require boyfoo/elasticsearch-query -vvv
使用
示例
使用 search
构建器起步创建查询:
use Boyfoo\ElasticsearchSql\Search; use Boyfoo\ElasticsearchSql\Query; // 创建查询 $params = Search::create() ->index('goods') ->source(['no', 'price', 'category']) ->size(10) ->query(function (Query $query) { $query->mustMatch("小米手机")->mustTerm('category', '电子产品'); });
打印结果 var_dump($params->toArray()
:
[ "index" => "goods", "type" => "_doc", "body" => [ "_source" => ["no", "name", "price", "category"], "size" => 10, "query" => [ "bool" => [ "must" => [ [ "match" => ["name" => "小米手机"] ], [ "term" => [ "category" => ["value" => "电子产品"] ] ] ] ] ] ] ];
将结果通过 elasticsearch/elasticsearch
官方扩展包执行:
use Elasticsearch\ClientBuilder; ... $client = ClientBuilder::create()->fromConfig($config); $client->search($params);
查询
Boyfoo\ElasticsearchSql\Query
类为 Elasticsearch query
语句构建类
use Boyfoo\ElasticsearchSql\Query; $query = Query::create() ->mustMatch('字段1', '内容1') ->notTerm('字段1', '内容2') ->shouldRange('字段3', [ '>=' => 2018, '<=' => 2019 ]);
打印 query
构建器结果: var_dump($query->toArray())
:
[ "bool" => [ "must" => [ [ "match" => [ "字段1" => "内容1" ] ] ], "must_not" => [ [ "term" => [ "字段1" => [ "value" => "内容2" ] ] ] ], "should" => [ [ "range" => [ "字段3" => [ "gte" => 2018, "lte" => 2019 ] ] ] ] ] ];
将 query
构建器放入 search
构建器内:
use Boyfoo\ElasticsearchSql\Search; use Boyfoo\ElasticsearchSql\Query; $query = Query::create()->mustTerms('key', 'value'); Search::create()->query($query);
License
MIT