public function actionSearch($title) { $query = new Query(); $result = $query->from('apps_zh')->match($title)->all(); var_dump($result); exit; $query = DictZhXs::find(); $result = $query->where($title)->limit(10)->all(); Yii::info($result); return $this->render('search', ['query' => $title, 'models' => $result]); }
public function search($q) { $query = new Query(); $rows = $query->from(array_values($this->indexs))->match($q)->showMeta(true)->search(); $models = []; /** @var \XSDocument $item */ foreach ($rows["hits"] as $item) { $models[] = $item["entity"]::findOne($item["id"]); } return $models; }
public function testQuery() { $query = new Query(); $query->from('yii2_test_article_index'); $provider = new ActiveDataProvider(['query' => $query, 'db' => $this->getConnection()]); $models = $provider->getModels(); $this->assertEquals(2, count($models)); $provider = new ActiveDataProvider(['query' => $query, 'db' => $this->getConnection(), 'pagination' => ['pageSize' => 1]]); $models = $provider->getModels(); $this->assertEquals(1, count($models)); }
/** * @depends testRun * * @see https://github.com/yiisoft/yii2/issues/4375 */ public function testRunOnDistributedIndex() { $connection = $this->getConnection(); $query = new Query(); $rows = $query->from('yii2_test_distributed')->match('about')->options(['cutoff' => 50, 'field_weights' => ['title' => 10, 'content' => 3]])->all($connection); $this->assertNotEmpty($rows); }
/** * @return int */ public function actionIle() { $query = new Query(); $rows = $query->from('lproduct')->match(new Expression(':match', ['match' => '@(description)' . \Yii::$app->sphinx->escapeMatchValue('база макияж под')]))->all(); print_r($rows); print_r(IledebeauteProduct::findOne(['id' => $rows[0]['id']])->toArray()); die; return $rows; }
/** * @depends testMatchSpecialCharValue */ public function testMatchComplex() { $connection = $this->getConnection(); $query = new Query(); $rows = $query->from('yii2_test_article_index')->match(new Expression(':match', ['match' => '@(content) ' . $connection->escapeMatchValue('about\\"')]))->all($connection); $this->assertNotEmpty($rows); }
public function testCount() { $connection = $this->getConnection(); $query = new Query(); $count = $query->from('yii2_test_article_index')->match('about')->count('*', $connection); $this->assertEquals(2, $count); }
public static function getLastTorrentsDataProvider($count = 5, $forceRefresh = false) { $cacheKey = 'browse_torrents'; $torrentsByTags = Yii::$app->cache->get($cacheKey); if (!$torrentsByTags) { $torrentsByTags = []; $torrentsIds = []; $tags = Category::$categoriesTags; foreach ($tags as $tag) { $query = new Query(); $rows = $query->from(static::indexName())->match(new Expression(':match', [':match' => '@tags ' . Yii::$app->sphinx->escapeMatchValue($tag)]))->where(['deleted' => 0])->orderBy(['weight()' => SORT_DESC, 'id' => SORT_DESC])->limit($count)->addOptions(['ranker' => 'SPH04', 'max_matches' => 5]); $torrents = array_map(function ($item) { return $item['id']; }, $rows->all()); $torrentsIds = array_merge($torrentsIds, $torrents); } if (empty($torrentsIds)) { Yii::log('Empty last torrents ids', CLogger::LEVEL_WARNING); } else { $query = (new Torrent())->find()->with('scrapes')->where(['id' => $torrentsIds]); foreach ($query->all() as $torrent) { $torrentsByTags[Category::getTorrentCategoryTag($torrent)][] = $torrent; } } Yii::$app->cache->set($cacheKey, $torrentsByTags, 600); } $result = array_map(function ($data) { return new ArrayDataProvider(['key' => 'id', 'allModels' => $data]); }, $torrentsByTags); return $result; }
/** * Выборка для случайного концерта * @return string */ function actionConcert() { if ($user = Yii::$app->getRequest()->getQueryParam('user')) { if (!$user) { return 'Доступ запрещн!'; } // $user = Yii::$app->getRequest()->getQueryParam('user'); if (Items::find()->where(['is_next' => 1])->one()) { $item = Items::find()->where(['is_next' => 1])->one(); $song_id = $item->id; } else { $song_id = 100; } $songs = []; //return $song_id; $songs1 = Items::find()->where("(source_id = 6 or source_id = 40) and id < {$song_id} and published = 1")->limit(5)->orderBy('id DESC')->all(); $songs2 = Items::find()->where("(source_id = 19 or source_id = 41) and id < {$song_id} and published = 1")->limit(5)->orderBy('id DESC')->all(); $songs3 = Items::find()->where("source_id = 38 and id < {$song_id} and published = 1")->limit(5)->orderBy('id DESC')->all(); $songs4 = Items::find()->where("(source_id = 29 or source_id = 326 or source_id = 526) and id < {$song_id} and published = 1")->limit(1)->orderBy('id DESC')->all(); $songs = array_merge($songs1, $songs2, $songs3, $songs4); shuffle($songs); //return var_dump($songs); //$choosen_songs = []; foreach ($songs as $song) { try { $items_records = []; $tags = explode(',', $song->tags); $rand_tag = $tags[mt_rand(0, count($tags) - 1)]; $this->rand_tag[] = $rand_tag; $query = new Query(); // $search_result = $query_search->from('siteSearch')->match($q)->all(); // поиск осуществляется по средством метода match с переданной поисковой фразой. $query_items_ids = $query->from('items')->match($rand_tag)->all(); //return var_dump($query_items_ids); foreach ($query_items_ids as $arr_item_rec) { foreach ($arr_item_rec as $id) { $item = Items::findOne((int) $id); if ($item->cat_id == 93 || $item->cat_id == 104 || $item->cat_id == 105 || $item->cat_id == 143 || $item->cat_id == 136) { $items_records[] = Items::findOne((int) $id); } } } //return var_dump($items_records[mt_rand(0,count($items_records)-1)]->text); if (empty($items_records)) { continue; } $rand_index = mt_rand(0, count($items_records) - 1); $song->phrase = $items_records[$rand_index]->text; if (isset($items_records[$rand_index + 1]) && $song->phrase != $song->phrase2) { $song->phrase2 = $items_records[$rand_index + 1]->text; } else { $song->phrase2 = $items_records[0]->text; } //return $song->phrase.'<br>'.$song->phrase2; //return var_dump($song); $this->choosen_songs[] = $song; } catch (\ErrorException $e) { return $e->getMessage(); } } //exit; //return var_dump($this->choosen_songs); return $this->renderPartial('concert', ['songs' => $this->choosen_songs, 'tags' => $this->rand_tag]); } }