Inheritance: extends AbstractQuery
コード例 #1
0
 /**
  * Filter search results so only documents that are viewable by the current
  * user will be returned...
  *
  * @param \Elastica\Query\BoolQuery $elasticaQueryBool
  */
 protected function applySecurityContext($elasticaQueryBool)
 {
     $roles = $this->getCurrentUserRoles();
     $elasticaQueryRoles = new \Elastica\Query\Terms();
     $elasticaQueryRoles->setTerms('view_roles', $roles)->setMinimumMatch(1);
     $elasticaQueryBool->addMust($elasticaQueryRoles);
 }
コード例 #2
0
 /**
  * @group functional
  */
 public function testIndicesQuery()
 {
     $docs = array(new Document(1, array('color' => 'blue')), new Document(2, array('color' => 'green')), new Document(3, array('color' => 'blue')), new Document(4, array('color' => 'yellow')));
     $index1 = $this->_createIndex();
     $index1->addAlias('indices_query');
     $index1->getType('test')->addDocuments($docs);
     $index1->refresh();
     $index2 = $this->_createIndex();
     $index2->addAlias('indices_query');
     $index2->getType('test')->addDocuments($docs);
     $index2->refresh();
     $boolQuery = new Query\BoolQuery();
     $boolQuery->addMustNot(new Term(array('color' => 'blue')));
     $indicesQuery = new Indices($boolQuery, array($index1->getName()));
     $boolQuery = new Query\BoolQuery();
     $boolQuery->addMustNot(new Term(array('color' => 'yellow')));
     $indicesQuery->setNoMatchQuery($boolQuery);
     $query = new Query();
     $query->setPostFilter($indicesQuery);
     // search over the alias
     $index = $this->_getClient()->getIndex('indices_query');
     $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() === $index1->getName()) {
             $this->assertNotEquals('blue', $color);
         } else {
             $this->assertNotEquals('yellow', $color);
         }
     }
 }
コード例 #3
0
ファイル: MoreLikeThisTest.php プロジェクト: rdohms/Elastica
 /**
  * @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\BoolQuery();
     $query->addMust($mltQuery);
     // Return just the visible similar
     $filter = new BoolFilter();
     $filterTerm = new Term();
     $filterTerm->setTerm('visible', true);
     $filter->addMust($filterTerm);
     $query->addFilter($filter);
     $resultSet = $type->search($query);
     $this->assertEquals(2, $resultSet->count());
 }
コード例 #4
0
 /**
  * @group functional
  */
 public function testSearch()
 {
     $client = $this->_getClient();
     $index = new Index($client, 'test');
     $index->create(array(), true);
     $type = new Type($index, 'helloworld');
     $doc = new Document(1, array('id' => 1, 'email' => '*****@*****.**', 'username' => 'hans', 'test' => array('2', '3', '5')));
     $type->addDocument($doc);
     $doc = new Document(2, array('id' => 2, 'email' => '*****@*****.**', 'username' => 'emil', 'test' => array('1', '3', '6')));
     $type->addDocument($doc);
     $doc = new Document(3, array('id' => 3, 'email' => '*****@*****.**', 'username' => 'ruth', 'test' => array('2', '3', '7')));
     $type->addDocument($doc);
     // Refresh index
     $index->refresh();
     $boolQuery = new BoolQuery();
     $termQuery1 = new Term(array('test' => '2'));
     $boolQuery->addMust($termQuery1);
     $resultSet = $type->search($boolQuery);
     $this->assertEquals(2, $resultSet->count());
     $termQuery2 = new Term(array('test' => '5'));
     $boolQuery->addMust($termQuery2);
     $resultSet = $type->search($boolQuery);
     $this->assertEquals(1, $resultSet->count());
     $termQuery3 = new Term(array('username' => 'hans'));
     $boolQuery->addMust($termQuery3);
     $resultSet = $type->search($boolQuery);
     $this->assertEquals(1, $resultSet->count());
     $termQuery4 = new Term(array('username' => 'emil'));
     $boolQuery->addMust($termQuery4);
     $resultSet = $type->search($boolQuery);
     $this->assertEquals(0, $resultSet->count());
 }
コード例 #5
0
 /**
  * @group functional
  */
 public function testGeoProvided()
 {
     $index = $this->_createIndex();
     $indexName = $index->getName();
     $type = $index->getType('type');
     $otherType = $index->getType('other_type');
     // create mapping
     $mapping = new \Elastica\Type\Mapping($type, array('location' => array('type' => 'geo_shape')));
     $type->setMapping($mapping);
     // create other type mapping
     $otherMapping = new \Elastica\Type\Mapping($type, array('location' => array('type' => 'geo_shape')));
     $otherType->setMapping($otherMapping);
     // add type docs
     $type->addDocument(new \Elastica\Document('1', array('location' => array('type' => 'envelope', 'coordinates' => array(array(0.0, 50.0), array(50.0, 0.0))))));
     // add other type docs
     $otherType->addDocument(new \Elastica\Document('2', array('location' => array('type' => 'envelope', 'coordinates' => array(array(25.0, 75.0), array(75.0, 25.0))))));
     $index->optimize();
     $index->refresh();
     $gsp = new GeoShapePreIndexed('location', '1', 'type', $indexName, 'location');
     $gsp->setRelation(AbstractGeoShape::RELATION_INTERSECT);
     $expected = array('geo_shape' => array('location' => array('indexed_shape' => array('id' => '1', 'type' => 'type', 'index' => $indexName, 'path' => 'location'), 'relation' => $gsp->getRelation())));
     $this->assertEquals($expected, $gsp->toArray());
     $query = new BoolQuery();
     $query->addFilter($gsp);
     $results = $index->getType('type')->search($query);
     $this->assertEquals(1, $results->count());
     $index->delete();
 }
