PHP code example of vikram / es

1. Go to this page and download the library: Download vikram/es 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/ */

    

vikram / es example snippets


use Fadion\Bouncy\BouncyTrait;

class Product extends Eloquent {
    
    use BouncyTrait;
    
    // ...other Eloquent attributes
    // or methods.
}

class Product extends Eloquent {

    protected $indexName = 'awesome_index';
    protected $typeName = 'cool_type';

}

use Fadion\Bouncy\BouncyTrait;
use Carbon\Carbon;

class Product extends Eloquent {
    
    use BouncyTrait;
    
    protected $mappingProperties = [
        'title' => [
            'type' => 'keyword'
        ],
        'description' => [
            'type' => 'text',
            'fielddata' => true
        ],
        'created_at' => [
            'type' => 'date',
            'format' => 'yyyy-MM-dd HH:mm:ss'
        ]
    ]
    
    /**
     * You need to have documentFields in order to have the columns indexed
     */
    public function documentFields()
    {
        return [
            'id' => $this->id,
            'title' => $this->title,
            'description' => 'description',
            'created_at' => ($this->created_at != '') ? Carbon::parse($this->created_at)->format('Y-m-d H:i:s'): null,
        ];
    }
}

App\Product::createIndex();

App\Product::putMapping();

App\Product::getMapping();

if (App\Product::hasMapping()) {
    // do something
}

App\Product::deleteIndex();

App\Product::all()->index();

App\Product::where('sold', true)->get()->index();

$product = App\Product::find(10);
$product->index();

$product = App\Product::find(10);
$product->price = 100;
$product->updateIndex();

$product = App\Product::find(10);
$product->updateIndex([
    'price' => 120,
    'sold' => false
]);

App\Product::where('quantity', '<', 25)->get()->removeIndex();

$product = App\Product::find(10);
$product->removeIndex();

App\Product::where('condition', 'new')->get()->reindex();

$product = App\Product::find(10);
$product->reindex();

$product = App\Product::find(10);
$product->indexWithVersion(3);

App\Product::where('price', 100)->update(['price' => 110]);
// or
App\Product::where('price', 100)->delete();

App\Product::where('price', 100)->get()->updateIndex(['price' => 110]);
App\Product::where('price', 100)->update(['price' => 110]);
// or
App\Product::where('price', 100)->get()->removeIndex();
App\Product::where('price', 100)->delete();

$params = [
    'query' => [
        'match' => [
            'title' => 'github'
        ]
    ],
    'size' => 20
];

$products = App\Product::search($params);

foreach ($products as $product) {
    echo $product->title;
}

$products = App\Product::search($params)->paginate();

$products = App\Product::search($params)->paginate(30);

$products->links();

$products = App\Product::search($params)->limit(50);

$products = App\Product::search($params);

$products->total(); // Total number of hits
$products->maxScore(); // Maximum score of the results
$products->took(); // Time in ms it took to run the query
$products->timedOut(); // Wheather the query timed out, or not.

$products->shards(); // Array of shards information
$products->shards($key); // Information on specific shard

$products = App\Product::search($params);

foreach ($products as $product) {
    $product->isDocument(); // Checks if it's an Elasticsearch document
    $product->documentScore(); // Score set in search results
    $product->documentVersion(); // Document version if present
}

$params = [
    'query' => [
        'match' => [
            'title' => 'github'
        ]
    ],
    'highlight' => [
        'fields' => [
            'title' => new \stdClass
        ]
    ]
];

$products = App\Product::search($params);

foreach ($products as $product) {
    echo $product->highlight('title');
}

$products = App\Product::match($title, $query)

$products = App\Product::multiMatch(Array $fields, $query)

$products = App\Product::fuzzy($field, $value, $fuzziness = 'AUTO')

$products = App\Product::geoshape($field, Array $coordinates, $type = 'envelope')

$products = App\Product::ids(Array $values)

$products = App\Product::moreLikeThis(Array $fields, Array $ids, $minTermFreq = 1, $percentTermsToMatch = 0.5, $minWordLength = 3)

$params =	[
	'query' => [
		'match_all' => new \stdClass()
	]
];

// how long between scroll requests. should be small!
$scrollTime = "30s"; 

// how many results *per shard* you want back
$size =  10000;

$products = App\Product::scroll($params, $scrollTime, $size);

$mappingProperties = [
						'name' => [
							'type' => 'completion'
						]
					 ];

$searchText = 'a'; // example
$params = [
                'suggest' => [
                    'name-suggest' => [
                        'prefix' => $searchText,
                        'completion' => [
                            'field' => 'name',
                            'size' => 10,
                        ]
                    ]
                ]
            ];
$productsCollection = App\Product::suggest($params);			
// To get the products from the code
$products = $productsCollection['suggest']['name-suggest'][0]['options'];


use Illuminate\Database\Eloquent\Collection;
use Fadion\Bouncy\BouncyCollectionTrait;

class MyAwesomeCollection extends Collection {

    use BouncyCollectionTrait;

}

Elastic::index();
Elastic::get();
Elastic::search();
Elastic::indices()->create();

// and any other method it provides