PHP code example of fadion / bouncy

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

    

fadion / bouncy 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';

}

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

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

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

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

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

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

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

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

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

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

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

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

use Fadion\Bouncy\BouncyTrait;

class Product extends Eloquent {
    
    use BouncyTrait;
    
    protected $mappingProperties = [
        'title' => [
            'type' => 'string',
            'store' => true
        ],
        'description' => [
            'type' => 'string',
            'index' => 'analyzed'
        ]
    ]
    
}

Product::putMapping();

Product::getMapping();

Product::deleteMapping();

Product::rebuildMapping();

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

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

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

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

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

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

$products->links();

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

$products = 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 = 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 = Product::search($params);

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

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

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

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

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

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

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

use Fadion\Bouncy\BouncyTrait;

class Product extends Eloquent {
    
    use BouncyTrait;
    
    public function documentFields()
    {
        return [
            'id' => $this->id,
            'price' => $this->price,
            'rating' => 'perfect'
        ];
    };

}

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