PHP code example of drauta / elasticquent

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

    

drauta / elasticquent example snippets


    $books = Book::where('id', '<', 200)->get();
    $books->addToIndex();

    $books = Book::search('Moby Dick')->get();
    echo $books->totalHits();

    $books = $books->filter(function($book)
    {
        return $book->hasISBN();
    });

use Elasticquent\ElasticquentTrait;

class Book extends Eloquent {

    use ElasticquentTrait;

}



return array(

    /*
    |--------------------------------------------------------------------------
    | Custom Elasticsearch Client Configuration
    |--------------------------------------------------------------------------
    |
    | This array will be passed to the Elasticsearch client.
    | See configuration options here:
    |
    | http://www.elasticsearch.org/guide/en/elasticsearch/client/php-api/current/_configuration.html
    */

    'config' => [
        'hosts'     => ['localhost:9200'],
        'retries'   => 1,
    ],

    /*
    |--------------------------------------------------------------------------
    | Default Index Name
    |--------------------------------------------------------------------------
    |
    | This is the index name that Elastiquent will use for all
    | Elastiquent models.
    */

    'default_index' => 'my_custom_index_name',

);


protected $mappingProperties = array(
   'title' => array(
        'type' => 'string',
        'analyzer' => 'standard'
    )
);

    Book::putMapping($ignoreConflicts = true);

    Book::deleteMapping();

    Book::rebuildMapping();

    Book::mappingExists();
    Book::getMapping();



return array(

    /*
    |--------------------------------------------------------------------------
    | Default Index Name
    |--------------------------------------------------------------------------
    |
    | This is the index name that Elastiquent will use for all
    | Elastiquent models.
    */

    'default_index' => 'my_custom_index_name',

);

function getTypeName()
{
    return 'custom_type_name';
}

    $typeExists = Book::typeExists();

    Book::addAllToIndex();

    $books = Book::where('id', '<', 200)->get();
    $books->addToIndex();

    $book = Book::find($id);
    $book->addToIndex();

    Book::reindex();

    $books = Book::search('Moby Dick');

    public static function searchByQuery($query = null, $aggregations = null, $sourceFields = null, $limit = null, $offset = null, $sort = null)

    $books = Book::searchByQuery(array('match' => array('title' => 'Moby Dick')));

    $books = Book::complexSearch(array(
        'body' => array(
            'query' => array(
                'match' => array(
                    'title' => 'Moby Dick'
                )
            )
        )
    ));

    $books = Book::searchByQuery(array('match' => array('title' => 'Moby Dick')));

    $books->totalHits();

    $books->shards();

    $books->maxScore();

    $books->timedOut();

    $books->took();

    $books->getAggregations();

    $book->isDocument();

    $book->documentScore();

    $all_books = Book::searchByQuery(array('match' => array('title' => 'Moby Dick')));
    $books = $all_books->chunk(10);

$client = new \Elasticsearch\Client();

$params = array(
    'index' => 'default',
    'type'  => 'books'
);

$params['body']['query']['match']['title'] = 'Moby Dick';

$collection = new \Elasticquent\ElasticquentResultCollection($client->search($params), new Book);


function getIndexDocumentData()
{
    return array(
        'id'      => $this->id,
        'title'   => $this->title,
        'custom'  => 'variable'
    );
}

class MyCollection extends \Illuminate\Database\Eloquent\Collection {

    use ElasticquentCollectionTrait;
}