PHP code example of buiphong / elasticquent
1. Go to this page and download the library: Download buiphong/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/ */
buiphong / elasticquent example snippets
$books = Book::where('id', '<', 200)->get();
$books->addToIndex();
$books = Book::search('Moby Dick');
echo $books->totalHits();
$books = $books->filter(function ($book) {
return $book->hasISBN();
});
'providers' => [
...
Elasticquent\ElasticquentServiceProvider::class,
],
'aliases' => [
...
'Es' => Elasticquent\ElasticquentElasticsearchFacade::class,
],
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,
],
);
/**
* The elasticsearch settings.
*
* @var array
*/
protected $indexSettings = [
'analysis' => [
'char_filter' => [
'replace' => [
'type' => 'mapping',
'mappings' => [
'&=> and '
],
],
],
'filter' => [
'word_delimiter' => [
'type' => 'word_delimiter',
'split_on_numerics' => false,
'split_on_case_change' => true,
'generate_word_parts' => true,
'generate_number_parts' => true,
'catenate_all' => true,
'preserve_original' => true,
'catenate_numbers' => true,
]
],
'analyzer' => [
'default' => [
'type' => 'custom',
'char_filter' => [
'html_strip',
'replace',
],
'tokenizer' => 'whitespace',
'filter' => [
'lowercase',
'word_delimiter',
],
],
],
],
];
protected $mappingProperties = array(
'title' => array(
'type' => 'string',
'analyzer' => 'standard'
)
);
Book::putMapping($ignoreConflicts = true);
Book::deleteMapping();
Book::rebuildMapping();
Book::mappingExists();
Book::getMapping();
return array(
// Other configuration keys ...
/*
|--------------------------------------------------------------------------
| Default Index Name
|--------------------------------------------------------------------------
|
| This is the index name that Elastiquent will use for all
| Elastiquent models.
*/
'default_index' => 'my_custom_index_name',
);
function getIndexName()
{
return '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 = Book::hydrateElasticsearchResult($client->search($params));
function getIndexDocumentData()
{
return array(
'id' => $this->id,
'title' => $this->title,
'custom' => 'variable'
);
}
class MyCollection extends \Illuminate\Database\Eloquent\Collection
{
use ElasticquentCollectionTrait;
}
shell
$ php artisan vendor:publish --provider="Elasticquent\ElasticquentServiceProvider"