PHP code example of torann / elasticquent
1. Go to this page and download the library: Download torann/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/ */
torann / 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,
],
use Elasticquent\ElasticquentTrait;
class Book extends Eloquent
{
use ElasticquentTrait;
}
return [
/*
|--------------------------------------------------------------------------
| 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',
/*
|--------------------------------------------------------------------------
| Default Index Settings
|--------------------------------------------------------------------------
|
| This is the settings used when creating an Elasticsearch index.
|
| 'default_settings' => [
| 'number_of_shards' => 1,
| 'analysis' => [
| 'filter' => [
| 'autocomplete_filter' => [
| 'type' => 'edge_ngram',
| 'min_gram' => 1,
| 'max_gram' => 20,
| ],
| ],
| 'analyzer' => [
| 'autocomplete' => [
| 'type' => 'custom',
| 'tokenizer' => 'standard',
| 'filter' => [
| 'lowercase',
| 'autocomplete_filter',
| ],
| ],
| ],
| ],
| ],
*/
'default_settings' => null,
];
[
'default_settings' => [
'number_of_shards' => 1,
'analysis' => [
'filter' => [
'autocomplete_filter' => [
'type' => 'edge_ngram',
'min_gram' => 1,
'max_gram' => 20,
],
],
'analyzer' => [
'autocomplete' => [
'type' => 'custom',
'tokenizer' => 'standard',
'filter' => [
'lowercase',
'autocomplete_filter',
],
],
],
],
],
]
protected $mappingProperties = [
'title' => [
'type' => 'string',
'analyzer' => 'standard'
]
];
Book::putMapping($ignoreConflicts = true);
Book::deleteMapping();
Book::rebuildMapping();
Book::mappingExists();
Book::getMapping();
return [
// 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([
'match' => ['title' => 'Moby Dick']
]);
$books = Book::complexSearch([
'body' => [
'query' => [
'match' => [
'title' => 'Moby Dick'
]
]
]
]);
$books = Book::searchByQuery(['match' => ['title' => 'Moby Dick']]);
$books->totalHits();
$books->shards();
$books->maxScore();
$books->timedOut();
$books->took();
$books->getAggregations();
$book->isDocument();
$book->documentScore();
$all_books = Book::searchByQuery(['match' => ['title' => 'Moby Dick']]);
$books = $all_books->chunk(10);
$client = new \Elasticsearch\Client();
$params = [
'index' => 'default',
'type' => 'books'
];
$params['body']['query']['match']['title'] = 'Moby Dick';
$collection = Book::hydrateElasticsearchResult($client->search($params));
function getIndexDocumentData()
{
return [
'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"