コード例 #6
0
ファイル: PeopleController.php プロジェクト: beyzakokcan/ojs
 /**
  * @param  Request $request
  * @param  int $page
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public function indexAction(Request $request, $page = 1)
 {
     $getRoles = $request->query->get('role_filters');
     $getSubjects = $request->query->get('subject_filters');
     $getJournals = $request->query->get('journal_filters');
     $roleFilters = !empty($getRoles) ? explode(',', $getRoles) : [];
     $subjectFilters = !empty($getSubjects) ? explode(',', $getSubjects) : [];
     $journalFilters = !empty($getJournals) ? explode(',', $getJournals) : [];
     $userSearcher = $this->get('fos_elastica.index.search.user');
     $userQuery = new Query('*');
     if (!empty($roleFilters) || !empty($subjectFilters) || !empty($journalFilters)) {
         $boolQuery = new Query\BoolQuery();
         foreach ($roleFilters as $role) {
             $match = new Query\Match();
             $match->setField('journalUsers.roles', $role);
             $boolQuery->addMust($match);
         }
         foreach ($subjectFilters as $subject) {
             $match = new Query\Match();
             $match->setField('subjects', $subject);
             $boolQuery->addMust($match);
         }
         foreach ($journalFilters as $journal) {
             $match = new Query\Match();
             $match->setField('journalUsers.journal.title', $journal);
             $boolQuery->addMust($match);
         }
         $userQuery->setQuery($boolQuery);
     }
     $roleAgg = new Aggregation\Terms('roles');
     $roleAgg->setField('journalUsers.roles');
     $roleAgg->setOrder('_term', 'asc');
     $roleAgg->setSize(0);
     $userQuery->addAggregation($roleAgg);
     $subjectAgg = new Aggregation\Terms('subjects');
     $subjectAgg->setField('subjects');
     $subjectAgg->setOrder('_term', 'asc');
     $subjectAgg->setSize(0);
     $userQuery->addAggregation($subjectAgg);
     $journalAgg = new Aggregation\Terms('journals');
     $journalAgg->setField('journalUsers.journal.title');
     $journalAgg->setOrder('_term', 'asc');
     $journalAgg->setSize(0);
     $userQuery->addAggregation($journalAgg);
     $adapter = new ElasticaAdapter($userSearcher, $userQuery);
     $pagerfanta = new Pagerfanta($adapter);
     $pagerfanta->setMaxPerPage(20);
     $pagerfanta->setCurrentPage($page);
     $people = $pagerfanta->getCurrentPageResults();
     $roles = $adapter->getResultSet()->getAggregation('roles')['buckets'];
     $subjects = $adapter->getResultSet()->getAggregation('subjects')['buckets'];
     $journals = $adapter->getResultSet()->getAggregation('journals')['buckets'];
     $data = ['people' => $people, 'roles' => $roles, 'subjects' => $subjects, 'journals' => $journals, 'pagerfanta' => $pagerfanta, 'role_filters' => $roleFilters, 'subject_filters' => $subjectFilters, 'journal_filters' => $journalFilters, 'page' => 'ojs_site_people_index'];
     return $this->render('OjsSiteBundle:People:index.html.twig', $data);
 }
 /**
  * @return Query
  */
 public function getESQuery()
 {
     if (!$this->esQuery) {
         $this->esQuery = new Query();
         $this->boolQuery = new Query\BoolQuery();
         $this->esQuery->setQuery($this->boolQuery);
         $familyQuery = new Query\Match('family', implode(' ', $this->family->getMatchingCodes()));
         $this->boolQuery->addMust($familyQuery);
     }
     return $this->esQuery;
 }
コード例 #8
0
 /**
  * @param ElasticaFilterInterface $filter
  * @param FormInterface           $form
  * @param Query\BoolQuery         $query
  */
 public function handleESForm(ElasticaFilterInterface $filter, FormInterface $form, Query\BoolQuery $query)
 {
     $data = $form->getData();
     if (!$data) {
         return;
     }
     foreach ($filter->getAttributes() as $column) {
         $subQuery = new Query\Match($column, $data);
         $query->addMust($subQuery);
     }
 }
コード例 #9
0
 public function __construct(Index $index, Query $query, LoggerInterface $logger = null)
 {
     //Optimise the query by just getting back the ids and types
     $query->setFields(array('_id', '_type'));
     //If we are in live reading mode, only return published documents
     if (\Versioned::get_reading_mode() == \Versioned::DEFAULT_MODE) {
         $publishedFilter = new Query\BoolQuery();
         $publishedFilter->addMust(new Query\Term([Searchable::$published_field => 'true']));
         $query->setPostFilter($publishedFilter);
     }
     $this->index = $index;
     $this->query = $query;
     $this->logger = $logger;
 }
