Esempio n. 1
1
 private function getCountQuery($journal)
 {
     $filter = new Term();
     $filter->setTerm('journalUsers.journal.id', $journal->getId());
     $filterQuery = new Query\Filtered();
     $filterQuery->setFilter($filter);
     $query = new Query($filterQuery);
     $query->setSize(0);
     return $query;
 }
Esempio n. 2
0
 /**
  * @group unit
  */
 public function testToArrayWithLegacyFilter()
 {
     $query = new BoolQuery();
     $idsQuery1 = new Ids();
     $idsQuery1->setIds(1);
     $idsQuery2 = new Ids();
     $idsQuery2->setIds(2);
     $idsQuery3 = new Ids();
     $idsQuery3->setIds(3);
     $this->hideDeprecated();
     $filter1 = new TermFilter();
     $filter1->setTerm('test', '1');
     $filter2 = new TermFilter();
     $filter2->setTerm('username', 'ruth');
     $this->showDeprecated();
     $boost = 1.2;
     $minMatch = 2;
     $query->setBoost($boost);
     $query->setMinimumNumberShouldMatch($minMatch);
     $query->addMust($idsQuery1);
     $query->addMustNot($idsQuery2);
     $query->addShould($idsQuery3->toArray());
     $this->hideDeprecated();
     $query->addFilter($filter1);
     $query->addFilter($filter2);
     $this->showDeprecated();
     $expectedArray = array('bool' => array('must' => array($idsQuery1->toArray()), 'should' => array($idsQuery3->toArray()), 'filter' => array($filter1->toArray(), $filter2->toArray()), 'minimum_number_should_match' => $minMatch, 'must_not' => array($idsQuery2->toArray()), 'boost' => $boost));
     $this->assertEquals($expectedArray, $query->toArray());
 }
Esempio n. 3
0
 public function testFilteredSearch()
 {
     $client = $this->_getClient();
     $index = $client->getIndex('test');
     $index->create(array(), true);
     $type = $index->getType('helloworld');
     $doc = new Document(1, array('id' => 1, 'email' => '*****@*****.**', 'username' => 'hanswurst', 'test' => array('2', '3', '5')));
     $type->addDocument($doc);
     $doc = new Document(2, array('id' => 2, 'email' => '*****@*****.**', 'username' => 'peter', 'test' => array('2', '3', '5')));
     $type->addDocument($doc);
     $queryString = new QueryString('test*');
     $filter1 = new Term();
     $filter1->setTerm('username', 'peter');
     $filter2 = new Term();
     $filter2->setTerm('username', 'qwerqwer');
     $query1 = new Filtered($queryString, $filter1);
     $query2 = new Filtered($queryString, $filter2);
     $index->refresh();
     $resultSet = $type->search($queryString);
     $this->assertEquals(2, $resultSet->count());
     $resultSet = $type->search($query1);
     $this->assertEquals(1, $resultSet->count());
     $resultSet = $type->search($query2);
     $this->assertEquals(0, $resultSet->count());
 }
Esempio n. 4
0
 /**
  * @group functional
  */
 public function testSearchByDocument()
 {
     $client = $this->_getClient(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->addDocuments(array(new Document(1, array('visible' => true, 'name' => 'bruce wayne batman')), new Document(2, array('visible' => true, 'name' => 'bruce wayne')), new Document(3, array('visible' => false, 'name' => 'bruce wayne')), new Document(4, array('visible' => true, 'name' => 'batman')), new Document(5, array('visible' => false, 'name' => 'batman')), new Document(6, array('visible' => true, 'name' => 'superman')), new Document(7, array('visible' => true, 'name' => 'spiderman'))));
     $index->refresh();
     $doc = $type->getDocument(1);
     // Return all similar
     $mltQuery = new MoreLikeThis();
     $mltQuery->setMinTermFrequency(1);
     $mltQuery->setMinDocFrequency(1);
     $mltQuery->setLike($doc);
     $query = new Query($mltQuery);
     $resultSet = $type->search($query);
     $this->assertEquals(4, $resultSet->count());
     $mltQuery = new MoreLikeThis();
     $mltQuery->setMinTermFrequency(1);
     $mltQuery->setMinDocFrequency(1);
     $mltQuery->setLike($doc);
     $query = new Query\Filtered($mltQuery);
     // Return just the visible similar
     $filter = new BoolFilter();
     $filterTerm = new Term();
     $filterTerm->setTerm('visible', true);
     $filter->addMust($filterTerm);
     $query->setFilter($filter);
     $resultSet = $type->search($query);
     $this->assertEquals(2, $resultSet->count());
 }
Esempio n. 5
0
 /**
  * @group unit
  */
 public function testToArray()
 {
     $query = new Term();
     $key = 'name';
     $value = 'ruflin';
     $query->setTerm($key, $value);
     $data = $query->toArray();
     $this->assertInternalType('array', $data['term']);
     $this->assertEquals(array($key => $value), $data['term']);
 }
Esempio n. 6
0
 /**
  * @group functional
  */
 public function testQuery()
 {
     $query = new Query();
     $match = new Match();
     $match->setField('make', 'ford');
     $query->setQuery($match);
     $filter = new Term();
     $filter->setTerm('color', 'green');
     $query->setPostFilter($filter);
     $results = $this->_getTestIndex()->search($query);
     $this->assertEquals(1, $results->getTotalHits());
 }
Esempio n. 7
0
 /**
  * @group functional
  */
 public function testFilteredSearchNoQuery()
 {
     $index = $this->_createIndex();
     $type = $index->getType('helloworld');
     $type->addDocuments(array(new Document(1, array('id' => 1, 'email' => '*****@*****.**', 'username' => 'hanswurst', 'test' => array('2', '3', '5'))), new Document(2, array('id' => 2, 'email' => '*****@*****.**', 'username' => 'peter', 'test' => array('2', '3', '5')))));
     $index->refresh();
     $filter = new Term();
     $filter->setTerm('username', 'peter');
     $query = new Filtered(null, $filter);
     $resultSet = $type->search($query);
     $this->assertEquals(1, $resultSet->count());
 }
 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();
 }
