Пример #1
0
 public function invoke(ElasticSearch $elastic)
 {
     $res = $elastic->analyze($this->in->getArgument('analyzer'), $this->in->getArgument('query'));
     foreach ($res['tokens'] as $token) {
         $this->out->writeln("{$token['type']} {$token['token']}");
     }
 }
 /**
  * @param Entity|IIndexable $entity
  */
 public function indexEntity(Entity $entity)
 {
     $data = $entity->getIndexData();
     if ($data !== FALSE) {
         $this->es->addToIndex($entity->getShortEntityName(), (int) $entity->id, $data);
     }
 }
 public function invoke(ElasticSearch $elastic, Paths $paths)
 {
     $raw = file_get_contents($paths->getApp() . '/config/synonyms.esn');
     $output = [];
     foreach (explode("\n", $raw) as $line) {
         $line = preg_replace('~\\s*#.*$~m', '', $line);
         // remove comments
         if (!$line) {
             continue;
         }
         $parts = preg_split('~\\s*(=>|,)\\s*~', $line, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
         foreach ($parts as &$part) {
             if ($part === '=>' || $part === ',') {
                 continue;
             }
             $words = [];
             foreach ($elastic->analyze('synonyms_compiler', $part)['tokens'] as $token) {
                 $words[] = $token['token'];
             }
             $part = implode(' ', $words);
         }
         $output[] = '- "' . implode(' ', $parts) . '"';
     }
     $content = "# Do not edit this file directly! Instead,\n";
     $content .= "# edit synonyms.esn and run elasticsearch:build-synonyms command.\n\n";
     $content .= str_replace(' ,', ',', implode("\n", $output)) . "\n";
     file_put_contents($paths->getApp() . '/config/synonyms.compiled.neon', $content);
     $this->out->writeln('<info>Synonyms compiled</info>');
 }
 public function registerEvents(Events $events)
 {
     $events->addCallbackListener($events::PERSIST_AFTER, function (EventArguments $args) {
         /** @var IIndexable|Entity $e */
         $e = $args->entity;
         $data = $e->getIndexData();
         if ($data === FALSE) {
             try {
                 $this->elastic->removeFromIndex($this->getShortEntityName(), (int) $e->id);
             } catch (Missing404Exception $e) {
                 // entity was yet not in index
             }
         } else {
             $this->elastic->addToIndex($this->getShortEntityName(), (int) $e->id, $data);
         }
     });
 }
Пример #5
0
 private static function getRegex()
 {
     if (!self::$regex) {
         $words = [''];
         // empty word to merge immediate tokens
         foreach (ElasticSearch::getStopwords() as $word) {
             $words[] = preg_quote($word, '~');
         }
         self::$regex = '~' . preg_quote(self::END, '~') . '(\\W*\\s*(' . implode('|', $words) . ')\\s*\\W*)' . preg_quote(self::START, '~') . '~';
     }
     return self::$regex;
 }
Пример #6
0
 public function invoke(ElasticSearch $es)
 {
     $es->setupIndices();
     $this->out->writeln('<info>Indices recreated</info>');
 }