PHPackages                             ljw/es - 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. ljw/es

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

ljw/es
======

a simple es handler

v1.0(8y ago)3211MITPHPPHP &gt;=5.6

Since Nov 7Pushed 8y ago1 watchersCompare

[ Source](https://github.com/lejianwen/es)[ Packagist](https://packagist.org/packages/ljw/es)[ Docs](https://github.com/lejianwen/es)[ RSS](/packages/ljw-es/feed)WikiDiscussions master Synced 2mo ago

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

es使用
====

[](#es使用)

- 项目要用到es，所以自己进行了一下简单的封装
- es版本2.3

安装
--

[](#安装)

composer require ljw/es:dev-master

配置
--

[](#配置)

```
//配置es
\Ljw\Es\Es::$hosts = '127.0.0.1';
\Ljw\Es\Es::$port = '9200';
```

开始使用
----

[](#开始使用)

### 建立索引

[](#建立索引)

- 创建一个类继承model

```
class Product extends \Ljw\Es\Model
{
    //指定索引
    protected $index = 'product_index';
    //指定索引别名
    protected $alias = 'product_alias';
    //指定type
    protected $type = 'products';
    /**
     * 索引设置
     * @var array
     */
    protected $setting = [];
    /**
     * 索引参数
     * @var array
     */
    protected $properties = [
        'id'             => ['type' => 'long'],
        'name'           => ['type' => 'string'],
        'description'    => ['type' => 'string'],
    ];

}
```

- 注：如果没有在类里面指定index，alias和type,会自动设置为

{class}\_index | {class}\_alias | {class}\_type

**{class}** 为类名小写

- 运行

```
//创建索引。会根据Product中的$setting，$properties来创建
Product::createIndex();
//删除索引
Product::deleteIndex();
//跟新索引。根据Product中的$properties来跟新，只能进行追加，不能修改和删除字段
Product::updateIndex();
```

---

### 查询

[](#查询)

1. 直接查询

```
//简单查询,where只支持 = , > , < , >= , index('pro_index')
    ->type('type')
    ->where(...)
    ->orWhere(...)
    ->where(function ($q) {
        $q->where(...)->orWhere(...);
    })
    ->order('id', 'desc')
    ->page($page, $page_size)
    ->search();
$list = $query->getData(); //数据列表
$count = $query->getCount(); //数据总量
// ---------------
// 聚合，支持 group,avg,sum,max,min
$query = \Ljw\Es\Es::query()
    ->index('pro_index')
    ->type('type')
    ->where(...)
    ->group('xxx', function($q){  //  group by xxx 并计算不同xxx下面aaa的和
        $q->sum('aaa');
    })
    ->sum('id') // 计算id的和
    ->search();
$data = $query->getData();
```

2. 创建模型查询

```
class Product extends \Ljw\Es\Model
{
    //指定索引
    protected $index = 'product_index';
    //指定索引别名
    protected $alias = 'product_alias';
    //指定type
    protected $type = 'products';
    protected $setting = [...];
    protected $properties = [...];

}
$re = Product::query()->where(...)->orWhere(...)->search();
$list = $re->getData(); //数据列表
$count = $re->getCount(); //数据总量
$re = Product::query()->where(...)->orWhere(...)->group(...)->search();
$data = $re->getData();
```

### 添加，修改，删除数据

[](#添加修改删除数据)

- 建立模型

```
class Product extends \Ljw\Es\Model
{
    //指定索引
    protected $index = 'product_index';
    //指定索引别名
    protected $alias = 'product_alias';
    //指定type
    protected $type = 'products';
    protected $setting = [...];
    protected $properties = [...];

}
// $id 必须为唯一id， $data为数据，会根据 $properties 自动剔除
Product::add($id, $data);
//删除
Product::delete($id);
//跟新数据
Product::update($id, $data);
```

注：暂时就这些简单的功能了
=============

[](#注暂时就这些简单的功能了)

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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

Unknown

Total

1

Last Release

3111d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/15119474?v=4)[乐](/maintainers/lejianwen)[@lejianwen](https://github.com/lejianwen)

---

Top Contributors

[![lejianwen](https://avatars.githubusercontent.com/u/15119474?v=4)](https://github.com/lejianwen "lejianwen (6 commits)")

---

Tags

elasticelasticsearchljwelasticesljw

### Embed Badge

![Health badge](/badges/ljw-es/health.svg)

```
[![Health](https://phpackages.com/badges/ljw-es/health.svg)](https://phpackages.com/packages/ljw-es)
```

###  Alternatives

[jeroen-g/explorer

Next-gen Elasticsearch driver for Laravel Scout.

397612.3k](/packages/jeroen-g-explorer)[babenkoivan/elastic-client

The official PHP Elasticsearch client integrated with Laravel

544.0M6](/packages/babenkoivan-elastic-client)[madewithlove/elasticsearcher

Wrapper on top of the ElasticSearch PHP SDK which allows easier index/document/query management.

264133.2k2](/packages/madewithlove-elasticsearcher)[thomasjsn/laravel-scout-elastic

Elastic Driver for Laravel Scout

1411.5k](/packages/thomasjsn-laravel-scout-elastic)[blomstra/search

Replaces Flarum search with one powered by an elastic search server.

114.9k](/packages/blomstra-search)

PHPackages © 2026

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