コード例 #10
0
 /**
  * @param ElasticaFilterInterface $filter
  * @param FormInterface           $form
  * @param Query\BoolQuery         $query
  *
  * @throws \Exception
  */
 public function handleESForm(ElasticaFilterInterface $filter, FormInterface $form, Query\BoolQuery $query)
 {
     $data = $form->getData();
     if (!$data) {
         return;
     }
     if (is_object($data)) {
         if (!method_exists($data, 'getId')) {
             throw new \Exception('It is not yet possible to search in data not implementing a getId function');
         }
         $data = $data->getId();
         // @todo fix me !
     }
     foreach ($filter->getAttributes() as $column) {
         $subQuery = new Query\Match($column, $data);
         $query->addMust($subQuery);
     }
 }
 /**
  * @group functional
  */
 public function testConstructEnvelope()
 {
     $index = $this->_createIndex();
     $type = $index->getType('test');
     // create mapping
     $mapping = new Mapping($type, array('location' => array('type' => 'geo_shape')));
     $type->setMapping($mapping);
     // add docs
     $type->addDocument(new Document(1, array('location' => array('type' => 'envelope', 'coordinates' => array(array(-50.0, 50.0), array(50.0, -50.0))))));
     $index->optimize();
     $index->refresh();
     $envelope = array(array(25.0, 75.0), array(75.0, 25.0));
     $gsp = new GeoShapeProvided('location', $envelope);
     $expected = array('geo_shape' => array('location' => array('shape' => array('type' => GeoShapeProvided::TYPE_ENVELOPE, 'coordinates' => $envelope, 'relation' => AbstractGeoShape::RELATION_INTERSECT))));
     $this->assertEquals($expected, $gsp->toArray());
     $query = new BoolQuery();
     $query->addFilter($gsp);
     $results = $type->search($query);
     $this->assertEquals(1, $results->count());
 }
コード例 #12
0
 protected function searchDocuments()
 {
     dump(__METHOD__);
     $client = $this->getClient();
     $index = $client->getIndex($this->getIndexName());
     $type = $index->getType($this->getTypeName());
     if (true) {
         $query = json_decode('{"query":{"bool":{"must":[{"term":{"uid":19246}},{"term":{"name":"XXXXXXXXXX"}},{"term":{"op":30}}],"filter":[{"range":{"level":{"from":10,"to":300}}},{"range":{"addtime":{"gte":"20150706T145200+0800","lte":"20150707T145203+0800"}}}]}}}', true);
         dump($query);
         $path = $index->getName() . '/' . $type->getName() . '/_search';
         $response = $client->request($path, Request::GET, $query);
         $this->assertTrue($response->isOk());
         //            dump($response->getEngineTime());
         //            dump($response->getQueryTime());
         //            dump($response->getShardsStatistics());
         //            dump($response->getStatus()); // http status code
         //            dump($response->getTransferInfo());
         dump($response->getData()['hits']['hits']);
     }
     if (false) {
         $search = new Search($client);
         $search->addIndex($index)->addType($type);
         $query = new Query\BoolQuery();
         //        $query->setFrom(0);
         //        $query->setSize(10);
         //        $query->setSort(['uid' => 'asc']);
         //        $query->setFields(['snsid', 'uid']);
         //        $query->setHighlight(['fields' => 'uid']);
         //        $query->setExplain(true);
         //        $term = new Query\Term(['name' => 'XXXXXXXXXX']);
         //        $query->setQuery($term);
         $query->addMust(new Term(['uid' => 19246]));
         $query->addMust(new Term(['name' => 'XXXXXXXXXX']));
         $query->addMust(new Term(['op' => 30]));
         //        $query->addMustNot(new Term(['country' => 'CN']));
         $range = new Query\Range('level', ['from' => 10, 'to' => 300]);
         $query->addFilter($range);
         $range = new Query\Range();
         $range->addField('addtime', ['gte' => '20150706T145200+0800', 'lte' => '20150707T145203+0800']);
         $query->addFilter($range);
         $search->setQuery($query);
         $resultSet = $search->search();
         dump('Hit: ' . $resultSet->count());
         $queryArray = $resultSet->getQuery()->toArray();
         dump(json_encode($queryArray));
         dump($resultSet->getResponse()->getData()['hits']['hits']);
         dump('query time: ' . \PHP_Timer::secondsToTimeString($resultSet->getResponse()->getQueryTime()));
     }
 }
