/**
  * @param $queryString
  * @param int $offset
  * @param int $limit
  * @return null|SugarSeachEngineElasticResultSet
  */
 public function search($queryString, $offset = 0, $limit = 20, $options = array())
 {
     if (self::isSearchEngineDown()) {
         return null;
     }
     $appendWildcard = false;
     if (!empty($options['append_wildcard']) && $this->canAppendWildcard($queryString)) {
         $appendWildcard = true;
     }
     $queryString = DBManagerFactory::getInstance()->sqlLikeString($queryString, self::WILDCARD_CHAR, $appendWildcard);
     $this->logger->info("Going to search with query {$queryString}");
     $results = null;
     try {
         $queryObj = $this->buildQueryObject($queryString, $options);
         $s = new \Elastica\Search($this->_client);
         $finalTypes = array();
         if (!empty($options['moduleFilter'])) {
             foreach ($options['moduleFilter'] as $moduleName) {
                 // only add the module to the list if it can be viewed
                 if ($this->checkAccess($moduleName)) {
                     $finalTypes[] = $moduleName;
                 }
             }
             if (!empty($finalTypes)) {
                 $s->addTypes($finalTypes);
             }
         }
         $s->addIndices($this->getReadIndices($finalTypes));
         // main filter
         $mainFilter = $this->constructMainFilter($finalTypes, $options);
         $query = new \Elastica\Query($queryObj);
         $query->setFilter($mainFilter);
         if (isset($options['sort']) && is_array($options['sort'])) {
             foreach ($options['sort'] as $sort) {
                 $query->addSort($sort);
             }
         }
         $query->setParam('from', $offset);
         // set query highlight
         $fields = $this->getSearchFields($options, false);
         $highlighArray = $this->constructHighlightArray($fields, $options);
         $query->setHighlight($highlighArray);
         // add facets
         $this->addFacets($query, $options, $mainFilter);
         $esResultSet = $s->search($query, $limit);
         $results = new SugarSeachEngineElasticResultSet($esResultSet);
     } catch (Exception $e) {
         $this->reportException("Unable to perform search", $e);
         $this->checkException($e);
         return null;
     }
     return $results;
 }
Example #2
0
 /**
  * method to search log details
  *
  * @param string $operator    the operator for the query
  * @param string $words       the words
  * @param string $names_types the types to search
  *
  * @return \Elastica\ResultSet
  */
 function searchQueryLogDetails($operator, $words, $names_types = null)
 {
     $words = CmbString::normalizeUtf8(stripcslashes($words));
     // Define a Query. We want a string query.
     $elasticaQueryString = new QueryString();
     //'And' or 'Or' default : 'Or'
     $elasticaQueryString->setDefaultOperator($operator);
     $elasticaQueryString->setQuery($words);
     // Create the actual search object with some data.
     $elasticaQuery = new Query();
     $elasticaQuery->setQuery($elasticaQueryString);
     //Search on the index.
     $index = CAppUI::conf("search index_name") . "_log";
     $this->_index = $this->loadIndex($index);
     $search = new \Elastica\Search($this->_client);
     $search->addIndex($this->_index);
     if ($names_types) {
         $search->addTypes($names_types);
     }
     $elasticaQuery->setFrom(0);
     // Where to start
     $elasticaQuery->setLimit(1000);
     return $search->search($elasticaQuery);
 }
Example #3
0
 /**
  * Méthode utilisée pour la loupe dans les recherches classique qui affiche les volets avec les résultats de chaque type.
  *
  * @param string $words Les mots recherchés
  * @param string $name  Le nom de l'index
  * @param array  $types Les types sur lesquels on effectue la recherche
  *
  * @return \Elastica\ResultSet
  */
 function queryByType($words, $name, $types)
 {
     // La query de recherche
     $query_words = new Elastica\Query\QueryString();
     $query_words->setQuery($words);
     $query_words->setFields(array("body", "title"));
     $query_words->setDefaultOperator("and");
     // Aggregation par type
     $agg_by_type = new CSearchAggregation("Terms", "ref_type", "_type", 100);
     $query = new Query($query_words);
     $query->addAggregation($agg_by_type->_aggregation);
     //Search on the index.
     $this->_index = $this->loadIndex($name);
     $search = new \Elastica\Search($this->_client);
     $search->addIndex($this->_index);
     $search->addTypes($types);
     return $search->search($query);
 }