Ejemplo n.º 1
0
 public function getItems()
 {
     if (!$this->items) {
         $this->items = array();
         //Get Status of ElasticSearch Server
         $status = $this->elasticaClient->getStatus();
         $this->status = $status->getServerStatus();
         // Get all Indexes with types
         $indexes = $status->getIndexNames();
         $index = $this->elasticaClient->getIndex(ElasticSearchConfig::getIndexName());
         if ($index->exists()) {
             // First we get all types
             $dispatcher = JDispatcher::getInstance();
             JPluginHelper::importPlugin('elasticsearch');
             $types = $dispatcher->trigger('onElasticSearchType', array());
             // Prepare an array index by type name
             foreach ($types as $type) {
                 $this->items[$type['type']] = array();
             }
             $mapping = $index->getMapping();
             //  Add language by type. Example : array('article' => array('en','fr'))
             $lang = array();
             // Array to count doc by type
             $counts = array();
             //boost
             $boosts = array();
             foreach ($mapping[ElasticSearchConfig::getIndexName()] as $key => $map) {
                 $elasticType = $index->getType($key);
                 $mapping = $elasticType->getMapping();
                 $pos = strrpos($key, '_');
                 if ($pos) {
                     // Add the language
                     $type_base = substr($key, 0, $pos);
                     $type_lang = substr($key, $pos + 1);
                     $lang[$type_base][] = $type_lang;
                     // Count
                     $sum = array_key_exists($type_base, $counts) ? $counts[$type_base] : 0;
                     $counts[$type_base] = $sum + $elasticType->count('');
                     $boosts[$type_base] = $mapping[$key]["_boost"]["null_value"];
                     if (!array_key_exists($type_base, $mapping[ElasticSearchConfig::getIndexName()])) {
                         $mapping[ElasticSearchConfig::getIndexName()][$type_base];
                     }
                 } else {
                     $boosts[$key] = $mapping[$key]["_boost"]["null_value"];
                 }
             }
             // Get all types of the index
             foreach ($this->items as $key => $map) {
                 $elasticType = $index->getType($key);
                 $count = $elasticType->count('');
                 $langs = array_key_exists($key, $lang) ? $lang[$key] : array();
                 $total = array_key_exists($key, $lang) ? $counts[$key] + $count : $count;
                 $boost = array_key_exists($key, $boosts) ? $boosts[$key] : 1.0;
                 $this->items[$key] = array('name' => $key, 'count' => $total, 'lang' => $langs, "boost" => $boost);
             }
         }
     }
     return $this->items;
 }
Ejemplo n.º 2
0
 function display($tpl = null)
 {
     $elasticaClient = ElasticSearchConfig::getElasticSearchClient();
     $index = $elasticaClient->getIndex(ElasticSearchConfig::getIndexName());
     $index->delete();
     // Display the template
     parent::display($tpl);
 }
Ejemplo n.º 3
0
 public static function truncateHighLight($text, $limit)
 {
     preg_match('/' . preg_quote(ElasticSearchConfig::getHighLigthPre()) . '/', $text, $matches, PREG_OFFSET_CAPTURE);
     if ($matches) {
         $posPreTag = $matches[0][1];
         //If pos if at the beginning it is not necessary to truncate the left part
         if ($posPreTag > $limit * 0.75) {
             $pos = strpos($text, " ", $posPreTag - $limit * 0.3);
             $text = substr($text, $pos);
             $text = '... ' . $text;
         }
         return JHtmlString::truncate($text, $limit + 4, true, true);
     }
     return $text;
 }
Ejemplo n.º 4
0
 /**
  * ElasticSearch Default view display method
  * @return void
  */
 function display($tpl = null)
 {
     // Load plug-in language files.
     JFactory::getLanguage()->load('com_finder');
     if ($this->get('isConnected')) {
         $this->items = $this->get('Items');
         $this->status = $this->get('Status');
     }
     $this->pluginStatus = $this->get('pluginState');
     $this->indexName = ElasticSearchConfig::getIndexName();
     // Set the toolbar
     $this->addToolBar();
     // Display the template
     parent::display($tpl);
 }
Ejemplo n.º 5
0
 /**
  * Main method to make a search
  */
 public function search()
 {
     $this->getSearchFields();
     $word = JRequest::getString('searchword', null, 'get');
     $offset = JRequest::getInt('start', 0, 'get');
     $limit = JRequest::getInt('limit', 10, 'get');
     if ($limit == 0) {
         // no limit
         $limit = 10000;
     }
     $this->getSearchAreas();
     $elasticaQueryString = new \Elastica\Query\QueryString();
     // Log search word only on the first page
     if ($offset == 0) {
         SearchHelper::logSearch($word);
     }
     // Convert accents
     $word = htmlentities($word, ENT_NOQUOTES, 'utf-8');
     $word = preg_replace('#\\&([A-za-z])(?:uml|circ|tilde|acute|grave|cedil|ring)\\;#', '\\1', $word);
     $word = preg_replace('#\\&([A-za-z]{2})(?:lig)\\;#', '\\1', $word);
     $word = preg_replace('#\\&[^;]+\\;#', '', $word);
     // Check if there are quotes ( for exact search )
     $exactSearch = false;
     if (strlen($word) > 1 && $word[0] == '"' && $word[strlen($word) - 1] == '"') {
         $exactSearch = true;
         $word = substr($word, 1, strlen($word) - 2);
         // Remove external "
     }
     $word = Elastica\Util::replaceBooleanWordsAndEscapeTerm($word);
     // Escape ElasticSearch specials char
     if ($exactSearch) {
         $word = '"' . $word . '"';
     }
     if ($word == "") {
         $word = "*";
     }
     $elasticaQueryString->setQuery($word);
     //Create the actual search object with some data.
     $elasticaQuery = new Elastica\Query();
     $elasticaQuery->setQuery($elasticaQueryString);
     if (ElasticSearchConfig::getHighLigthEnable()) {
         $fields = $this->getHighlightFields();
         $hlfields = array();
         foreach ($fields as $field) {
             foreach ($field as $highlight) {
                 $hlfields[] = array($highlight => array('fragment_size' => 1000, 'number_of_fragments' => 1));
             }
         }
         $highlightFields = array('pre_tags' => array(ElasticSearchConfig::getHighLigthPre()), 'post_tags' => array(ElasticSearchConfig::getHighLigthPost()), "order" => "score", 'fields' => $hlfields);
         $elasticaQuery->setHighlight($highlightFields);
     }
     // Set offset and limit for pagination
     $elasticaQuery->setFrom($offset);
     $elasticaQuery->setLimit($limit);
     //Create a filter for _type
     $elasticaFilterype = $this->createFilterType($this->areas);
     // Add filter to the search object.
     $elasticaQuery->setFilter($elasticaFilterype);
     //Search on the index.
     $elasticaResultSet = $this->elasticaIndex->search($elasticaQuery);
     $this->results = $elasticaResultSet->getResults();
     $this->totalHits = $elasticaResultSet->getTotalHits();
 }
Ejemplo n.º 6
0
 protected function typeExist($type)
 {
     $mapping = $this->elasticaIndex->getMapping();
     foreach ($mapping[ElasticSearchConfig::getIndexName()] as $key => $map) {
         if ($key == $type) {
             return true;
         }
     }
     return false;
 }
Ejemplo n.º 7
0
 public static function getElasticSearchClient()
 {
     $elasticaClient = new \Elastica\Client(array('host' => ElasticSearchConfig::getHostServer(), 'port' => ElasticSearchConfig::getPortServer()));
     return $elasticaClient;
 }