定制 foolz/sphinxql-query-builder 二次开发

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

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

foolz/sphinxql-query-builder

Composer 安装命令:

composer require foolz/sphinxql-query-builder

包简介

A PHP query builder for SphinxQL and ManticoreQL with MySQLi and PDO drivers.

README 文档

README

CI Documentation Latest Stable Version Total Downloads

A fluent PHP query builder for SphinxQL and ManticoreQL.

It supports:

  • SELECT, INSERT, REPLACE, UPDATE, DELETE
  • MATCH() building (including MatchBuilder)
  • FACET queries
  • batched/multi-queries
  • helper commands (SHOW, CALL, maintenance operations)
  • percolate workflows for Manticore
  • both mysqli and PDO drivers

Installation

composer require foolz/sphinxql-query-builder

Requirements:

  • PHP 8.2+
  • mysqli or pdo_mysql
  • Running Sphinx Search or Manticore Search server

Quick Start

<?php

use Foolz\SphinxQL\Drivers\Mysqli\Connection;
use Foolz\SphinxQL\SphinxQL;

$conn = new Connection();
$conn->setParams([
    'host' => '127.0.0.1',
    'port' => 9306,
]);

$rows = (new SphinxQL($conn))
    ->select('id', 'gid', 'title')
    ->from('rt')
    ->match('title', 'vacation')
    ->where('gid', '>', 300)
    ->orderBy('id', 'DESC')
    ->limit(5)
    ->execute()
    ->getStored();

Connection Setup

mysqli driver

<?php

use Foolz\SphinxQL\Drivers\Mysqli\Connection;

$conn = new Connection();
$conn->setParams([
    'host' => '127.0.0.1',
    'port' => 9306,
    'options' => [
        MYSQLI_OPT_CONNECT_TIMEOUT => 2,
    ],
]);

PDO driver

<?php

use Foolz\SphinxQL\Drivers\Pdo\Connection;

$conn = new Connection();
$conn->setParams([
    'host' => '127.0.0.1',
    'port' => 9306,
    'charset' => 'utf8',
]);

Query Builder Examples

Compile SQL before executing

<?php

use Foolz\SphinxQL\SphinxQL;

$sql = (new SphinxQL($conn))
    ->select('a.id')
    ->from('rt a')
    ->leftJoin('rt b', 'a.id', '=', 'b.id')
    ->where('a.id', '>', 1)
    ->compile()
    ->getCompiled();

// SELECT a.id FROM rt a LEFT JOIN rt b ON a.id = b.id WHERE a.id > 1

Insert rows

<?php

(new SphinxQL($conn))
    ->insert()
    ->into('rt')
    ->columns('id', 'gid', 'title', 'content')
    ->values(10, 9003, 'modifying the same line again', 'because i am that lazy')
    ->values(11, 201, 'replacing value by value', 'i have no idea who would use this directly')
    ->execute();

Replace rows

<?php

(new SphinxQL($conn))
    ->replace()
    ->into('rt')
    ->set([
        'id' => 10,
        'gid' => 9002,
        'title' => 'modified',
        'content' => 'this field was modified with replace',
    ])
    ->execute();

Update rows (including MVA)

<?php

(new SphinxQL($conn))
    ->update('rt')
    ->where('id', '=', 15)
    ->value('tags', [111, 222])
    ->execute();

Delete rows

<?php

$affected = (new SphinxQL($conn))
    ->delete()
    ->from('rt')
    ->where('id', 'IN', [11, 12, 13])
    ->match('content', 'content')
    ->execute()
    ->getStored();

Grouped boolean filters

<?php

$sql = (new SphinxQL($conn))
    ->select()
    ->from('rt')
    ->where('gid', 200)
    ->orWhereOpen()
    ->where('gid', 304)
    ->where('id', '>', 12)
    ->whereClose()
    ->compile()
    ->getCompiled();

// SELECT * FROM rt WHERE gid = 200 OR ( gid = 304 AND id > 12 )

MATCH with builder callback

<?php

$rows = (new SphinxQL($conn))
    ->select()
    ->from('rt')
    ->match(function ($m) {
        $m->field('content')
          ->match('directly')
          ->orMatch('lazy');
    })
    ->execute()
    ->getStored();

ORDER BY KNN

<?php

$sql = (new SphinxQL($conn))
    ->select('id')
    ->from('rt')
    ->orderByKnn('embeddings', 5, [0.1, 0.2, 0.3])
    ->compile()
    ->getCompiled();

