PHPackages                             linhanwei/thinkphp-elasticsearch - 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. [Database &amp; ORM](/categories/database)
4. /
5. linhanwei/thinkphp-elasticsearch

ActiveLibrary[Database &amp; ORM](/categories/database)

linhanwei/thinkphp-elasticsearch
================================

Thinkphp use SQL statements to query elasticsearch

v1.0.1(5y ago)430MITPHPPHP &gt;=7.2.0

Since May 18Pushed 5y ago1 watchersCompare

[ Source](https://github.com/linhanwei/thinkphp-elasticsearch)[ Packagist](https://packagist.org/packages/linhanwei/thinkphp-elasticsearch)[ Docs](https://github.com/linhanwei/thinkphp-elasticsearch)[ RSS](/packages/linhanwei-thinkphp-elasticsearch/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (2)Versions (3)Used By (0)

Thinkphp Elasticsearch
======================

[](#thinkphp-elasticsearch)

版本对应
----

[](#版本对应)

Elasticsearch Versionthinkphp-elasticsearch Branch&gt;= 7.01.\*Install
-------

[](#install)

You can install the package via composer:

```
composer require linhanwei/thinkphp-elasticsearch

```

Thinkphp
--------

[](#thinkphp)

服务区提供者，门面配置 `app / service.php`

```
return [
    think\elastic\service\ElasticService::class,
];

```

简单上手
----

[](#简单上手)

### Create

[](#create)

```
$result = \Elasticsearch::index('index')->type('type')->create([
    'key' => 'value',
    'key2' => 'value2',
]);
```

### Update

[](#update)

```
$result = \Elasticsearch::index('index')->type('type')->update('id',[
    'key' => 'value2',
]);
dump($result);
```

### Delete

[](#delete)

```
$result = \Elasticsearch::index('index')->type('type')->delete('id');
dump($result);
```

### Select

[](#select)

```
//指定索引index和type
$builder = \Elasticsearch::index('laisiou_test')->type('user');

//全查询，但因ES聚合查询引擎的原因只能返回10条
$result = $builder->get();

//骚操作全查询
$result = $builder->take($builder->count())->get();

//根据id查询一条 注：查询不到则返回 null
$result = $builder->whereTerm('id',1)->first();

//子条件查询
$result = $builder->where(function ($inQuery) {
    $inQuery->whereTerm('key',1)->orWhereTerm('key',2)
})->whereTerm('key1',1)->get();
```

### 更多查询

[](#更多查询)

skip / take

```
$builder->take(10)->get(); // or limit(10)
$builder->offset(10)->take(10)->get(); // or skip(10)
```

term query

```
$builder->whereTerm('key',value)->first();
```

match query

```
$builder->whereMatch('key',value)->first();
```

range query

```
$builder->whereBetween('key',[value1,value2])->first();
```

where in query

```
$builder->whereIn('key',[value1,value2])->first();
```

logic query

```
$builder->whereTerm('key',value)->orWhereTerm('key2',value)->first();
```

nested query

```
$result = $builder->where(function (Builder $inQuery) {
    $inQuery->whereTerm('key',1)->orWhereTerm('key',2)
})->whereTerm('key1',1)->get();
```

### 所有可用条件方法

[](#所有可用条件方法)

```
public function select($columns): self
```

```
public function where($column, $operator = null, $value = null, $leaf = 'term', $boolean = 'and'): self
```

```
public function orWhere($field, $operator = null, $value = null, $leaf = 'term'): self
```

```
public function whereMatch($field, $value, $boolean = 'and'): self
```

```
public function orWhereMatch($field, $value, $boolean = 'and'): self
```

```
public function whereTerm($field, $value, $boolean = 'and'): self
```

```
public function whereIn($field, array $value)
```

```
public function orWhereIn($field, array $value)
```

```
public function orWhereTerm($field, $value, $boolean = 'or'): self
```

```
public function whereRange($field, $operator = null, $value = null, $boolean = 'and'): self
```

```
public function orWhereRange($field, $operator = null, $value = null): self
```

```
public function whereBetween($field, array $values, $boolean = 'and'): self
```

```
public function orWhereBetween($field, array $values): self
```

```
public function orderBy(string $field, $sort): self
```

```
public function scroll(string $scroll): self
```

```
public function aggBy($field, $type): self
```

```
public function select($columns): self
```

### 获取结果的方法

[](#获取结果的方法)

```
public function get(): Collection
```

```
public function paginate(int $page, int $perPage = 15): Collection
```

```
public function first()
```

```
public function byId($id)
```

```
public function byIdOrFail($id): stdClass
```

```
public function chunk(callable $callback, $limit = 2000, $scroll = '10m')
```

```
public function create(array $data, $id = null, $key = 'id'): stdClass
```

```
public function update($id, array $data): bool
```

```
public function delete($id)
```

```
public function count(): int
```

### 日志

[](#日志)

```
$builder->enableQueryLog();
```

### Elastisearch object

[](#elastisearch-object)

```
\Elasticsearch::getElasticSearch() // 或者 \Elasticsearch::search()
```

License
-------

[](#license)

[MIT license](https://opensource.org/licenses/MIT)

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 80% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

1825d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/16769092?v=4)[darren](/maintainers/linhanwei)[@linhanwei](https://github.com/linhanwei)

---

Top Contributors

[![darren111111](https://avatars.githubusercontent.com/u/105826278?v=4)](https://github.com/darren111111 "darren111111 (4 commits)")[![linhanwei](https://avatars.githubusercontent.com/u/16769092?v=4)](https://github.com/linhanwei "linhanwei (1 commits)")

---

Tags

phpelasticsearchormsqlthinkphp

### Embed Badge

![Health badge](/badges/linhanwei-thinkphp-elasticsearch/health.svg)

```
[![Health](https://phpackages.com/badges/linhanwei-thinkphp-elasticsearch/health.svg)](https://phpackages.com/packages/linhanwei-thinkphp-elasticsearch)
```

###  Alternatives

[crcms/elasticsearch

Use SQL statements to query elasticsearch

1058.3k1](/packages/crcms-elasticsearch)[matchory/elasticsearch

The missing elasticsearch ORM for Laravel!

3059.0k](/packages/matchory-elasticsearch)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
