Coder Social home page Coder Social logo

sphinxsearch's Introduction

Stories in Ready

Sphinx Search

License Latest Stable Version Total Downloads Monthly Downloads

Sphinx Search is a package for Laravel 4 which queries Sphinxsearch and integrates with Eloquent.

Installation

Add scalia/sphinxsearch to composer.json.

"scalia/sphinxsearch": "dev-master"

Run composer update to pull down the latest version of Sphinx Search.

Now open up app/config/app.php and add the service provider to your providers array.

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

Now add the alias.

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

Configuration

To use Sphinx Search, you need to configure your indexes and what model it should query. To do so, publish the configuration into your app.

php artisan config:publish scalia/sphinxsearch

This will create the file app/config/packages/scalia/sphinxsearch/config.php. Modify as needed the host and port, and configure the indexes, binding them to a table and id column.

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

Or disable the model querying to just get a list of result id's.

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

Usage

Basic query (raw sphinx results)

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

Basic query (with Eloquent)

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

Query another Sphinx index with limit and filters.

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

Query with match and sort type specified.

$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

Query and sort with geo-distant searching.

$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);

Integration with Eloquent

This package integrates well with Eloquent. You can change index configuration with modelname to get Eloquent's Collection (Illuminate\Database\Eloquent\Collection) as a result of SphinxSearch::search.

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

Eager loading with Eloquent is the same an one would expect:

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

More on eager loading: http://laravel.com/docs/eloquent#eager-loading

Paging results in Laravel 4 (with caching)

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) {
        $pages = array_chunk($results, $perPage);

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

And, in your view after you finish displaying rows,

<?php echo $data->links()?>

Searching through multiple Sphinx indexes (main/delta)

It is a common strategy to utilize the main+delta scheme (www.sphinxconsultant.com/sphinx-search-delta-indexing/). When using deltas, it is often necessary to query on multiple indexes simultaneously. In order to achieve this using SphinxSearch, modify your config file to include the "name" and "mapping" keys like so:

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

You can also pass in multiple indexes (separated by comma or space) to your search like so (if the "mapping" key is not specified in the config, search retrieves ids):

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

sphinxsearch's People

Contributors

anpez avatar brianmcdo avatar splinter89 avatar levi730 avatar alpha0010 avatar smitp avatar waffle-with-pears avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.