/**
  * @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;
 }