protected function buildQuery(array $classes) { $searchQuery = new Search($this->client); $searchQuery->setOption(Search::OPTION_VERSION, true); foreach ($classes as $class) { if ($class->index) { $indexObject = $this->getIndex($class->index); $searchQuery->addIndex($indexObject); if ($class->type) { $searchQuery->addType($indexObject->getType($class->type)); } } } return $searchQuery; }
/** * @group functional */ public function testQueryCacheOption() { $client = $this->_getClient(); $index = $client->getIndex('zero'); $index->create(array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)), true); $type = $index->getType('zeroType'); $type->addDocuments(array(new Document(1, array('id' => 1, 'username' => 'farrelley')), new Document(2, array('id' => 2, 'username' => 'bunny')))); $index->refresh(); $aggregation = new Aggregation\Terms('username'); $aggregation->setField('username'); $query = new Query(); $query->addAggregation($aggregation); $search = new Search($client); $search->addIndex($index); $search->setQuery($query); $search->setOption(Search::OPTION_SEARCH_TYPE, Search::OPTION_SEARCH_TYPE_COUNT); $search->setOption(Search::OPTION_QUERY_CACHE, true); // before search query cache should be empty $statsData = $index->getStats()->getData(); $queryCache = $statsData['_all']['primaries']['query_cache']; $this->assertEquals(0, $queryCache['memory_size_in_bytes']); $this->assertEquals(0, $queryCache['evictions']); $this->assertEquals(0, $queryCache['hit_count']); $this->assertEquals(0, $queryCache['miss_count']); // first search should result in cache miss and save data to cache $search->search(); $index->getStats()->refresh(); $statsData = $index->getStats()->getData(); $queryCache = $statsData['_all']['primaries']['query_cache']; $this->assertNotEquals(0, $queryCache['memory_size_in_bytes']); $this->assertEquals(0, $queryCache['evictions']); $this->assertEquals(0, $queryCache['hit_count']); $this->assertEquals(1, $queryCache['miss_count']); // next search should result in cache hit $search->search(); $index->getStats()->refresh(); $statsData = $index->getStats()->getData(); $queryCache = $statsData['_all']['primaries']['query_cache']; $this->assertNotEquals(0, $queryCache['memory_size_in_bytes']); $this->assertEquals(0, $queryCache['evictions']); $this->assertEquals(1, $queryCache['hit_count']); $this->assertEquals(1, $queryCache['miss_count']); }
/** * Build a Search that will count all pages that link to $titles. * @param string $titles title in prefixedDBKey form * @return Search that counts all pages that link to $titles */ private function buildCount($titles) { $filter = new Terms('outgoing_link', $titles); $filter->setCached(false); // We're not going to be redoing this any time soon. $type = $this->connection->getPageType(wfWikiId()); $search = new Search($type->getIndex()->getClient()); $search->addIndex($type->getIndex()); $search->addType($type); $search->setOption(Search::OPTION_SEARCH_TYPE, Search::OPTION_SEARCH_TYPE_COUNT); $matchAll = new MatchAll(); $search->setQuery(new Filtered($matchAll, $filter)); $search->getQuery()->addParam('stats', 'link_count'); return $search; }