// SELECT id FROM rt ORDER BY KNN(embeddings, 5, [0.1,0.2,0.3]) ASC

Subqueries

<?php

$subquery = (new SphinxQL($conn))
    ->select('id')
    ->from('rt')
    ->orderBy('id', 'DESC');

$sql = (new SphinxQL($conn))
    ->select()
    ->from($subquery)
    ->orderBy('id', 'ASC')
    ->compile()
    ->getCompiled();

// SELECT * FROM (SELECT id FROM rt ORDER BY id DESC) ORDER BY id ASC

Helper API Example

<?php

use Foolz\SphinxQL\Helper;

$helper = new Helper($conn);

$tables = $helper->showTables()->execute()->getStored();
$variables = Helper::pairsToAssoc($helper->showVariables()->execute()->getStored());
$keywords = $helper->callKeywords('test case', 'rt', 1)->execute()->getStored();

Compile examples from tests:

  • $helper->showTables()->compile()->getCompiled() -> SHOW TABLES
  • $helper->showTables('rt')->compile()->getCompiled() -> SHOW TABLES LIKE 'rt'
  • $helper->showTableStatus()->compile()->getCompiled() -> SHOW TABLE STATUS
  • $helper->showTableStatus('rt')->compile()->getCompiled() -> SHOW TABLE rt STATUS
  • $helper->callSuggest('teh', 'rt', ['limit' => 5])->compile()->getCompiled() -> CALL SUGGEST('teh', 'rt', 5 AS limit)

FACET Example

<?php

use Foolz\SphinxQL\Facet;
use Foolz\SphinxQL\SphinxQL;

$facet = (new Facet($conn))
    ->facet(['gid'])
    ->orderBy('gid', 'ASC');

$batchRows = (new SphinxQL($conn))
    ->select()
    ->from('rt')
    ->facet($facet)
    ->executeBatch()
    ->getStored();

// $batchRows[0] is SELECT data
// $batchRows[1] is FACET aggregation data

Multi Query / Batch Example

<?php

use Foolz\SphinxQL\Helper;
use Foolz\SphinxQL\SphinxQL;

$batch = (new SphinxQL($conn))
    ->select()
    ->from('rt')
    ->where('gid', 9003)
    ->enqueue()
    ->select()
    ->from('rt')
    ->where('gid', 201)
    ->enqueue((new Helper($conn))->showMeta())
    ->executeBatch();

$all = $batch->getStored();

Percolate Example (Manticore)

<?php

use Foolz\SphinxQL\Percolate;

(new Percolate($conn))
    ->insert('@subject orange')
    ->into('pq')
    ->tags(['tag2', 'tag3'])
    ->filter('price>3')
    ->execute();

$matches = (new Percolate($conn))
    ->callPQ()
    ->from('pq')
    ->documents(['{"subject":"document about orange"}'])
    ->options([
        Percolate::OPTION_QUERY => 1,
        Percolate::OPTION_DOCS => 1,
    ])
    ->execute()
    ->fetchAllAssoc();

Capability Checks

<?php

use Foolz\SphinxQL\Helper;

$helper = new Helper($conn);
$caps = $helper->getCapabilities();

if ($helper->supports('call_autocomplete')) {
    $rows = $helper->callAutocomplete('te', 'rt', ['fuzzy' => 1])->execute()->getStored();
}

Result Objects

execute() returns ResultSetInterface:

  • getStored()
  • fetchAllAssoc()
  • fetchAllNum()
  • fetchAssoc()
  • fetchNum()
  • getAffectedRows()

executeBatch() returns MultiResultSetInterface:

  • getStored()
  • getNext()

Documentation Map

Running Tests

./scripts/run-tests-docker.sh

This runs the repository test matrix (mysqli + pdo) in Docker.

Contributing

Pull requests are welcome. Please include tests for behavior changes and keep docs in sync with API updates.

foolz/sphinxql-query-builder 适用场景与选型建议

foolz/sphinxql-query-builder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.23M 次下载、GitHub Stars 达 323, 最近一次更新时间为 2012 年 12 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 foolz/sphinxql-query-builder 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 2.23M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 331
  • 点击次数: 25
  • 依赖项目数: 37
  • 推荐数: 1

GitHub 信息

  • Stars: 323
  • Watchers: 26
  • Forks: 99
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2012-12-05