Example #1
0
 /**
  * @param $query
  * @return array
  */
 public function search($query, $filters = [], $types = null, $fields = null)
 {
     $request = new SearchRequest($this->container, $this->em, $this->util);
     //prepare for searching
     $request->parseSearchExpression($query);
     //check if there ar not to many conditions
     if (!$request->hasToManyExpressions()) {
         //search
         $results = $request->search($types, $fields);
         //get searchwords of request
         $words = $request->getWords();
         //check filters
         $resultResources = array();
         foreach ($results as $result) {
             $resultResources[] = $result;
             //                $allFilters = true;
             //                foreach ($filters as $filter) {
             //                    if(!$filter->isGranted($result)){
             //                        $allFilters = false;
             //                        break;
             //                    }
             //                }
             //                if($allFilters){
             //                    $resultResources[] = $result;
             //                }
         }
         //return searchResult
         if (empty($resultResources)) {
             return [];
         }
         $searchResult = new SearchResult();
         $searchResult->setWords($words);
         $searchResult->setResources($resultResources);
         return $searchResult;
     } else {
         throw new SearchEngineException('There are to many AND/OR expressions');
     }
 }
Example #2
0
 public function search($query, $filters = [], $entities = null, $fields = null)
 {
     $client = Elasticsearch\ClientBuilder::create()->build();
     //get fields in which we want to search
     $fields = $this->getFields($entities, $fields);
     //add fields an the query to params body
     $params = ['body' => ["query" => ["query_string" => ["fields" => $fields, 'query' => $query]]]];
     //the client does the searching
     $results = $client->search($params);
     $resultResources = [];
     //if there are results extract bundle, entity an resourceId to get the real resource we can display
     if (!empty($results['hits']['hits'])) {
         foreach ($results['hits']['hits'] as $hit) {
             $resId = $hit['_id'];
             $resBundle = explode('_', $hit['_index']);
             foreach ($resBundle as &$element) {
                 $element = ucfirst($element);
             }
             $resBundle = implode('', $resBundle);
             $resEntity = $hit['_type'];
             $resource = $this->em->getRepository($resBundle . ':' . ucfirst($resEntity))->find($resId);
             $resultResources[] = $resource;
         }
     } else {
         return $resultResources;
     }
     //return results and search words
     $words = preg_replace('(AND|OR)', '', $query);
     $words = $this->util->searchSimplify($words);
     $words = explode(' ', $words);
     array_unshift($words, $query);
     $searchResult = new SearchResult();
     $searchResult->setWords($words);
     $searchResult->setResources($resultResources);
     return $searchResult;
 }