PHPackages                             ngocnm/elastic-query - 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. [API Development](/categories/api)
4. /
5. ngocnm/elastic-query

Abandoned → [ngocnm/laravel-elasticsearch](/?search=ngocnm%2Flaravel-elasticsearch)Library[API Development](/categories/api)

ngocnm/elastic-query
====================

Elasticsearch query

5.0(3y ago)2521LGPL-2.1-onlyPHPPHP &gt;=7.2.0

Since Sep 11Pushed 3y ago1 watchersCompare

[ Source](https://github.com/minhngoc2512/elasticsearch-query)[ Packagist](https://packagist.org/packages/ngocnm/elastic-query)[ GitHub Sponsors](https://github.com/minhngoc2512/elasticsearch-query)[ RSS](/packages/ngocnm-elastic-query/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (1)Versions (20)Used By (0)

### Elastic Query Builder For Lumen, Laravel

[](#elastic-query-builder-for-lumen-laravel)

Install
=======

[](#install)

```
composer require ngocnm/elastic-query

```

Config
======

[](#config)

- Register Service Provider

```
\Ngocnm\ElasticQuery\ElasticsearchServiceProvider::class
```

- Define env

```
ELASTIC_HOST = localhost,localhost_2 #default: localhost
ELASTIC_PORT = 9200 #default: 9200
ELASTIC_INDEX_PREFIX = project_1 #default: null
ELASTIC_USERNAME= root #default: null
ELASTIC_PASSWORD= admin #default: null
ELASTIC_SCHEME = https #default: http
ELASTIC_PATH= /data/elastic #default: null
ELASTICSEARCH_SSN_LOG_DEBUGBAR = true # add log query to debugbar  on core sosanhnha
ELASTIC_CACHE_ENABLED=true #default: true
```

- Config without laravel lumen
    - Create singleton with key (elastic\_query): ```
        define('ELASTICSEARCH_INDEX_PREFIX',env('ELASTIC_INDEX_PREFIX',''));
        define('ELASTICSEARCH_SSN_LOG_DEBUGBAR',env('ELASTICSEARCH_SSN_LOG_DEBUGBAR',false));

        $app->singleton("elastic_query",function(){
            $hosts = env('ELASTIC_HOST','localhost');
            $hosts_config = [
                'port'=>env('ELASTIC_PORT',9200),
                'scheme'=>env('ELASTIC_SCHEME','http'),
                'host'=>$hosts
            ];
            if(!empty(env('ELASTIC_PASSWORD',null))) $hosts_config['pass'] = env('ELASTIC_PASSWORD');
            if(!empty(env('ELASTIC_USERNAME',null))) $hosts_config['user'] = env('ELASTIC_USERNAME');
            if(!empty(env('ELASTIC_PATH',null))) $hosts_config['path'] = env('ELASTIC_PATH');
            if(!empty(env('ELASTIC_SCHEME',null))) $hosts_config['scheme'] = env('ELASTIC_SCHEME');

            if(strpos($hosts,',')!==false){
                $hosts = explode(',',$hosts);
            }
            if(is_array($hosts)){
                $hosts = array_map(function ($host)use($hosts_config){
                    $hosts_config['host'] = $host;
                    return $hosts_config;
                    },$hosts);
                }else{

                $hosts = [$hosts_config];
            }
            return ClientBuilder::create()->setHosts($hosts)->build();
        });
        ```

Get query log
=============

[](#get-query-log)

```
$query_log =\Ngocnm\ElasticQuery\ElasticsearchQueryLog::getLog();
```

Query
=====

[](#query)

- Create Object

```
 $client = new Ngocnm\ElasticQuery\ElasticsearchQuery('index_name');

```

- Select query

```
$response = $client->select('field_1,field_2')->get();
```

- Limit query

```
$response = $client->select('field_1,field_2')->limit(3)->get();
```

- Offset query

```
$response = $client->select('field_1,field_2')->offset($offset)->limit(3)->get();
```

- Where query

```
$response = $client->select('field_1,field_2')->where('field_name',$value)->limit(3)->get();
//or
$response = $client->select('field_1,field_2')->where('field_name','>',$value)->limit(3)->get();
```

- OrderBy query(ASC,DESC)

```
$response = $client->select('field_1,field_2')->orderBy('field_name','asc')->get();
```

- Where between query

```
$value = ['value_1','value_2'];
$response = $client->select('field_1,field_2')->whereBetween('field_name',$value)->get();
```

- Where GeoDistance query

```
//$distance = '1km' default
//column name map: location
$response = $client->select('field_1,field_2')->whereGeoDistance($lat,$lng,$distance,'asc')->get();
```

- Delete row by id

```
$response = $client->delete($id);
```

- Delete Multi rows

```
$response = $client->where('field_name',$value)->deleteMulti();
```

- QueryString - Fulltext search

```
$response = $client->queryString('field_name',$keyword)->get();
```

- Full Text Search Trigrams query

```
$response = $client->select('field_1,field_2')->fullTextSearchTrigrams('field_name',$keyword)->get();
```

- Full Text Search moreLikeThis query: [Document moreLikeThis](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-mlt-query.html)

```
$column = 'field';
//or
$column = ['field_1','field_2'];
$keyword = 'keyword_search';
$config = [
     "min_term_freq" => 1,
     "max_query_terms" => 12
];//default:null

$response = $client->select('field_1,field_2')->moreLikeThis($column,$keyword,$config)->get();
```

- WhereIn query

```
$value = [23,4,5,...];
$response = $client->whereIn('field_name',$value)->get();
```

- WhereNot query

```
$response = $client->WhereNot('field_name',$value)->get();
//Or
$response = $client->WhereNot('field_name','>',$value)->get();
```

- WhereNotIn query

```
$value = [23,4,5,...];
$response = $client->whereNotIn('field_name',$value)->get();
```

- WhereNotBetween query

```
$value = ['value_1','value_2'];
$response = $client->select('field_1,field_2')->whereNotBetween('field_name',$value)->get();
```

- Insert a document or multi documents
    - Insert or update a document ```
        $data = [
            'field_id_unique'=>1,
            'field_1'=>$value_1,
            'field_2'=>$value_2
        ];
        ```
    - Insert or update multi documents ```
        $data = [
              [
                  'field_id_unique'=>1,
                  'field_1'=>$value_1,
                  'field_2'=>$value_2
              ],
              [
                  'field_id_unique'=>2,
                  'field_1'=>$value_1,
                  'field_2'=>$value_2
              ]
        ];
        ```

```
$reponse = $client->insertOrUpdate($data,'name_field_id_unique');
```

- Customize functionScore

```
$keyword = "key search";
$matchs = ['filed' =>'field_name_match', 'value'=>'value_match', 'wieght' => 24];
//or
$matchs = [
        ['filed' =>'field_name_match', 'value'=>'value_match', 'wieght' => 24],
        ['filed' =>'field_name_match_1', 'value'=>'value_match_1', 'wieght' => 42]
    ];

$boost = 5;// default: 5;
$max_boost = 42; // default: 42;
$min_score = 23;// default: 23;
$boost_mode = 'multiply'; // default: multiply;
// multiply :scores are multiplied (default)
// sum : scores are summed
// avg : scores are averaged
// first : the first function that has a matching filter is applied

$score_mode = 'max'; // default: max;
// max : maximum score is used
// min : minimum score is used

$response = $client->queryString('field_name',$keyword)->functionScore($matchs, $boost, $max_boost,$min_score, $boost_mode, $score_mode)->get();
```

[Document functionScore](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html)

- Span near query

```
  $field = 'field_name';
  $spans_term = ['value_1','value_2'];
  $slop = 1;//default: 1
  $in_order = false;// default: false;

  $response = $client->spanNearQuery($field, $spans_term,  $slop, $in_order);
```

[Document span near](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-near-query.html)

- Update multi documents with condition (Update by query)

```
  $data_update = ['field'=>'new_value','field_2'=>'new_value_2'];
  $response = $client->where('field_condition','value_condition')->update($data_update);
```

- Delete index

```
Ngocnm\ElasticQuery\ElasticsearchQuery::deleteIndex($name_index);
```

- CreateIndex index by query

```
$query_create = [
        'index' => $index_name,
        'body' => [
            'settings' => [
                'number_of_shards' => 15,
                'number_of_replicas' => 1
            ]
        ]
    ];

Ngocnm\ElasticQuery\ElasticsearchQuery::createIndex($query_create);
```

- CreateIndex index by options

```
$index_name = 'index_demo';
$number_of_shards = 15; // default:15
$number_of_replicas = 1; // default:15
$mappings = [
                '_source' => [
                    'enabled' => true
                ],
                'properties' => [
                    'location' => [
                        'type' => 'geo_point'
                    ]
                ]
            ]; // default:[]
Ngocnm\ElasticQuery\ElasticsearchQuery::createIndexByOptions($index_name,$number_of_shards,$number_of_replicas,$mappings);
```

[Document mapping](https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index_management.html)

- Check index exist

```
Ngocnm\ElasticQuery\ElasticsearchQuery::indexExists($name_index);
```

- Cache query - Only has on laravel framework(Manage cache's laravel):

```
//Cache forever
$response = $client->WhereNot('field_name',$value)->cache()->get();

//With timeout cache
$timeout = 60;//60 second
$response = $client->WhereNot('field_name',$value)->cache($timeout)->get();
```

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

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

Every ~39 days

Recently: every ~101 days

Total

19

Last Release

1362d ago

Major Versions

1.7 → 2.02021-02-18

2.4 → 3.02021-03-13

3.0 → 4.02021-07-09

4.3 → 5.02022-08-19

PHP version history (2 changes)1.0PHP &gt;=5.5.0

1.2PHP &gt;=7.2.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/660e7de0b540eeaf8aeb187a7872b9e1b5db0d6555881df404d5fa9ffcb97574?d=identicon)[minhngoc2512](/maintainers/minhngoc2512)

---

Top Contributors

[![minhngoc2512](https://avatars.githubusercontent.com/u/25582495?v=4)](https://github.com/minhngoc2512 "minhngoc2512 (20 commits)")

### Embed Badge

![Health badge](/badges/ngocnm-elastic-query/health.svg)

```
[![Health](https://phpackages.com/badges/ngocnm-elastic-query/health.svg)](https://phpackages.com/packages/ngocnm-elastic-query)
```

###  Alternatives

[wheelpros/fitment-platform-api

Magento 2 (Open Source)

12.1k1.2k](/packages/wheelpros-fitment-platform-api)[shift31/laravel-elasticsearch

A Laravel Service Provider for the Elasticsearch API client

201369.7k1](/packages/shift31-laravel-elasticsearch)[handcraftedinthealps/elasticsearch-dsl

Elasticsearch DSL library

212.5M14](/packages/handcraftedinthealps-elasticsearch-dsl)[m6web/elasticsearch-bundle

Symfony2 Bundle on top of elasticsearch/elasticsearch-php

22631.8k2](/packages/m6web-elasticsearch-bundle)

PHPackages © 2026

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