コード例 #1
0
 public function testConfigFile()
 {
     $sphinx = FSphinxClient::fromConfig(__DIR__ . '/Fixtures/config.sample.php');
     $this->assertInstanceOf('\\FSphinx\\FSphinxClient', $sphinx);
     $results1 = $this->cl->query('drama (@actor "Morgan Freeman") (@director "Clint Eastwood")');
     if ($this->cl->isConnectError()) {
         $this->markTestSkipped('Could not connect to Sphinx.');
     }
     if (!$results1 || !isset($results1['matches'])) {
         $this->markTestSkipped('No results returned from Sphinx.');
     }
     $results2 = $sphinx->query('drama (@actor "Morgan Freeman") (@director "Clint Eastwood")');
     $ids1 = $ids2 = array();
     foreach ($this->cl->facets as $index => $facet) {
         $ids1[$index] = array();
         foreach ($facet as $match) {
             $ids1[$index][] = $match['@term'];
         }
     }
     foreach ($sphinx->facets as $index => $facet) {
         $ids2[$index] = array();
         foreach ($facet as $match) {
             $ids2[$index][] = $match['@term'];
         }
     }
     $this->assertEquals($ids1, $ids2);
 }
コード例 #2
0
<?php

/**
 * This file creates and returns an FSphinx client with attached query parser, facets and data sources.
 * Use it in the following manner:
 *
 * $cl = FSphinxClient::fromConfig($filepath);
 */
use FSphinx\FSphinxClient;
use FSphinx\MultiFieldQuery;
use FSphinx\Facet;
$cl = new FSphinxClient();
$cl->setServer(defined('SPHINX_HOST') ? SPHINX_HOST : '127.0.0.1', defined('SPHINX_PORT') ? SPHINX_PORT : 9312);
$cl->setDefaultIndex('items');
$cl->setMatchMode(FSphinxClient::SPH_MATCH_EXTENDED2);
$cl->setSortMode(FSphinxClient::SPH_SORT_EXPR, '@weight * user_rating_attr * nb_votes_attr * year_attr / 100000');
$cl->setFieldWeights(array('title' => 30));
$factor = new Facet('actor');
$factor->attachDataSource($cl, array('name' => 'actor_terms'));
$fdirector = new Facet('director');
$fdirector->attachDataSource($fdirector, array('name' => 'director_terms_attr'));
$cl->attachFacets(new Facet('year'), new Facet('genre'), new Facet('keyword', array('attr' => 'plot_keyword_attr')), $fdirector, $factor);
$group_func = 'sum(if (runtime_attr > 45, if (nb_votes_attr > 1000, if (nb_votes_attr < 10000, nb_votes_attr * user_rating_attr, 10000 * user_rating_attr), 1000 * user_rating_attr), 300 * user_rating_attr))';
foreach ($cl->facets as $facet) {
    $facet->setGroupFunc($group_func);
    $facet->setOrderBy('@term', 'asc');
    $facet->setMaxNumValues(5);
}
$cl->attachQueryParser(new MultiFieldQuery(array('genre' => 'genres', 'keyword' => 'plot_keywords', 'director' => 'directors', 'actor' => 'actors')));
return $cl;