定制 scalia/sphinxsearch 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

scalia/sphinxsearch

最新稳定版本:0.2

Composer 安装命令:

composer require scalia/sphinxsearch

包简介

Laravel package to query Sphinxsearch

README 文档

README

Build Status License Latest Stable Version Total Downloads Monthly Downloads Stories in Ready Stars Forks

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) {
    	$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');
});

Paging results in Laravel 4 (without caching)

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())
    {
        // 
    }
});

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

Retrieve search result excerpts using Sphinx

It is nifty to display excerpts with keywords highlighted in search result. Sphinx supports this feature natively. http://sphinxsearch.com/docs/archives/2.0.3/api-func-buildexcerpts.html

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

scalia/sphinxsearch 适用场景与选型建议

scalia/sphinxsearch 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 53.4k 次下载、GitHub Stars 达 160, 最近一次更新时间为 2013 年 08 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「sphinx」 「sphinxsearch」 「Laravel 4」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 scalia/sphinxsearch 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 scalia/sphinxsearch 我们能提供哪些服务?
定制开发 / 二次开发

基于 scalia/sphinxsearch 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 53.4k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 164
  • 点击次数: 15
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 160
  • Watchers: 14
  • Forks: 54
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2013-08-09