コード例 #13
0
 /**
  * Filter search results so only documents that are viewable by the current
  * user will be returned...
  *
  * @param \Elastica\Query\BoolQuery $elasticaQueryBool
  */
 protected function applySecurityContext($elasticaQueryBool)
 {
     $roles = array();
     if (!is_null($this->securityContext)) {
         $user = $this->securityContext->getToken()->getUser();
         if ($user instanceof BaseUser) {
             $roles = $user->getRoles();
         }
     }
     // Anonymous access should always be available for both anonymous & logged in users
     if (!in_array('IS_AUTHENTICATED_ANONYMOUSLY', $roles)) {
         $roles[] = 'IS_AUTHENTICATED_ANONYMOUSLY';
     }
     $elasticaQueryRoles = new \Elastica\Query\Terms();
     $elasticaQueryRoles->setTerms('view_roles', $roles)->setMinimumMatch(1);
     $elasticaQueryBool->addMust($elasticaQueryRoles);
 }
コード例 #14
0
 /**
  *
  * @param BoolQuery $lead_query        	
  * @param Filter $filter        	
  *
  * @return BoolQuery $lead_query
  */
 protected function applyFilters(BoolQuery $lead_query, Filter $filter = null)
 {
     if ($filter) {
         $account = $filter->getAccountFilter();
         $date = $filter->getDateFilter();
         if ($account) {
             $mode = $account->getMode();
             switch ($mode) {
                 case 'orphan':
                     $account_query = new Elastica\Query\Nested();
                     $account_query->setPath('account')->setScoreMode('avg');
                     $account_query->setQuery(new Elastica\Query\MatchAll());
                     $lead_query->addMustNot($account_query);
                     break;
                 case 'account':
                     if (($account = $account->getAccount()) == true) {
                         $account_id = $account->getId();
                         $account_query = new Elastica\Query\Nested();
                         $account_query->setPath('account')->setScoreMode('avg');
                         $account_match_query = new Elastica\Query\Match('account.id', $account_id);
                         $account_bool_query = new Elastica\Query\BoolQuery();
                         $account_bool_query->addMust($account_match_query);
                         $account_query->setQuery($account_bool_query);
                         $lead_query->addMust($account_query);
                     }
                     break;
             }
         }
         if ($date) {
             $dates = [];
             $mode = $date->getMode();
             switch ($mode) {
                 case null:
                 case false:
                     break;
                 case 'timecreated':
                     if (($range = $date->getTimecreated()) == true) {
                         $dates = explode(" - ", $range);
                     }
                     break;
                 default:
                     switch ($mode) {
                         // Today
                         case "1":
                             $dates = [date('Y-m-d\\T00:00:00', time()), date('Y-m-d\\T23:59:59', time())];
                             break;
                             // Last 7 days
                         // Last 7 days
                         case "7":
                             $dates = [date('Y-m-d\\T00:00:00', strtotime('-7 days')), date('Y-m-d\\T23:59:59', time())];
                             break;
                             // Last 30 days
                         // Last 30 days
                         case "30":
                             $dates = [date('Y-m-d\\T00:00:00', strtotime('-30 days')), date('Y-m-d\\T23:59:59', time())];
                             break;
                             // This Month
                         // This Month
                         case "month":
                             $dates = [date('Y-m-01\\T00:00:00', time()), date('Y-m-t\\T23:59:59', time())];
                             break;
                             // Last Month
                         // Last Month
                         case "lmonth":
                             $dates = [date('Y-m-01\\T00:00:00', strtotime('last month')), date('Y-m-t\\T23:59:59', strtotime('last month'))];
                             break;
                             // This Year
                         // This Year
                         case "year":
                             $dates = [date('Y-01-01\\T00:00:00', time()), date('Y-m-d\\T23:59:59', time())];
                             break;
                     }
                     break;
             }
             if ($dates) {
                 $date_query = new Elastica\Query\Range();
                 foreach ($dates as &$date) {
                     $time = strtotime($date);
                     if ($time) {
                         $date = date('Y-m-d\\TH:i:s', $time);
                     } else {
                         $date = date('Y-m-d\\TH:i:s', -9999999999.0);
                     }
                 }
                 @(list($from, $to) = $dates);
                 if (isset($from, $to)) {
                     $date_query->addField('timecreated', ['gte' => $from, 'lte' => $to]);
                     $lead_query->addMust($date_query);
                 }
             }
         }
     }
     return $lead_query;
 }
