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; }
/** * @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()); }
public function findByUserAndTags(User $user, $tags = null) { $filterQuery = new Query\Filtered(); $searchFilter = new Filter\Bool(); $searchFilter->addMust(new Filter\Term(array('user' => $user->getId()))); if (!empty($tags)) { $tagFilter = new Filter\Terms('tags', explode('+', $tags)); $tagFilter->setExecution('and'); $searchFilter->addMust($tagFilter); } $filterQuery->setFilter($searchFilter); $searchQuery = new Query($filterQuery); $searchQuery->addSort(array('createdAt' => 'desc')); $agg = new Aggregation\Terms('tags'); $agg->setField('tags')->setOrder('_count', 'desc')->setSize(100); $searchQuery->addAggregation($agg); return $searchQuery; }
/** * Compile the Elasticsearch query. * * @return string The Elasticsearch query. */ public function compileQuery() { if ($this->_parts['fields']) { $this->_elasticQuery->setSource($this->_parts['fields']); } if (isset($this->_parts['limit'])) { $this->_elasticQuery->setSize($this->_parts['limit']); } if (isset($this->_parts['offset'])) { $this->_elasticQuery->setFrom($this->_parts['offset']); } if ($this->_parts['order']) { $this->_elasticQuery->setSort($this->_parts['order']); } if ($this->_parts['aggregations']) { foreach ($this->_parts['aggregations'] as $aggregation) { $this->_elasticQuery->addAggregation($aggregation); } } $filteredQuery = new FilteredQuery(); if ($this->_parts['query'] !== null) { $filteredQuery->setQuery($this->_parts['query']); $this->_elasticQuery->setQuery($filteredQuery); } if ($this->_parts['preFilter'] !== null) { $filteredQuery->setFilter($this->_parts['preFilter']); $this->_elasticQuery->setQuery($filteredQuery); } if ($this->_parts['postFilter'] !== null) { $this->_elasticQuery->setPostFilter($this->_parts['postFilter']); } return $this->_elasticQuery; }
/** * @group unit * @expectedException \Elastica\Exception\InvalidException */ public function testFilteredWithoutArgumentsShouldRaiseException() { $query = new Filtered(); $query->toArray(); }
public function search() { //$finder = $this->container->get('fos_elastica.finder.search.articles'); $bool = new Query\Bool(); $multiMatch = new Query\MultiMatch(); $multiMatch->setFields(['subjects', 'title', 'keywords', 'subtitle', 'citations.raw', 'journal.title', 'journal.subtitle']); $multiMatch->setType('phrase_prefix'); $multiMatch->setQuery($this->getParam('term')); $bool->addMust($multiMatch); if ($this->filter) { foreach ($this->filter as $key => $filter) { $filterObj = new Query\Match(); $this->applyFilter($filterObj, $key, $filter); $bool->addMust($filterObj); } } $missing = new Filter\Missing("issue"); $not = new Filter\BoolNot($missing); $notQ = new Query\Filtered(); $notQ->setFilter($not); $bool->addMust($notQ); $query = new Query(); $query->setQuery($bool); $query->setFrom($this->getPage() * $this->getLimit()); $query->setSize($this->getLimit()); $aggregation = new Terms('journals'); $aggregation->setField('journal.id'); $aggregation->setOrder('_count', 'desc'); $qb = $this->em->createQueryBuilder(); $qb->select('count(r.id)')->from('OjsJournalBundle:Journal', 'r')->where($qb->expr()->eq('r.status', 3)); $aggregation->setSize($qb->getQuery()->getSingleScalarResult()); $query->addAggregation($aggregation); $aggregation = new Terms('authors'); $aggregation->setField('articleAuthors.author.id'); $aggregation->setOrder('_count', 'desc'); $qb = $this->em->createQueryBuilder(); $qb->select('count(r.id)')->from('OjsJournalBundle:Author', 'r'); $aggregation->setSize($qb->getQuery()->getSingleScalarResult()); $query->addAggregation($aggregation); $elasticaAdapter = new ElasticaAdapter($this->index, $query); $pagerFanta = new Pagerfanta($elasticaAdapter); $pagerFanta->setMaxPerPage($this->getLimit()); $pagerFanta->setCurrentPage($this->getPage()); /** @var ResultSet $search */ $search = $pagerFanta->getCurrentPageResults(); $result = $search->getResults(); //$search->getResults(); $this->pager = $pagerFanta; $transformer = new ElasticaToModelTransformer($this->registry, 'OjsJournalBundle:Article'); $transformer->setPropertyAccessor($this->propertyAccessor); $this->result = $transformer->transform($result); $this->setCount($pagerFanta->getNbResults()); $this->addAggregation('journal', $this->transform($search->getAggregation('journals')['buckets'], 'OjsJournalBundle:Journal')); $this->addAggregation('author', $this->transform($search->getAggregation('authors')['buckets'], 'OjsJournalBundle:Author')); return $this; }
/** * @param BoolOr $nestedOrFilter Nested or filter * @param string $nestedProperty Nested property (can be 'variants') */ private function addAvailableProductsVariantFilters(BoolOr $nestedOrFilter, $nestedProperty) { $variantsNestedBool = new BoolQuery(); $variantsNestedBool->setMinimumNumberShouldMatch(1); $availableOn = new QueryRange($nestedProperty . '.availableOn', array('lte' => "now")); $variantsNestedBool->addMust($availableOn); $availableUntil = new Filtered(); $availableUntilFilter = new BoolOr(); $availableUntilNull = new Missing($nestedProperty . '.availableUntil'); $availableUntilFilter->addFilter($availableUntilNull); $availableUntilGte = new FilterRange($nestedProperty . '.availableUntil', array('gte' => time())); $availableUntilFilter->addFilter($availableUntilGte); $availableUntil->setFilter($availableUntilFilter); $variantsNestedBool->addMust($availableUntil); $availableOnDemand = new QueryTerm(array($nestedProperty . '.availableOnDemand' => true)); $variantsNestedBool->addShould($availableOnDemand); $onHand = new QueryRange($nestedProperty . '.onHand', array('gt' => 0)); $variantsNestedBool->addShould($onHand); $nested = new Nested(); $nested->setPath($nestedProperty); $nested->setQuery($variantsNestedBool); $nestedOrFilter->addFilter($nested); }