PHPackages                             php-module/elastic-php-based-official - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Search &amp; Filtering](/categories/search)
4. /
5. php-module/elastic-php-based-official

ActiveLibrary[Search &amp; Filtering](/categories/search)

php-module/elastic-php-based-official
=====================================

7.1.10(4y ago)58.1k↓41.7%5PHP

Since Oct 11Pushed 2y ago1 watchersCompare

[ Source](https://github.com/callmejea/PHP-Elasticsearch)[ Packagist](https://packagist.org/packages/php-module/elastic-php-based-official)[ RSS](/packages/php-module-elastic-php-based-official/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (14)Used By (0)

### 为什么写

[](#为什么写)

```
由于php版本es操作依赖httpAPI来操作数据, 并且官方的API需要仔细研读方可使用,  这里出现了二封版本的MySQL 操作类

```

### 详细使用方法见

[](#详细使用方法见)

- tests/test.php

### 支持的API

[](#支持的api)

```
filter{geo where and or in "not in" > >= < select(array('field', 'field2'))
    ->from('index')
    ->where('field', DSLBuilder::OPERATOR_EQ, 'value')
    ->whereGeo('geo_point', $lat, $lon, $distance, $minDistance, $unit, $distanceType, $type)
    ->orWhere('field', DSLBuilder::OPERATOR_NE, value8)
    ->andWhereBegin()
    ->orWhere('field', DSLBuilder::OPERATOR_EQ, value)
    ->orWhere('field', DSLBuilder::OPERATOR_NE, value)
    ->orWhere('field', DSLBuilder::OPERATOR_NE, value)
    ->match(要匹配的字段(单个字段或数组), "关键词", $matchType = 'phrase', $type = 'must')
    ->andWhereEnd()
    ->orderBy('field', 'value')
    ->offset($offset)
    ->limit($limit)
    ->debug() //if need
    ->search()
    ->getFormat();
```

### 聚合

[](#聚合)

- 现在聚合支持多聚合了～
- 多重聚合被支持了
- 嵌套nested被支持了

```
$res = $es
    ->select(array('field', 'field2'))
    ->from('index')
     ->beginNested()
    ->setNested('parentField') // 标记嵌套文档聚合开始
    ->where('parentField.field', '!=', 0)
    ->dateHistogram('parentField.filed1', 'year', \PhpES\EsClient\DSLBuilder::AGG_HISTOGRAM_TYPE_CALENDAR, $child)
    ->endNested() // 标记嵌套文档结束
    ->groupBy('field') // 普通聚合 返回值中将自动拼接上 agg_字段名
    ->sum(field) // 获取参与聚合的文档数量 自动拼接 count_字段名
    ->cardinality('field') // 获取不重复的符合条件的总数 自动拼接 cardinality_字段名
    ->sum('field') // 获取sum
    ->search()
    ->getFormat();
```

### 单例模式中不使用连接池

[](#单例模式中不使用连接池)

```
# 继承Client, 重写getClient, 实例化Client时用: setHandler来设置链接数量
class classNmae extends Client{

    /**
     * @throws \Exception
     */
    public function getClient()
    {
        if (empty($this->hosts)) {
            throw new \Exception('using getClient , You must set host before');
        }
        if ($this->client == null) {
            $this->client = self::create()
                ->setHosts($this->hosts)
                ->setHandler(new CurlHandler(['max_handles' => 0]))
                ->build();
        }
        return $this->client;
    }
}
```

### 使用连接池,从这里复制了一份

[](#使用连接池从这里复制了一份)

```