コード例 #15
0
 /**
  *
  * @param array $location        	
  *
  * @return Elastica\Query $localityQuery
  */
 public static function getLocalityQuery($location)
 {
     $query = new Agent\Elastica\Query\BoolQuery();
     $method = 'addMust';
     if (!isset($location['state']) && !isset($location['zip']) && !isset($location['locality'])) {
         foreach (['phone', 'ipaddress'] as $field) {
             if (isset($location[$field]) && empty($location['state'])) {
                 switch ($field) {
                     case 'ipaddress':
                         $geo = self::$geo;
                         $loc = $geo->getRecord($location['ipaddress']);
                         if ($loc instanceof Record) {
                             $state = $loc->getRegion();
                             if ($state) {
                                 $location['state'] = $state;
                             }
                         }
                         break;
                     case 'phone':
                         $phone = Helper::parse_phonenumber($location['phone'], 'array');
                         if ($phone) {
                             $state = Helper::area_code_to_state($phone[0]);
                             if ($state) {
                                 $location['state'] = $state;
                             }
                         }
                         break;
                 }
             }
         }
     }
     foreach ($location as $field => $value) {
         switch ($field) {
             case 'locality':
                 if (!isset($location['zip'])) {
                     $fields = ['latitude', 'longitude'];
                     $values = is_array($value) ? $value : explode(",", $value);
                     $latlon = count($values) == 2 ? array_combine($fields, $values) : false;
                     if ($latlon) {
                         $path = "location";
                         $nested = new Elastica\Query\Nested();
                         $nested->setPath($path);
                         $bool = new Elastica\Query\BoolQuery();
                         foreach ($latlon as $dim => $coord) {
                             $bool->addMust(new Elastica\Query\Match("{$path}.{$dim}", $coord));
                         }
                         $nested->setQuery($bool);
                         $query->addMust($nested);
                     }
                 }
                 break;
             case 'city':
                 if (!isset($location['locality'])) {
                     $query->addShould(new Elastica\Query\Match($field, $value));
                 }
                 break;
             case 'state':
                 if (!isset($location['locality'])) {
                     $fields = ['state.abbrev', 'state.full'];
                     $values = is_array($value) ? $value : [$value];
                     foreach ($values as $state) {
                         $querystring = new Elastica\Query\QueryString($state);
                         $querystring->setFields($fields);
                         $nested = new Elastica\Query\Nested();
                         $nested->setQuery($querystring);
                         $nested->setPath($field);
                         if (count($values) > 1) {
                             $query->addShould($nested);
                         } else {
                             $query->addMust($nested);
                         }
                     }
                 }
                 break;
             case 'zip':
                 $query->{$method}(new Elastica\Query\Match($field, $value));
                 break;
         }
     }
     $localityQuery = new Elastica\Query($query);
     $localityQuery->setSize(1);
     return $localityQuery;
 }
コード例 #16
0
 /**
  * @group functional
  */
 public function testSearchWithLegacyFilter()
 {
     $client = $this->_getClient();
     $index = new Index($client, 'test');
     $index->create(array(), true);
     $type = new Type($index, 'helloworld');
     $doc = new Document(1, array('id' => 1, 'email' => '*****@*****.**', 'username' => 'hans', 'test' => array('2', '4', '5')));
     $type->addDocument($doc);
     $doc = new Document(2, array('id' => 2, 'email' => '*****@*****.**', 'username' => 'emil', 'test' => array('1', '3', '6')));
     $type->addDocument($doc);
     $doc = new Document(3, array('id' => 3, 'email' => '*****@*****.**', 'username' => 'ruth', 'test' => array('2', '3', '7')));
     $type->addDocument($doc);
     $doc = new Document(4, array('id' => 4, 'email' => '*****@*****.**', 'username' => 'john', 'test' => array('2', '4', '8')));
     $type->addDocument($doc);
     // Refresh index
     $index->refresh();
     $boolQuery = new BoolQuery();
     $termQuery1 = new TermQuery(array('test' => '2'));
     $boolQuery->addMust($termQuery1);
     $resultSet = $type->search($boolQuery);
     $this->assertEquals(3, $resultSet->count());
     $this->hideDeprecated();
     $termFilter = new TermFilter(array('test' => '4'));
     $boolQuery->addFilter($termFilter);
     $this->showDeprecated();
     $resultSet = $type->search($boolQuery);
     $this->assertEquals(2, $resultSet->count());
 }
コード例 #17
0
ファイル: Logs.php プロジェクト: bd808/SAL
 /**
  * Search for logs
  *
  * @param array $params Search parameters:
  *   - project: Project to find logs for
  *   - query: Elasticsearch simple query string
  *   - items: Number of results to return per page
  *   - page: Page of results to return (0-index)
  * @return ResultSet
  */
 public function search(array $params = [])
 {
     $params = array_merge(['project' => 'production', 'query' => null, 'items' => 20, 'page' => 0, 'date' => null], $params);
     $filters = new BoolQuery();
     if ($params['query'] !== null) {
         $filters->addMust(new SimpleQueryString($params['query'], ['message', 'nick']));
     }
     if ($params['date'] !== null) {
         $filters->addMust(new Range('@timestamp', ['lte' => "{$params['date']}||/d"]));
     }
     $query = new Query($filters);
     $query->setPostFilter(new Term(['project' => $params['project']]));
     $query->setFrom($params['page'] * $params['items'])->setSize($params['items'])->setFields(self::$DEFAULT_FIELDS)->setSort(['@timestamp' => ['order' => 'desc']]);
     return $this->doSearch($query);
 }
コード例 #18
0
ファイル: AdminJournalController.php プロジェクト: ojs/ojs
 /**
  * @param Request $request
  * @return JsonResponse
  */
 public function autoCompleteAction(Request $request)
 {
     $q = filter_var($request->get('q'), FILTER_SANITIZE_STRING);
     $search = $this->get('fos_elastica.index.search.journal');
     $notCollectJournals = [];
     if ($request->query->has('notCollectJournals')) {
         $notCollectJournalsParam = $request->query->get('notCollectJournals');
         if (!empty($notCollectJournalsParam) && is_array($notCollectJournalsParam)) {
             $notCollectJournals = $notCollectJournalsParam;
         }
     }
     $searchQuery = new Query('_all');
     $boolQuery = new Query\BoolQuery();
     $fieldQuery = new Query\MultiMatch();
     $fieldQuery->setFields(['title']);
     $fieldQuery->setQuery(strtoupper($q));
     $fieldQuery->setFuzziness(0.7);
     $boolQuery->addMust($fieldQuery);
     $searchQuery->setQuery($boolQuery);
     $searchQuery->setSize(10);
     $results = $search->search($searchQuery);
     $data = [];
     foreach ($results as $result) {
         if (!in_array($result->getId(), $notCollectJournals)) {
             $data[] = ['id' => $result->getId(), 'text' => $result->getData()['title']];
         }
     }
     return new JsonResponse($data);
 }
