PHP code example of chenyuanqi / elasticsearch

1. Go to this page and download the library: Download chenyuanqi/elasticsearch library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chenyuanqi / elasticsearch example snippets


'providers' => [
	// elasticsearch service
	chenyuanqi\elasticsearch\SearchServiceProvider::class,
],

'aliases' => [
	// elasticsearch service facade
	'Search' => chenyuanqi\elasticsearch\SearchFacade::class,
],

Search::createIndex();

Search::createMapping();
Search::updateMapping();
Search::deleteMapping();

Search::index('test')->type('test');

$data = [
    'name'  => 'Kyyomi',
    'age'   => 18,
    'birth' => '2017-03-03'
];
// However, you can set id for the record. For instance, "Search::insert($data, 1);"
Search::insert($data);

$data = [
    'birth' => '1999-03-03'
];
// update by id
Search::updateById($data, 1);
// update by query
Search::queryString('name:"Kyyomi"')->update($data);

Search::queryString('name:"海盗之王"')->increase('age');
Search::queryString('name:"海盗之王"')->increase('age', 2);
Search::queryString('name:"海盗之王"')->decrease('age', 3);

$data = [
    'birth' => '1999-03-03'
];
// delete by id
Search::deleteById(1);
// delete by query, not support for version >2.0 (consider plugin: delete-by-query) 
Search::queryString('name:"Kyyomi"')->delete();

Search::truncate();

$data = [
    [
        'index',
        '_id'  => 1,
        'name' => 'viki',
        'age'  => 18
    ],
    [
        'create',
        '_id'  => 2,
        'name' => 'lucy',
        'age'  => 15
    ],
    [
        'update',
        '_id'  => 1,
        'name' => 'vikey',
        'age'  => 28
    ],
    [
        'delete',
        '_id' => 2
    ]
]
Search::bulk($data);

Search::pluck(['name', 'age'])->search();

Search::pluck(['name', 'age'])->search(false);

 Search::queryString('name=Kyyomi');
 

 Search::filter('status', 'show');
 

 Search::ids([1, 2, 3]);
 // If other field with in or not in function
 Search::whereIn('name', ['A', 'B', 'C']);
 Search::whereNotIn('name', ['A', 'B', 'C']);
 

 Search::match('name', 'Kyyomi', 'match');
 Search::match(['name', 'age'], 'Kyyomi', 'multi_match');
 Search::match('Kyyomi');
 

Search::term('name', 'Kyyomi');

// The third parameter Kyyomi', 'must_not');

Search::isNull('name');
// If need the field is not null
Search::isNotNull('name');

Search::max('id');
Search::min('id');
Search::sum('id');
Search::avg('id');

Search::range('age', [7, 18], ['gt', 'lte']);

Search::where('id', '=', 100)->search();
// The same as last sentence
Search::where('id', 100)->search();
Search::where('id', '=', 100)->orWhere('age', '>=', 18)->search();
// Also, we can use like query
Search::where('name', 'like', '%天天%')->search();
// any more where function like whereBetween, whereNotBetween
Search::whereBetween('id', [1, 2]);

// paging style
Search::queryString('name:"珍珠海盗"')->limit(0, 10)->search();
// scroll style
Search::queryString('name:"珍珠海盗"')->scroll(1000, '30s', 'scan')->search();
// If you want use scroll id for search or delete it
Search::searchByScrollId('xxx');
Search::deleteByScrollId('xxx');

Search::queryString('name:"珍珠海盗"')->count();

Search::queryString('name:"珍珠海盗"')->search();
Search::debug();
// If you need curl sentence, do it
Search::toCurl();

use chenyuanqi\elasticsearch\Builder;
$search = new Builder(false);
json
{
"php": ">=7.0",
"illuminate/support": "~5.1",
"elasticsearch/elasticsearch": "~5.0"
}
bash
php artisan vendor:publish