public function testGeoPoint() { $client = $this->_getClient(); $index = $client->getIndex('test'); $index->create(array(), true); $type = $index->getType('test'); // Set mapping $type->setMapping(array('location' => array('type' => 'geo_point'))); // Add doc 1 $doc1 = new Document(1, array('name' => 'ruflin')); $doc1->addGeoPoint('location', 17, 19); $type->addDocument($doc1); // Add doc 2 $doc2 = new Document(2, array('name' => 'ruflin')); $doc2->addGeoPoint('location', 30, 40); $type->addDocument($doc2); $index->refresh(); // Only one point should be in polygon $query = new Query(); $points = array(array(16, 16), array(16, 20), array(20, 20), array(20, 16), array(16, 16)); $geoFilter = new GeoPolygon('location', $points); $query = new Query(new MatchAll()); $query->setFilter($geoFilter); $this->assertEquals(1, $type->search($query)->count()); // Both points should be inside $query = new Query(); $points = array(array(16, 16), array(16, 40), array(40, 40), array(40, 16), array(16, 16)); $geoFilter = new GeoPolygon('location', $points); $query = new Query(new MatchAll()); $query->setFilter($geoFilter); $this->assertEquals(2, $type->search($query)->count()); }
public function testGeoPoint() { $client = $this->_getClient(); $index = $client->getIndex('test'); $index->create(array(), true); $type = $index->getType('test'); // Set mapping $type->setMapping(array('point' => array('type' => 'geo_point'))); // Add doc 1 $doc1 = new Document(1, array('name' => 'ruflin')); $doc1->addGeoPoint('point', 17, 19); $type->addDocument($doc1); // Add doc 2 $doc2 = new Document(2, array('name' => 'ruflin')); $doc2->addGeoPoint('point', 30, 40); $type->addDocument($doc2); $index->optimize(); $index->refresh(); // Only one point should be in radius $query = new Query(); $geoFilter = new GeoDistance('point', array('lat' => 30, 'lon' => 40), '1km'); $query = new Query(new MatchAll()); $query->setFilter($geoFilter); $this->assertEquals(1, $type->search($query)->count()); // Both points should be inside $query = new Query(); $geoFilter = new GeoDistance('point', array('lat' => 30, 'lon' => 40), '40000km'); $query = new Query(new MatchAll()); $query->setFilter($geoFilter); $index->refresh(); $this->assertEquals(2, $type->search($query)->count()); }
/** * @group unit */ public function testSetFilterWithLegacyFilterDeprecated() { $this->hideDeprecated(); $existsFilter = new Exists('test'); $this->showDeprecated(); $query = new Query(); $errorsCollector = $this->startCollectErrors(); $query->setFilter($existsFilter); $this->finishCollectErrors(); $errorsCollector->assertOnlyDeprecatedErrors(array('Deprecated: Elastica\\Query::setFilter() passing filter as AbstractFilter is deprecated. Pass instance of AbstractQuery instead.', 'Deprecated: Elastica\\Query::setFilter() is deprecated and will be removed in further Elastica releases. Use Elastica\\Query::setPostFilter() instead.', 'Deprecated: Elastica\\Query::setPostFilter() passing filter as AbstractFilter is deprecated. Pass instance of AbstractQuery instead.')); }
/** * @depends testAddDocument * @dataProvider providerTransport */ public function testRandomRead(array $config, $transport) { $type = $this->getType($config); $type->search('test'); $times = array(); for ($i = 0; $i < $this->_max; $i++) { $test = rand(1, $this->_max); $query = new Query(); $query->setQuery(new MatchAllQuery()); $query->setFilter(new TermFilter(array('test' => $test))); $result = $type->search($query); $times[] = $result->getResponse()->getQueryTime(); } self::logResults('random read', $transport, $times); }
public function testBoolFilter() { $index = $this->_createIndex('bool_filter_test'); $type = $index->getType('book'); //index some test data $type->addDocument(new \Elastica\Document(1, array('author' => 'Michael Shermer', 'title' => 'The Believing Brain', 'publisher' => 'Robinson'))); $type->addDocument(new \Elastica\Document(2, array('author' => 'Jared Diamond', 'title' => 'Guns, Germs and Steel', 'publisher' => 'Vintage'))); $type->addDocument(new \Elastica\Document(3, array('author' => 'Jared Diamond', 'title' => 'Collapse', 'publisher' => 'Penguin'))); $type->addDocument(new \Elastica\Document(4, array('author' => 'Richard Dawkins', 'title' => 'The Selfish Gene', 'publisher' => 'OUP Oxford'))); $type->addDocument(new \Elastica\Document(5, array('author' => 'Anthony Burges', 'title' => 'A Clockwork Orange', 'publisher' => 'Penguin'))); $index->refresh(); //use the terms lookup feature to query for some data //build query //must // should // author = jared // author = richard // must_not // publisher = penguin //construct the query $query = new Query(); $mainBoolFilter = new Bool(); $shouldFilter = new Bool(); $authorFilter1 = new Term(); $authorFilter1->setTerm('author', 'jared'); $authorFilter2 = new Term(); $authorFilter2->setTerm('author', 'richard'); $shouldFilter->addShould(array($authorFilter1, $authorFilter2)); $mustNotFilter = new Bool(); $publisherFilter = new Term(); $publisherFilter->setTerm('publisher', 'penguin'); $mustNotFilter->addMustNot($publisherFilter); $mainBoolFilter->addMust(array($shouldFilter, $mustNotFilter)); $query->setFilter($mainBoolFilter); //execute the query $results = $index->search($query); //check the number of results $this->assertEquals($results->count(), 2, 'Bool filter with child Bool filters: number of results check'); //count compare the id's $ids = array(); /** @var \Elastica\Result $result **/ foreach ($results as $result) { $ids[] = $result->getId(); } $this->assertEquals($ids, array("2", "4"), 'Bool filter with child Bool filters: result ID check'); $index->delete(); }
public function testIndicesFilter() { $filter = new Indices(new BoolNot(new Term(array("color" => "blue"))), array($this->_index1->getName())); $filter->setNoMatchFilter(new BoolNot(new Term(array("color" => "yellow")))); $query = new Query(); $query->setFilter($filter); // search over the alias $index = $this->_getClient()->getIndex("indices_filter"); $results = $index->search($query); // ensure that the proper docs have been filtered out for each index $this->assertEquals(5, $results->count()); foreach ($results->getResults() as $result) { $data = $result->getData(); $color = $data["color"]; if ($result->getIndex() == $this->_index1->getName()) { $this->assertNotEquals("blue", $color); } else { $this->assertNotEquals("yellow", $color); } } }
/** * @param string $term text to search * @return \Status */ public function searchText($term) { $term = trim($term); // No searching for nothing! That takes forever! if (!$term) { return null; } $query = new Query(); $offset = min($this->offset, static::MAX_OFFSET); if ($offset) { $query->setFrom($offset); } if ($this->limit) { $query->setSize($this->limit); } $filter = new Bool(); // filters if ($this->namespaces) { $filter->addMust(new Terms('namespace', $this->namespaces)); } if ($this->pageIds) { $filter->addMust(new Terms('pageid', $this->pageIds)); } if ($this->moderationStates) { $filter->addMust(new Terms('revisions.moderation_state', $this->moderationStates)); } // only apply filters if there are any if ($filter->toArray()) { $query->setFilter($filter); } $sortArgs = $this->getSortArgs(); if (isset($sortArgs[$this->sort]) && $sortArgs[$this->sort]) { $query->setSort($sortArgs[$this->sort]); } // @todo: interwiki stuff? (see \CirrusSearch) $searcher = new Searcher($query, false, $this->type); return $searcher->searchText($term); }
public function findOneBy(ClassMetadata $class, $field, $value) { $query = new Query(); $query->setVersion(true); $query->setSize(1); $filter = new Term(array($field => $value)); $query->setFilter($filter); $results = $this->search($query, array($class)); if (!$results->count()) { throw new NoResultException(); } return $results[0]; }
/** * Search children. * * @param string $type Post type * @param int $parent Parent ID to get all children * @param string $order Order way * @return array $search Combine of all results, total and aggregations * * @since 3.0.0 */ public function searchChildren($type, $parent, $order = 'desc') { //Check page if (is_search()) { return; } //Return array $return = array('parent' => $parent, 'total' => 0, 'results' => array()); //Check request if (empty($parent)) { return $return; } //Get query vars $results = array(); $types = array(); //Get Elasticsearch datas $index = $this->getIndex(); //Check index if (null === $index || empty($index)) { return $return; } //Create the actual search object with some data. $es_query = new Query(); //Define term $es_term = new Term(); $es_term->setTerm($type . '.parent', $parent); //Filter 'And' $es_filter = new Bool(); $es_filter->addMust($es_term); //Add filter to the search object $es_query->setFilter($es_filter); //Add sort $es_query->setSort(array($type . '.date' => array('order' => $order))); //Search! $es_resultset = $index->search($es_query); //Retrieve data $es_results = $es_resultset->getResults(); //Check results if (null == $es_results || empty($es_results)) { return $return; } //Iterate to retrieve all IDs foreach ($es_results as $res) { $typ = $res->getType(); //Save type $types[$typ] = $typ; //Save datas $results[$typ][] = array('id' => $res->getId(), 'score' => $res->getScore(), 'source' => $res->getSource()); } //Get total $total = $es_resultset->getTotalHits(); //Return everything $return = array('parent' => $parent, 'total' => $total, 'results' => $results); return $return; }
public function testMoreLikeThisApi() { $client = new Client(array('persistent' => false)); $index = $client->getIndex('elastica_test'); $index->create(array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)), true); $type = new Type($index, 'mlt_test'); $type->addDocument(new Document(1, array('visible' => true, 'name' => 'bruce wayne batman'))); $type->addDocument(new Document(2, array('visible' => true, 'name' => 'bruce wayne'))); $type->addDocument(new Document(3, array('visible' => false, 'name' => 'bruce wayne'))); $type->addDocument(new Document(4, array('visible' => true, 'name' => 'batman'))); $type->addDocument(new Document(5, array('visible' => false, 'name' => 'batman'))); $type->addDocument(new Document(6, array('visible' => true, 'name' => 'superman'))); $type->addDocument(new Document(7, array('visible' => true, 'name' => 'spiderman'))); $index->refresh(); $document = $type->getDocument(1); // Return all similar $resultSet = $type->moreLikeThis($document, array('min_term_freq' => '1', 'min_doc_freq' => '1')); $this->assertEquals(4, $resultSet->count()); // Return just the visible similar $query = new Query(); $filterTerm = new Term(); $filterTerm->setTerm('visible', true); $query->setFilter($filterTerm); $resultSet = $type->moreLikeThis($document, array('min_term_freq' => '1', 'min_doc_freq' => '1'), $query); $this->assertEquals(2, $resultSet->count()); }
public function Addons() { $list = Addon::get(); $search = $this->request->getVar('search'); $type = $this->request->getVar('type'); $compat = $this->request->getVar('compatibility'); $tags = $this->request->getVar('tags'); $sort = $this->request->getVar('sort'); $view = $this->request->getVar('view'); if (!$view) { $view = 'list'; } if (!in_array($sort, array('name', 'downloads', 'newest'))) { $sort = null; } // Proxy out a search to elastic if any parameters are set. if ($search || $type || $compat || $tags) { $filter = new BoolAnd(); $query = new Query(); $query->setSize(count($list)); if ($search) { $match = new Match(); $match->setField('_all', $search); $query->setQuery($match); } if ($type) { $filter->addFilter(new Term(array('type' => $type))); } if ($compat) { $filter->addFilter(new Terms('compatible', (array) $compat)); } if ($tags) { $filter->addFilter(new Terms('tag', (array) $tags)); } if ($type || $compat || $tags) { $query->setFilter($filter); } $list = new ResultList($this->elastica->getIndex(), $query); if ($sort) { $ids = $list->column('ID'); if ($ids) { $list = Addon::get()->byIDs($ids); } else { $list = new ArrayList(); } } } else { if (!$sort) { $sort = 'downloads'; } } switch ($sort) { case 'name': $list = $list->sort('Name'); break; case 'newest': $list = $list->sort('Released', 'DESC'); break; case 'downloads': $list = $list->sort('Downloads', 'DESC'); break; } $list = new PaginatedList($list, $this->request); $list->setPageLength(16); return $list; }