コード例 #19
0
 /**
  * Filter search results so only documents that are viewable by the current
  * user will be returned...
  *
  * @param \Elastica\Query\BoolQuery $elasticaQueryBool
  */
 protected function applySecurityFilter($elasticaQueryBool)
 {
     $roles = $this->getCurrentUserRoles();
     $elasticaQueryRoles = new \Elastica\Query\Terms();
     $elasticaQueryRoles->setTerms('view_roles', $roles);
     $elasticaQueryBool->addMust($elasticaQueryRoles);
 }
コード例 #20
0
 /**
  * @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);
 }
コード例 #21
0
ファイル: ExploreController.php プロジェクト: beyzakokcan/ojs
 public function publisherAction(Request $request, $page = 1)
 {
     $getTypes = $request->query->get('type_filters');
     $typeFilters = !empty($getTypes) ? explode(',', $getTypes) : [];
     $publisherSearcher = $this->get('fos_elastica.index.search.publisher');
     $boolQuery = new Query\BoolQuery();
     if (!empty($typeFilters)) {
         foreach ($typeFilters as $type) {
             $match = new Query\Match();
             $match->setField('publisherType', $type);
             $boolQuery->addMust($match);
         }
     }
     $publisherQuery = new Query($boolQuery);
     $publisherQuery->setSort(['name.raw' => ['order' => 'asc']]);
     $typeAgg = new Aggregation\Terms('types');
     $typeAgg->setField('publisher.publisherType.name');
     $typeAgg->setOrder('_term', 'asc');
     $typeAgg->setSize(0);
     $publisherQuery->addAggregation($typeAgg);
     $adapter = new ElasticaAdapter($publisherSearcher, $publisherQuery);
     $pagerfanta = new Pagerfanta($adapter);
     $pagerfanta->setMaxPerPage(20);
     $pagerfanta->setCurrentPage($page);
     $publishers = $pagerfanta->getCurrentPageResults();
     $types = $adapter->getResultSet()->getAggregation('types')['buckets'];
     $data = ['types' => $types, 'page' => 'ojs_site_explore_publisher', 'publishers' => $publishers, 'pagerfanta' => $pagerfanta, 'type_filters' => $typeFilters];
     return $this->render('OjsSiteBundle:Explore:publisher.html.twig', $data);
 }
コード例 #22
0
ファイル: SearchController.php プロジェクト: beyzakokcan/ojs
 /**
  * search page index controller
  *
  * @param Request $request
  * @param int $page
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public function indexAction(Request $request, $page = 1)
 {
     $searchManager = $this->get('ojs_search_manager');
     $journalId = null;
     $getRoles = $request->query->get('role_filters');
     $getSubjects = $request->query->get('subject_filters');
     $getJournals = $request->query->get('journal_filters');
     $getLocales = $request->query->get('locale_filters');
     $getPublishers = $request->query->get('publisher_filters');
     $getIndexes = $request->query->get('index_filters');
     $roleFilters = !empty($getRoles) ? explode(',', $getRoles) : [];
     $subjectFilters = !empty($getSubjects) ? explode(',', $getSubjects) : [];
     $journalFilters = !empty($getJournals) ? explode(',', $getJournals) : [];
     $localeFilters = !empty($getLocales) ? explode(',', $getLocales) : [];
     $publisherFilters = !empty($getPublishers) ? explode(',', $getPublishers) : [];
     $indexFilters = !empty($getIndexes) ? explode(',', $getIndexes) : [];
     $queryType = $request->query->has('type') ? $request->get('type') : 'basic';
     $query = filter_var($request->get('q'), FILTER_SANITIZE_STRING);
     $section = filter_var($request->get('section'), FILTER_SANITIZE_STRING);
     $searcher = $this->get('fos_elastica.index.search');
     $searchQuery = new Query('_all');
     $boolQuery = new Query\BoolQuery();
     $match = new Query\Match();
     $match->setField('published', false);
     $boolQuery->addMustNot($match);
     //set query according to query type
     if ($queryType == 'basic') {
         $fieldQuery = new Query\Prefix();
         $fieldQuery->setPrefix('_all', $query);
         $boolQuery->addMust($fieldQuery);
     } elseif ($queryType == 'advanced') {
         $parseQuery = $searchManager->parseSearchQuery($query);
         foreach ($parseQuery as $searchTerm) {
             $condition = $searchTerm['condition'];
             $advancedFieldQuery = new Query\MultiMatch();
             $advancedFieldQuery->setFields([$searchTerm['searchField']]);
             $advancedFieldQuery->setQuery($searchTerm['searchText']);
             if ($condition == 'AND') {
                 $boolQuery->addMust($advancedFieldQuery);
             } elseif ($condition == 'OR') {
                 $boolQuery->addShould($advancedFieldQuery);
             } elseif ($condition == 'NOT') {
                 $boolQuery->addMustNot($advancedFieldQuery);
             }
         }
     } elseif ($queryType == 'tag') {
         $regexpQuery = new Query\Regexp();
         $regexpQuery->setParams(['tags' => ".*" . $query . ".*"]);
         $boolQuery->addMust($regexpQuery);
     } elseif ($queryType == 'injournal') {
         $journalId = $request->get('journalId');
         $boolQuery->setParams($searchManager->getSearchInJournalQuery($journalId, $query));
     }
     //set aggregations if requested
     if (!empty($roleFilters) || !empty($subjectFilters) || !empty($journalFilters) || !empty($localeFilters) || !empty($publisherFilters)) {
         foreach ($roleFilters as $role) {
             $match = new Query\Match();
             $match->setField('user.userJournalRoles.role.name', $role);
             $boolQuery->addMust($match);
         }
         foreach ($subjectFilters as $subject) {
             $match = new Query\Match();
             $match->setField('subjects', $subject);
             $boolQuery->addMust($match);
         }
         foreach ($journalFilters as $journal) {
             $match = new Query\Match();
             $match->setField('user.userJournalRoles.journal.title', $journal);
             $boolQuery->addMust($match);
         }
         foreach ($localeFilters as $locale) {
             $match = new Query\Match();
             $match->setField('translations.locale', $locale);
             $boolQuery->addMust($match);
         }
         foreach ($publisherFilters as $publisher) {
             $match = new Query\Match();
             $match->setField('publisher.name', $publisher);
             $boolQuery->addMust($match);
         }
         foreach ($indexFilters as $index) {
             $match = new Query\Match();
             $match->setField('journalIndexs.index.name', $index);
             $boolQuery->addMust($match);
         }
     }
     //set our boolean query
     $searchQuery->setQuery($boolQuery);
     //get all result
     $searchQuery->setSize(1000);
     //get role aggregation
     $roleAgg = new Aggregation\Terms('roles');
     $roleAgg->setField('userJournalRoles.role.name');
     $roleAgg->setOrder('_term', 'asc');
     $roleAgg->setSize(0);
     $searchQuery->addAggregation($roleAgg);
     //get subject aggregation
     $subjectAgg = new Aggregation\Terms('subjects');
     $subjectAgg->setField('subjects.subject');
     $subjectAgg->setOrder('_term', 'asc');
     $subjectAgg->setSize(0);
     $searchQuery->addAggregation($subjectAgg);
     //get journal aggregation
     $journalAgg = new Aggregation\Terms('journals');
     $journalAgg->setField('journal.title.raw');
     $journalAgg->setOrder('_term', 'asc');
     $journalAgg->setSize(0);
     $searchQuery->addAggregation($journalAgg);
     $localeAgg = new Aggregation\Terms('locales');
     $localeAgg->setField('translations.locale');
     $localeAgg->setOrder('_term', 'asc');
     $localeAgg->setSize(0);
     $searchQuery->addAggregation($localeAgg);
     $publisherAgg = new Aggregation\Terms('publishers');
     $publisherAgg->setField('publisher.name.raw');
     $publisherAgg->setOrder('_term', 'asc');
     $publisherAgg->setSize(0);
     $searchQuery->addAggregation($publisherAgg);
     $indexAgg = new Aggregation\Terms('indexes');
     $indexAgg->setField('journalIndexs.index.name.raw');
     $indexAgg->setOrder('_term', 'asc');
     $indexAgg->setSize(0);
     $searchQuery->addAggregation($indexAgg);
     /**
      * @var ResultSet $resultData
      */
     $resultData = $searcher->search($searchQuery);
     $roles = $resultData->getAggregation('roles')['buckets'];
     $subjects = $resultData->getAggregation('subjects')['buckets'];
     $journals = $resultData->getAggregation('journals')['buckets'];
     $locales = $resultData->getAggregation('locales')['buckets'];
     $publishers = $resultData->getAggregation('publishers')['buckets'];
     $indexes = $resultData->getAggregation('indexes')['buckets'];
     if ($resultData->count() > 0) {
         /**
          * manipulate result data for easily use on template
          */
         $results = $searchManager->buildResultsObject($resultData, $section);
         /**
          * if search section is not defined or empty redirect to first result section
          */
         if (empty($section)) {
             $section = array_keys($results)[0];
             $redirectParams = array_merge($request->query->all(), ['section' => $section]);
             return $this->redirectToRoute('ojs_search_index', $redirectParams);
         } else {
             /**
              * if section result is empty redirect to first that have result section
              */
             if (!isset($results[$section])) {
                 foreach ($results as $resultKey => $result) {
                     if ($result['total_item'] > 0) {
                         $redirectParams = array_merge($request->query->all(), ['section' => $resultKey]);
                         return $this->redirectToRoute('ojs_search_index', $redirectParams);
                     }
                 }
             }
         }
         $adapter = new ArrayAdapter($results[$section]['data']);
         $pagerfanta = new Pagerfanta($adapter);
         $pagerfanta->setMaxPerPage(20);
         $pagerfanta->setCurrentPage($page);
         $results[$section]['data'] = $pagerfanta->getCurrentPageResults();
         /**
          * add search query to query history
          * history data stores on session
          */
         $this->addQueryToHistory($request, $query, $queryType, $resultData->count());
         $data = ['journalId' => $journalId, 'results' => $results, 'query' => $query, 'queryType' => $queryType, 'section' => $section, 'total_count' => $searchManager->getTotalHit(), 'roles' => $roles, 'subjects' => $subjects, 'locales' => $locales, 'journals' => $journals, 'publishers' => $publishers, 'indexes' => $indexes, 'role_filters' => $roleFilters, 'subject_filters' => $subjectFilters, 'journal_filters' => $journalFilters, 'locale_filters' => $localeFilters, 'publisher_filters' => $publisherFilters, 'index_filters' => $indexFilters, 'pagerfanta' => $pagerfanta, 'page' => $page];
     } else {
         $data = ['journalId' => $journalId, 'query' => $query, 'queryType' => $queryType, 'total_count' => $searchManager->getTotalHit(), 'journals' => []];
     }
     return $this->render('OjsSiteBundle:Search:index.html.twig', $data);
 }
