PHP code example of scalia / sphinxsearch

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

    

scalia / sphinxsearch example snippets


'providers' => array(
	'Scalia\SphinxSearch\SphinxSearchServiceProvider',
)

'aliases' => array(
	'SphinxSearch' => 'Scalia\SphinxSearch\SphinxSearchFacade',
)

php artisan config:publish scalia/sphinxsearch

return array (
	'host'    => '127.0.0.1',
	'port'    => 9312,
	'indexes' => array (
		'my_index_name' => array ( 'table' => 'my_keywords_table', 'column' => 'id' ),
	)
);

return array (
	'host'    => '127.0.0.1',
	'port'    => 9312,
	'indexes' => array (
		'my_index_name' => FALSE,
	)
);

$results = SphinxSearch::search('my query')->query();

$results = SphinxSearch::search('my query')->get();

$results = SphinxSearch::search('my query', 'index_name')
	->limit(30)
	->filter('attribute', array(1, 2))
	->range('int_attribute', 1, 10)
	->get();

$result = SphinxSearch::search('my query', 'index_name')
	->setFieldWeights(
		array(
			'partno'  => 10,
			'name'    => 8,
			'details' => 1
		)
	)
	->setMatchMode(\Sphinx\SphinxClient::SPH_MATCH_EXTENDED)
	->setSortMode(\Sphinx\SphinxClient::SPH_SORT_EXTENDED, "@weight DESC")
	->get(true);  //passing true causes get() to respect returned sort order

$radius = 1000; //in meters
$latitude = deg2rad(25.99);
$longitude = deg2rad(-80.35);
$result = SphinxSearch::search('my_query', 'index_name')
	->setSortMode(\Sphinx\SphinxClient::SPH_SORT_EXTENDED, '@geodist ASC')
	->setFilterFloatRange('@geodist', 0.0, $radius)
	->setGeoAnchor('lat', 'lng', $latitude, $longitude)
	->get(true);

return array (
	'host'    => '127.0.0.1',
	'port'    => 9312,
	'indexes' => array (
		'my_index_name' => array ( 'table' => 'my_keywords_table', 'column' => 'id', 'modelname' => 'Keyword' ),
	)
);

$results = SphinxSearch::search('monkeys')->with('arms', 'legs', 'otherLimbs')->get();

Route::get('/search', function ()
{
    $page = Input::get('page', 1);
    $search = Input::get('q', 'search string');
    $perPage = 15;  //number of results per page
    // use a cache so you dont have to keep querying sphinx for every page!
    $results = Cache::remember(Str::slug($search), 10, function () use($search)
    {
        return SphinxSearch::search($search)
        ->setMatchMode(\Sphinx\SphinxClient::SPH_MATCH_EXTENDED2)
        ->get();
    });
    if ($results) {
    	$totalItems = $results->count();
        $pages = array_chunk($results->all(), $perPage);

        $paginator = Paginator::make($pages[$page - 1], $totalItems, $perPage);
        return View::make('searchpage')->with('data', $paginator);
    }
    return View::make('notfound');
});

Route::get('/search', function ()
{
    $page = Input::get('page', 1);
    $search = Input::get('q', 'search string');
    $perPage = 15;  //number of results per page
    $items = null;

    $results = SphinxSearch::search($search)
        ->setMatchMode(\Sphinx\SphinxClient::SPH_MATCH_EXTENDED2)
        ->limit($perPage, ($page-1)* $perPage)
        ->get();

    if (!empty($results['total'])) {
        $items = Item::whereIn('id', array_keys($results['matches']))->get();
        $items = Paginator::make($items->all(), $results['total'], $perPage);

        $items->appends(['search' => $search]); //add search query string
    }

    if($error = SphinxSearch::getErrorMessage())
    {
        // 
    }
});

 echo $data->links()

return array (
	'host'    => '127.0.0.1',
	'port'    => 9312,
	'indexes' => array (
	    'name'    => array ('main', 'delta'),
	    'mapping' => array ( 'table' => 'properties', 'column' => 'id' ),
	)
);

SphinxSearch::search('lorem', 'main, delta')->get();

$search = SphinxSearch::search($term, 'articles');
$articles = $search->get();
$excerpt = $search->excerpt(current($articles)->content);

or

$search = SphinxSearch::search($term, 'articles');
dd($search->excerpts(array_pluck($articles, 'content')));