1. Go to this page and download the library: Download spatie/searchindex 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/ */
spatie / searchindex example snippets
//$product is an object that implements the Searchable interface
SearchIndex::upsertToIndex($product);
SearchIndex::getResults('look for this');
namespace Spatie\SearchIndex;
interface Searchable {
/**
* Returns an array with properties which must be indexed
*
* @return array
*/
public function getSearchableBody();
/**
* Return the type of the searchable subject
*
* @return string
*/
public function getSearchableType();
/**
* Return the id of the searchable subject
*
* @return string
*/
public function getSearchableId();
class Product extends Eloquent implements Searchable
{
...
/**
* Returns an array with properties which must be indexed
*
* @return array
*/
public function getSearchableBody()
{
$searchableProperties = [
'name' => $this->name,
'brand' => $this->brand->name,
'category' => $this->category->name
];
return $searchableProperties;
}
/**
* Return the type of the searchable subject
*
* @return string
*/
public function getSearchableType()
{
return 'product';
}
/**
* Return the id of the searchable subject
*
* @return string
*/
public function getSearchableId()
{
return $this->id;
}
}
//$product is an object that implements the Searchable interface
SearchIndex::upsertToIndex($product);
//$product is an object that implements the Searchable interface
SearchIndex::upsertToIndex($product);
//$product is an object that implements the Searchable interface
SearchIndex::removeFromIndex($product);
SearchIndex::getClient(); // will return the Elasticsearch or Algolia client.
use Spatie\SearchIndex\Query\Algolia\SearchIndex();
$searchQuery = new SearchQuery();
$searchQuery->searchFor('my query')
->withFacet('facetName', 'facetValue');
//a searchQuery object may be passed to the getResults-function directly.
SearchIndex::getResults($searchQuery);