コード例 #23
0
ファイル: SearchHandler.php プロジェクト: brycenrogers/linken
 /**
  * Perform a search based on the specified filter
  *
  * @param $typeTerms array
  * @param null $user
  * @param null $sortColumn
  * @param string $sortDirection
  * @param int $limit
  * @return array
  */
 public function filteredSearch($typeTerms, $user = null, $sortColumn = null, $sortDirection = 'desc', $limit = 50)
 {
     $query = new Query();
     $query->setFrom(0);
     $query->setSize($limit);
     $boolQuery = new BoolQuery();
     // Handle 'must' filters
     if (isset($typeTerms['must'])) {
         foreach ($typeTerms['must'] as $type => $term) {
             $termQueryType = new TermQuery([$type => $term]);
             $boolQuery->addMust($termQueryType);
         }
     }
     // Handle 'should' filters
     if (isset($typeTerms['should'])) {
         foreach ($typeTerms['should'] as $type => $term) {
             $termQueryType = new TermQuery([$type => $term]);
             $boolQuery->addShould($termQueryType);
         }
     }
     $query->setQuery($boolQuery);
     if ($user) {
         $userId = $user->id;
         $termQueryUser = new TermQuery(['user_id' => $userId]);
         $boolQuery->addMust($termQueryUser);
     }
     if ($sortColumn) {
         $query->addSort([$sortColumn => ['order' => $sortDirection]]);
     }
     $resultSet = $this->getTypedIndex()->search($query);
     return $this->getModels($resultSet);
 }
