/**
  * Adds a query to a multi-query batch
  *
  * @param string $query   Search query
  * @param array  $indexes Index list to perform the search
  *
  * @throws EmptyIndexException If $indexes is empty
  */
 public function addQuery($query, array $indexes)
 {
     if (empty($indexes)) {
         throw new EmptyIndexException('Try to search with empty indexes');
     }
     $this->sphinx->addQuery($query, implode(' ', $indexes));
 }
 /**
  * Display a listing of the resource.
  *
  * @return Response
  */
 public function index()
 {
     $search_query = \Input::get('q');
     $matches_ids = [];
     if ($search_query) {
         $sphinx = \Sphinx\SphinxClient::create();
         $sphinx->setServer('127.0.0.1', 3312);
         $sphinx->setMatchMode(\Sphinx\SphinxClient::SPH_MATCH_ANY);
         $sphinx->setSortMode(\Sphinx\SphinxClient::SPH_SORT_RELEVANCE);
         $sphinx->setFieldWeights(array('name' => 20, 'description' => 10));
         $sphinx->addQuery($search_query, '*');
         $res = $sphinx->runQueries();
         if (!$res or !array_key_exists('matches', $res[0])) {
             return [];
         }
         $matches_ids = array_keys($res[0]['matches']);
     }
     $products_in_purchases_arr = \DB::table('products_in_purchase')->select('*')->take(10);
     if (!empty($matches_ids)) {
         $products_in_purchases_arr = $products_in_purchases_arr->whereIn('product_id', $matches_ids);
     }
     $products_in_purchases_arr = $products_in_purchases_arr->get();
     if (empty($products_in_purchases_arr)) {
         return [];
     }
     $products_ins_arr = [];
     foreach ($products_in_purchases_arr as $product_in_purchase_mix) {
         $product = \App\BusinessLogic\Models\Product::find($product_in_purchase_mix->product_id);
         $purchase = \App\BusinessLogic\Models\Purchase::find($product_in_purchase_mix->purchase_id);
         $products_ins_arr[$product_in_purchase_mix->purchase_id . '_' . $product_in_purchase_mix->product_id] = json_decode(new \App\BusinessLogic\ProductInPurchase($product, $purchase));
     }
     return $products_ins_arr;
 }
 public function getProductsInPurchases()
 {
     $filter = \Input::get('filter', []);
     $search_query = '';
     $category_id = 0;
     if (array_key_exists('query', $filter)) {
         $search_query = trim($filter['query']);
     }
     if (array_key_exists('category', $filter)) {
         $category_id = intval($filter['category']);
     }
     $matches_ids = [];
     if ($category_id) {
         $products_ids_arr = \App\Helpers\ProjectHelper::getProductsIdsArrByTagId($category_id);
         $matches_ids = array_merge($matches_ids, $products_ids_arr);
     }
     if ($search_query) {
         $sphinx = \Sphinx\SphinxClient::create();
         $sphinx->setServer('127.0.0.1', 3312);
         $sphinx->setMatchMode(\Sphinx\SphinxClient::SPH_MATCH_ANY);
         $sphinx->setSortMode(\Sphinx\SphinxClient::SPH_SORT_RELEVANCE);
         $sphinx->setFieldWeights(array('name' => 20, 'description' => 10));
         $sphinx->addQuery($search_query, '*');
         $res = $sphinx->runQueries();
         if (!$res or !array_key_exists('matches', $res[0])) {
             return [];
         }
         $matches_ids = array_merge($matches_ids, array_keys($res[0]['matches']));
     }
     $products_in_purchases_query = \DB::table('products_in_purchase')->select('*');
     if (!empty($matches_ids)) {
         $products_in_purchases_query = $products_in_purchases_query->whereIn('product_id', $matches_ids);
     }
     $products_in_purchases_arr = $products_in_purchases_query->take(12)->get();
     if (empty($products_in_purchases_arr)) {
         return [];
     }
     $products_arr = [];
     foreach ($products_in_purchases_arr as $product_in_purchase_mix) {
         $product = \App\BusinessLogic\Models\Product::find($product_in_purchase_mix->product_id);
         $purchase = \App\BusinessLogic\Models\Purchase::find($product_in_purchase_mix->purchase_id);
         $products_arr[] = new ProductInPurchase($product, $purchase);
     }
     return $products_arr;
 }
Example #4
0
 public function testStatus()
 {
     $sphinx = new SphinxClient();
     $status = $sphinx->status();
     $this->assertGreaterThan(0, count($status));
 }
 /**
  * Wrapper function for Sphinx batch query execution.
  *
  * @return array Sphinx result array.
  */
 public function runQueries()
 {
     $results = parent::runQueries();
     return $results;
 }
 /**
  * {@inheritdoc}
  */
 public function fixUint($value)
 {
     return $this->proxy->fixUint($value);
 }