Esempio n. 9
0
 public function testAddFilterScript()
 {
     $customFiltersScoreQuery = new CustomFiltersScore();
     $rangeFilter = new Range('age', array('from' => 20, 'to' => 30));
     $rangeScript = "doc['num1'].value > 1";
     $customFiltersScoreQuery->addFilterScript($rangeFilter, $rangeScript);
     $expected = array('custom_filters_score' => array('query' => array('match_all' => new \stdClass()), 'filters' => array(array('filter' => $rangeFilter->toArray(), 'script' => $rangeScript))));
     $this->assertEquals($expected, $customFiltersScoreQuery->toArray());
     $customFiltersScoreQuery = new CustomFiltersScore();
     $script = "doc['num1'].value > 1";
     $rangeScript = new Script($script);
     $rangeScript->setParam('param1', 1);
     $rangeScript->setLang(Script::LANG_GROOVY);
     $customFiltersScoreQuery->addFilterScript($rangeFilter, $rangeScript);
     $expected = array('custom_filters_score' => array('query' => array('match_all' => new \stdClass()), 'filters' => array(array('filter' => $rangeFilter->toArray(), 'script' => $script))));
     $this->assertEquals($expected, $customFiltersScoreQuery->toArray());
     $termFilter = new Term();
     $termFilter->setTerm('name', 'ruflin');
     $termScript = "doc['num2'].value > 1";
     $customFiltersScoreQuery->addFilterScript($termFilter, $termScript);
     $expected = array('custom_filters_score' => array('query' => array('match_all' => new \stdClass()), 'filters' => array(array('filter' => $rangeFilter->toArray(), 'script' => $script), array('filter' => $termFilter->toArray(), 'script' => $termScript))));
     $this->assertEquals($expected, $customFiltersScoreQuery->toArray());
 }
 /**
  * {@inheritdoc}
  */
 public function get(array $data, array $keys, $namespace)
 {
     if (!$this->hasNamespace($namespace)) {
         return array();
     }
     // Build the query
     $query = new Query();
     // Add hash term
     $term = new Term();
     $term->setTerm('hash', $this->getHash($data, $keys));
     $query->setPostFilter($term);
     $esResults = $this->index->getType($namespace)->search($query)->getResults();
     $results = array();
     foreach ($esResults as $esResult) {
         $results[] = $esResult->getData();
     }
     return $results;
 }
Esempio n. 11
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;
 }
 /**
  * @return array
  */
 protected function addFilters()
 {
     if (!isset($_GET['filters'])) {
         return false;
     }
     $filterCount = 0;
     $filters = new El\Filter\Bool();
     foreach ($_GET['filters'] as $key => $filterValue) {
         $filterCatCount = 0;
         $filterPart = new El\Filter\BoolOr();
         foreach ($filterValue as $term => $enabled) {
             if ($enabled == 'true') {
                 $term = str_replace('_', ' ', $term);
                 $filter = new El\Filter\Term();
                 $filter->setTerm($key, $term);
                 $filterPart->addFilter($filter);
                 $filterCount++;
                 $filterCatCount++;
             }
         }
         if ($filterCatCount > 0) {
             $filters->addMust($filterPart);
         }
     }
     if ($filterCount === 0) {
         return false;
     }
     return $filters;
 }
Esempio n. 13
0
 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());
 }
Esempio n. 14
0
 /**
  * @return BoolOr
  */
 private function addFilters()
 {
     //		$filterCount = 0;
     $filters = new Bool();
     $typeFilter = new Type('issue');
     $filters->addMust($typeFilter);
     if (!isset($_GET['filters'])) {
         return $filters;
     }
     foreach ($_GET['filters'] as $key => $filterValue) {
         $filterCatCount = 0;
         $filterPart = new BoolOr();
         foreach ($filterValue as $term => $enabled) {
             if ($enabled == 'true') {
                 $term = str_replace('_', ' ', $term);
                 $filter = new Term();
                 if ($key == 'tracker' || $key == 'project' || $key == 'category' || $key == 'status' || $key == 'author' || $key == 'priority' || $key == 'fixed_version') {
                     $filter->setTerm($key . '.name', $term);
                 } else {
                     $filter->setTerm($key, $term);
                 }
                 $filterPart->addFilter($filter);
                 //					$filterCount++;
                 $filterCatCount++;
             }
         }
         if ($filterCatCount > 0) {
             $filters->addMust($filterPart);
         }
     }
     //		if($filterCount === 0) {
     //			return false;
     //		}
     return $filters;
 }