コード例 #24
0
 /**
  * @param string[] $fields
  * @param string[] $nearMatchFields
  * @param string $queryString
  * @param string $nearMatchQuery
  * @return \Elastica\Query\Simple|\Elastica\Query\BoolQuery
  */
 private function buildSearchTextQuery(array $fields, array $nearMatchFields, $queryString, $nearMatchQuery)
 {
     $queryForMostFields = $this->buildSearchTextQueryForFields($fields, $queryString, $this->config->getElement('CirrusSearchPhraseSlop', 'default'), false);
     if ($nearMatchQuery) {
         // Build one query for the full text fields and one for the near match fields so that
         // the near match can run unescaped.
         $BoolQuery = new \Elastica\Query\BoolQuery();
         $BoolQuery->setMinimumNumberShouldMatch(1);
         $BoolQuery->addShould($queryForMostFields);
         $nearMatch = new \Elastica\Query\MultiMatch();
         $nearMatch->setFields($nearMatchFields);
         $nearMatch->setQuery($nearMatchQuery);
         $BoolQuery->addShould($nearMatch);
         return $BoolQuery;
     }
     return $queryForMostFields;
 }
コード例 #25
0
 /**
  * Set the type of match for the query and then add it to the bool container.
  *
  * @param QueryContract $query
  * @param Bool $container
  * @return Bool
  */
 protected function addQueryToContainer(QueryContract $query, BoolQuery $container)
 {
     switch ($query->getType()) {
         case "must":
             $container->addMust($query->getQuery());
             break;
         case "should":
             $container->addShould($query->getQuery());
             break;
         case "must_not":
             $container->addMustNot($query->getQuery());
             break;
     }
     return $container;
 }
コード例 #26
0
 /**
  * getQuery
  *
  * @access private
  * @return \Elastica\Query\AbstractQuery
  */
 private function getQuery()
 {
     if (!$this->queryList) {
         return null;
     }
     if (count($this->queryList) == 1) {
         return current($this->queryList);
     }
     $query = new ElasticaQuery\BoolQuery();
     foreach ($this->queryList as $tmpQuery) {
         $query->addMust($tmpQuery);
     }
     return $query;
 }