示例#1
1
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $elasticaClient = new \Elastica\Client();
     $elasticaIndex = $elasticaClient->getIndex('evrika');
     $elasticaIndex->create(array('number_of_shards' => 2, 'number_of_replicas' => 0, 'analysis' => array('analyzer' => array('indexAnalyzer' => array('type' => 'custom', 'tokenizer' => 'standard', 'filter' => array('lowercase', 'mySnowball')), 'searchAnalyzer' => array('type' => 'custom', 'tokenizer' => 'standard', 'filter' => array('standard', 'lowercase', 'mySnowball'))), 'filter' => array('mySnowball' => array('type' => 'snowball', 'language' => 'Russian')))), true);
     $output->writeln('+++ evrika:elastic_index created!');
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     ini_set('memory_limit', -1);
     $output->writeln('--- vidal:autocomplete_clinic started');
     $em = $this->getContainer()->get('doctrine')->getManager('drug');
     $names = $em->getRepository('VidalDrugBundle:ClPhGroups')->getNames();
     $elasticaClient = new \Elastica\Client();
     $elasticaIndex = $elasticaClient->getIndex('website');
     $elasticaType = $elasticaIndex->getType('autocomplete_clinic');
     # delete if exists
     if ($elasticaType->exists()) {
         $elasticaType->delete();
     }
     # Define mapping
     $mapping = new \Elastica\Type\Mapping();
     $mapping->setType($elasticaType);
     # Set mapping
     $mapping->setProperties(array('id' => array('type' => 'integer', 'include_in_all' => FALSE), 'name' => array('type' => 'string', 'include_in_all' => TRUE)));
     # Send mapping to type
     $mapping->send();
     # записываем на сервер документы автодополнения
     $documents = array();
     for ($i = 0, $c = count($names); $i < $c; $i++) {
         $documents[] = new \Elastica\Document($i + 1, array('name' => $names[$i]));
         if ($i && $i % 500 == 0) {
             $elasticaType->addDocuments($documents);
             $elasticaType->getIndex()->refresh();
             $documents = array();
             $output->writeln("... + {$i}");
         }
     }
     $elasticaType->addDocuments($documents);
     $elasticaType->getIndex()->refresh();
     $output->writeln("+++ vidal:autocomplete_clinic loaded {$i} documents!");
 }
示例#3
0
 public function createIndex()
 {
     // Remove old unfinished indices
     foreach ($this->_client->getStatus()->getIndicesWithAlias($this->getIndex()->getName() . '.tmp') as $index) {
         /** @var Elastica\Index $index */
         $index->delete();
     }
     // Set current index to read-only
     foreach ($this->_client->getStatus()->getIndicesWithAlias($this->getIndex()->getName()) as $index) {
         $index->getSettings()->setBlocksWrite(true);
     }
     // Create new index and switch alias
     $version = time();
     /** @var $indexNew CM_Elasticsearch_Type_Abstract */
     $indexNew = new static($this->_client, $version);
     $indexNew->_createIndex(true);
     $indexNew->getIndex()->addAlias($this->getIndex()->getName() . '.tmp');
     $settings = $indexNew->getIndex()->getSettings();
     $refreshInterval = $settings->getRefreshInterval();
     //$mergeFactor = $settings->getMergePolicy('merge_factor');
     //$settings->setMergePolicy('merge_factor', 50);
     $settings->setRefreshInterval('-1');
     $indexNew->update(null, true);
     //$settings->setMergePolicy('merge_factor', $mergeFactor);
     $settings->setRefreshInterval($refreshInterval);
     $indexNew->getIndex()->addAlias($this->getIndex()->getName());
     $indexNew->getIndex()->removeAlias($this->getIndex()->getName() . '.tmp');
     // Remove old index
     foreach ($this->_client->getStatus()->getIndicesWithAlias($this->getIndex()->getName()) as $index) {
         /** @var Elastica\Index $index */
         if ($index->getName() != $indexNew->getIndex()->getName()) {
             $index->delete();
         }
     }
 }
示例#4
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     ini_set('memory_limit', -1);
     $output->writeln('--- vidal:autocomplete started');
     $em = $this->getContainer()->get('doctrine')->getManager('drug');
     $productNames = $em->getRepository('VidalDrugBundle:Product')->findAutocomplete();
     $moleculeNames = $em->getRepository('VidalDrugBundle:Molecule')->findAutocomplete();
     $companyNames = $em->getRepository('VidalDrugBundle:Company')->findAutocomplete();
     $atcNames = $em->getRepository('VidalDrugBundle:ATC')->findAutocomplete();
     $elasticaClient = new \Elastica\Client();
     $elasticaIndex = $elasticaClient->getIndex('website');
     $elasticaType = $elasticaIndex->getType('autocomplete');
     if ($elasticaType->exists()) {
         $elasticaType->delete();
     }
     // Define mapping
     $mapping = new \Elastica\Type\Mapping();
     $mapping->setType($elasticaType);
     // Set mapping
     $mapping->setProperties(array('name' => array('type' => 'string', 'include_in_all' => TRUE), 'type' => array('type' => 'string', 'include_in_all' => FALSE)));
     // Send mapping to type
     $mapping->send();
     # записываем в ElasticSearch документы автодополнения
     $this->save($productNames, 'product', $output, $elasticaType);
     $this->save($moleculeNames, 'molecule', $output, $elasticaType);
     $this->save($companyNames, 'company', $output, $elasticaType);
     $this->save($atcNames, 'atc', $output, $elasticaType);
     $output->writeln("+++ vidal:autocomplete completed!");
 }
示例#5
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $output->writeln('--- veterinar:autocomplete started');
     $em = $this->getContainer()->get('doctrine')->getManager('veterinar');
     $productNames = $em->getRepository('VidalVeterinarBundle:Product')->findProductNames();
     $names = array_unique($productNames);
     sort($names);
     $elasticaClient = new \Elastica\Client();
     $elasticaIndex = $elasticaClient->getIndex('website');
     $elasticaType = $elasticaIndex->getType('veterinar_autocomplete');
     if ($elasticaType->exists()) {
         $elasticaType->delete();
     }
     // Define mapping
     $mapping = new \Elastica\Type\Mapping();
     $mapping->setType($elasticaType);
     // Set mapping
     $mapping->setProperties(array('id' => array('type' => 'integer', 'include_in_all' => FALSE), 'name' => array('type' => 'string', 'include_in_all' => TRUE)));
     // Send mapping to type
     $mapping->send();
     # записываем на сервер документы автодополнения
     $documents = array();
     for ($i = 0; $i < count($names); $i++) {
         $documents[] = new \Elastica\Document($i + 1, array('name' => $names[$i]));
         if ($i && $i % 500 == 0) {
             $elasticaType->addDocuments($documents);
             $elasticaType->getIndex()->refresh();
         }
     }
     $output->writeln("+++ veterinar:autocomplete loaded {$i} documents!");
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     ini_set('memory_limit', -1);
     $output->writeln('--- vidal:autocomplete_document started');
     $em = $this->getContainer()->get('doctrine')->getManager('drug');
     $names = array();
     $documents = $em->createQuery("\n\t\t\tSELECT d.DocumentID, d.RusName\n\t\t\tFROM VidalDrugBundle:Document d\n\t\t\tWHERE d.CountryEditionCode = 'RUS'\n\t\t\tORDER BY d.RusName ASC\n\t\t")->getResult();
     foreach ($documents as $document) {
         $names[] = $document['DocumentID'] . ' ' . $this->strip($document['RusName']);
     }
     $elasticaClient = new \Elastica\Client();
     $elasticaIndex = $elasticaClient->getIndex('website');
     $elasticaType = $elasticaIndex->getType('autocomplete_document');
     # delete if exists
     if ($elasticaType->exists()) {
         $elasticaType->delete();
     }
     # Define mapping
     $mapping = new \Elastica\Type\Mapping();
     $mapping->setType($elasticaType);
     # Set mapping
     $mapping->setProperties(array('name' => array('type' => 'string', 'include_in_all' => TRUE)));
     # Send mapping to type
     $mapping->send();
     for ($i = 0; $i < count($names); $i++) {
         $document = new \Elastica\Document(null, array('name' => $names[$i]));
         $elasticaType->addDocument($document);
         $elasticaType->getIndex()->refresh();
         if ($i && $i % 500 == 0) {
             $output->writeln("... + {$i}");
         }
     }
     $output->writeln("+++ vidal:autocomplete_document loaded {$i} documents!");
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     ini_set('memory_limit', -1);
     $output->writeln('--- vidal:autocomplete_article started');
     $em = $this->getContainer()->get('doctrine')->getManager('drug');
     $articles = $em->getRepository('VidalDrugBundle:Article')->findActive();
     $elasticaClient = new \Elastica\Client();
     $elasticaIndex = $elasticaClient->getIndex('website');
     $elasticaType = $elasticaIndex->getType('autocomplete_article');
     # delete if exists
     if ($elasticaType->exists()) {
         $elasticaType->delete();
     }
     # Define mapping
     $mapping = new \Elastica\Type\Mapping();
     $mapping->setType($elasticaType);
     # Set mapping
     $mapping->setProperties(array('title' => array('type' => 'string', 'include_in_all' => TRUE)));
     # Send mapping to type
     $mapping->send();
     # записываем на сервер документы автодополнения
     $documents = array();
     for ($i = 0; $i < count($articles); $i++) {
         $documents[] = new \Elastica\Document($i + 1, array('title' => mb_strtolower($articles[$i]->getTitle(), 'utf-8')));
         if ($i && $i % 500 == 0) {
             $elasticaType->addDocuments($documents);
             $elasticaType->getIndex()->refresh();
             $documents = array();
         }
     }
     $elasticaType->addDocuments($documents);
     $elasticaType->getIndex()->refresh();
     $output->writeln("+++ vidal:autocomplete_article completed!");
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     ini_set('memory_limit', -1);
     $output->writeln('--- vidal:autocomplete_product started');
     $em = $this->getContainer()->get('doctrine')->getManager('drug');
     $names = array();
     $products = $em->createQuery("\n\t\t\tSELECT p.ProductID, p.RusName, p.ZipInfo\n\t\t\tFROM VidalDrugBundle:Product p\n\t\t\tWHERE p.MarketStatusID IN (1,2,7)\n\t\t\t\tAND p.ProductTypeCode IN ('DRUG','GOME')\n\t\t\t\tAND p.inactive = FALSE\n\t\t\tORDER BY p.RusName ASC\n\t\t")->getResult();
     foreach ($products as $p) {
         $names[] = $p['ProductID'] . ' ' . $this->strip($p['RusName']);
     }
     $elasticaClient = new \Elastica\Client();
     $elasticaIndex = $elasticaClient->getIndex('website');
     $elasticaType = $elasticaIndex->getType('autocomplete_product');
     # delete if exists
     if ($elasticaType->exists()) {
         $elasticaType->delete();
     }
     # Define mapping
     $mapping = new \Elastica\Type\Mapping();
     $mapping->setType($elasticaType);
     # Set mapping
     $mapping->setProperties(array('name' => array('type' => 'string', 'include_in_all' => TRUE)));
     # Send mapping to type
     $mapping->send();
     $total = count($products);
     for ($i = 0; $i < $total; $i++) {
         $id = $products[$i]['ProductID'];
         $name = $this->strip($products[$i]['RusName']) . ' ' . $id;
         $document = new \Elastica\Document($id, array('name' => $name));
         $elasticaType->addDocument($document);
         $elasticaType->getIndex()->refresh();
     }
     $output->writeln("+++ vidal:autocomplete_product loaded {$i} documents!");
 }
 public function getElasticSearchDao($configFilePath, $nodeName)
 {
     $config = $this->readConfigFromFile($configFilePath, $nodeName);
     $elasticaClient = new \Elastica\Client(['host' => $config['host'], 'port' => $config['port']]);
     $elasticaIndex = $elasticaClient->getIndex($config['index']);
     $elasticaType = $elasticaIndex->getType($config['type']);
     return new ElasticSearchDao($elasticaType);
 }
示例#10
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     ini_set('memory_limit', -1);
     $output->writeln('--- vidal:autocomplete_base started');
     $elasticaClient = new \Elastica\Client();
     $elasticaIndex = $elasticaClient->getIndex('website');
     $elasticaIndex->create(array('number_of_shards' => 4, 'number_of_replicas' => 1, 'analysis' => array('analyzer' => array('default' => array('tokenizer' => 'whitespace')))), true);
     $output->writeln('+++ vidal:autocomplete_base completed!');
 }
示例#11
0
 protected function getLoanIndex()
 {
     if ($this->loanIndex) {
         return $this->loanIndex;
     }
     $elasticaClient = new \Elastica\Client();
     $loanIndex = $elasticaClient->getIndex('loans');
     if (!$loanIndex->exists()) {
         $loanIndex->create(array('number_of_shards' => 1, 'number_of_replicas' => 1, 'analysis' => array('analyzer' => array('default_index' => array('type' => 'custom', 'tokenizer' => 'standard', 'filter' => array('lowercase')), 'default_search' => array('type' => 'custom', 'tokenizer' => 'standard', 'filter' => array('standard', 'lowercase'))))));
     }
     $this->loanIndex = $loanIndex;
     return $loanIndex;
 }
 public function init()
 {
     $conf = $this->configurationManager->getConfiguration(\TYPO3\Flow\Configuration\ConfigurationManager::CONFIGURATION_TYPE_SETTINGS, 'WMDB.Forger.Elasticsearch.Credentials');
     if (is_array($conf) && count($conf) > 0 && isset($conf['host']) && isset($conf['port']) && isset($conf['path']) && isset($conf['transport'])) {
         $elasticClientConfiguration = array('host' => $conf['host'], 'port' => $conf['port'], 'path' => $conf['path'], 'transport' => $conf['transport']);
         if (isset($conf['username']) && isset($conf['password'])) {
             $elasticClientConfiguration['headers'] = array('Authorization' => 'Basic ' . base64_encode($conf['username'] . ':' . $conf['password']) . '==');
         }
         $elasticaClient = new \Elastica\Client($elasticClientConfiguration);
         $this->index = $elasticaClient->getIndex($conf['index']);
     } else {
         throw new Exception('Could not load elastic-credentials! Please check your setting.yaml!');
     }
 }
示例#13
0
 public function testConnection()
 {
     $count = 0;
     $config = array('connectionStrategy' => function ($connections) use(&$count) {
         ++$count;
         return current($connections);
     });
     $client = new \Elastica\Client($config);
     $resonse = $client->request('/_aliases');
     $this->assertEquals(1, $count);
     $this->assertTrue($resonse->isOk());
     $strategy = $client->getConnectionStrategy();
     $condition = $strategy instanceof CallbackStrategy;
     $this->assertTrue($condition);
 }
示例#14
0
 public function query($type, $q)
 {
     $q = $this->linguaService->stem_string($q);
     $client = new \Elastica\Client();
     $index = $client->getIndex('website');
     $type = $index->getType($type);
     $query = array('query' => array('query_string' => array('query' => $q . '*', 'analyzer' => 'ru')));
     $path = $index->getName() . '/' . $type->getName() . '/_search';
     $response = $client->request($path, \Elastica\Request::GET, $query);
     $responseArray = $response->getData();
     $results = array();
     foreach ($responseArray['hits']['hits'] as $result) {
         $results[] = $result['_source'];
     }
     return $results;
 }
示例#15
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     ini_set('memory_limit', -1);
     $output->writeln('--- evrika:elastic_autocomlete started');
     $em = $this->getContainer()->get('doctrine')->getManager();
     $publications = $em->getRepository('EvrikaMainBundle:Publication')->findAutocomplete();
     $events = $em->getRepository('EvrikaMainBundle:Event')->findAutocomplete();
     for ($i = 0; $i < count($publications); $i++) {
         $publications[$i]['title'] = trim($publications[$i]['title']);
         $publications[$i]['type'] = 'publication';
     }
     for ($i = 0; $i < count($events); $i++) {
         $events[$i]['title'] = trim($events[$i]['title']);
         $events[$i]['type'] = 'event';
     }
     $articles = array_merge($publications, $events);
     usort($articles, function ($a, $b) {
         return strcmp(trim($a['title'], '"'), trim($b['title'], '"'));
     });
     $elasticaClient = new \Elastica\Client();
     $elasticaIndex = $elasticaClient->getIndex('evrika');
     $elasticaType = $elasticaIndex->getType('autocomplete');
     if ($elasticaType->exists()) {
         $elasticaType->delete();
     }
     // Define mapping
     $mapping = new \Elastica\Type\Mapping();
     $mapping->setType($elasticaType);
     // Set mapping
     $mapping->setProperties(array('id' => array('type' => 'integer', 'include_in_all' => false), 'type' => array('type' => 'string', 'include_in_all' => false), 'title' => array('type' => 'string', 'include_in_all' => true, 'index_analyzer' => 'indexAnalyzer', 'search_analyzer' => 'searchAnalyzer')));
     // Send mapping to type
     $mapping->send();
     # записываем на сервер документы автодополнения
     $documents = array();
     $count = count($articles);
     for ($i = 0; $i < $count; $i++) {
         $index = $articles[$i]['type'] == 'event' ? $articles[$i]['id'] + 999999 : $articles[$i]['id'];
         $documents[] = new \Elastica\Document($index, array('id' => $articles[$i]['id'], 'type' => $articles[$i]['type'], 'title' => $articles[$i]['title']));
         if ($i && $i % 100 == 0) {
             $elasticaType->addDocuments($documents);
             $elasticaType->getIndex()->refresh();
             $documents = array();
         }
     }
     $output->writeln("+++ evrika:elastic_autocomlete completed!");
 }
示例#16
0
文件: App.php 项目: bd808/quips
 /**
  * Configure inversion of control/dependency injection container.
  *
  * @param \Slim\Helper\Set $container IOC container
  */
 protected function configureIoc(\Slim\Helper\Set $container)
 {
     $container->singleton('i18nCache', function ($c) {
         return new JsonCache($c->settings['i18n.path'], $c->log);
     });
     $container->singleton('i18nContext', function ($c) {
         return new I18nContext($c->i18nCache, $c->settings['i18n.default'], $c->log);
     });
     $container->singleton('mailer', function ($c) {
         return new Mailer(array('Host' => $c->settings['smtp.host']), $c->log);
     });
     $container->singleton('parsoid', function ($c) {
         return new ParsoidClient($c->settings['parsoid.url'], $c->settings['parsoid.cache'], $c->log);
     });
     $container->singleton('quips', function ($c) {
         $settings = array('url' => $c->settings['es.url'], 'log' => true);
         if ($c->settings['es.user'] !== '') {
             $creds = base64_encode($c->settings['es.user'] . ':' . $c->settings['es.password']);
             $settings['headers'] = array('Authorization' => "Basic {$creds}");
         }
         $client = new \Elastica\Client($settings);
         $client->setLogger($c->log);
         return new Quips($client, $c->log);
     });
     $container->singleton('oauthConfig', function ($c) {
         $conf = new \MediaWiki\OAuthClient\ClientConfig($c->settings['oauth.endpoint']);
         $conf->setRedirURL($c->settings['oauth.redir']);
         $conf->setConsumer(new \MediaWiki\OAuthClient\Consumer($c->settings['oauth.consumer_token'], $c->settings['oauth.secret_token']));
         return $conf;
     });
     $container->singleton('oauthClient', function ($c) {
         $client = new \MediaWiki\OAuthClient\Client($c->oauthConfig, $c->log);
         $client->setCallback($c->settings['oauth.callback']);
         return $client;
     });
     $container->singleton('userManager', function ($c) {
         return new OAuthUserManager($c->oauthClient, $c->log);
     });
     $container->singleton('authManager', function ($c) {
         return new AuthManager($c->userManager);
     });
     // TODO: figure out where to send logs
 }
示例#17
0
 /**
  * Use Elasticsearch bulk API to send list of documents
  * @param  array             $documents
  * @throws \RuntimeException
  */
 protected function bulkSend(array $documents)
 {
     try {
         $this->client->addDocuments($documents);
     } catch (Elastica\Exception\ExceptionInterface $e) {
         if (!$this->options['ignore_error']) {
             throw new RuntimeException("Error sending messages to Elasticsearch", 0, $e);
         }
     }
 }
示例#18
0
 public function searchindexAction($searchTerm)
 {
     $client = new \Elastica\Client(array('host' => '192.168.15.102', 'port' => 9200));
     $index = $client->getIndex('coolcities');
     $multiMatchQuery = new \Elastica\Query\MultiMatch();
     $multiMatchQuery->setQuery($searchTerm);
     $multiMatchQuery->setFields(array('title', 'description', 'city^3'));
     // Create the actual search object with some data.
     $esQuery = new \Elastica\Query();
     $esQuery->setQuery($multiMatchQuery);
     //Search on the index.
     $results = $index->search($esQuery);
     foreach ($results as $result) {
         echo "<pre>";
         print_r($result);
         echo "<pre>";
     }
     die;
     return new Response('index search');
 }
示例#19
0
<?php

$testdata_file = $argv[1];
if (!file_exists($testdata_file)) {
    echo "Not found {$testdata_file}\n";
    exit;
}
require './vendor/autoload.php';
$client = new Elastica\Client(array('url' => 'http://localhost:9200/'));
$client->setConfig(array('timeout' => 10));
$client->setConfig(array('connections' => array('timeout' => 10)));
$chat_type = $client->getIndex('cw')->getType('chat');
while (1) {
    $fp = fopen($testdata_file, 'r');
    $insert_cnt = isset($argv[2]) ? $argv[2] : 100;
    if ($fp) {
        $documents = array();
        while (!feof($fp)) {
            $text = fgets($fp);
            if (!isset($text)) {
                continue;
            }
            $data = array('message' => $text, 'rid' => rand(1, 30), 'aid' => rand(1, 50), 'create_date' => date('Y-m-d H:i:s'));
            $documents[] = new Elastica\Document('', $data);
            if (count($documents) >= $insert_cnt) {
                try {
                    $start_time = microtime(true);
                    $chat_type->addDocuments($documents);
                    $end_time = microtime(true);
                    echo "###### Time : " . ($end_time - $start_time) . "\n";
                    echo "###### (insert " . count($documents) . " data) \n";
<?php

namespace elasticsearch;

add_action('nhp-opts-options-validate-elasticsearch', function ($new, $current) {
    global $NHP_Options;
    if ($new['server_url'] == $current['server_url'] && $new['server_index'] == $current['server_index']) {
        return;
    }
    if ($new['server_url']) {
        $client = new \Elastica\Client(array('url' => $new['server_url']));
        try {
            $index = $client->getIndex($new['server_index']);
            $status = $index->getStats()->getResponse()->getData();
        } catch (\Elastica\Exception\ResponseException $ex) {
            // This kind usually means there was an issue with the index not existing, so we'll associate the message to that field.
            $field = $NHP_Options->sections['server']['fields']['server_index'];
            $field['msg'] = __($ex->getMessage());
            $NHP_Options->errors[] = $field;
            set_transient('nhp-opts-errors-elasticsearch', $NHP_Options->errors, 1000);
            return;
        } catch (\Exception $ex) {
            $field = $NHP_Options->sections['server']['fields']['server_url'];
            $field['msg'] = __($ex->getMessage());
            $NHP_Options->errors[] = $field;
            set_transient('nhp-opts-errors-elasticsearch', $NHP_Options->errors, 1000);
            return;
        }
        try {
            $index = $client->getIndex($new['secondary_index']);
            $status = $index->getStats()->getData();
示例#21
0
 public function addArticleDocument($entity)
 {
     try {
         $elasticaClient = new \Elastica\Client();
         $elasticaIndex = $elasticaClient->getIndex('evrika');
         $elasticaType = $elasticaIndex->getType('article');
         # обновляем индекс статей
         $createdDate = $entity->isEvent() ? $entity->getStarts() : $entity->getCreated();
         $index = $entity->isEvent() ? $entity->getId() + 999999 : $entity->getId();
         $document = new \Elastica\Document($index, array('id' => $entity->getId(), 'type' => $entity->isEvent() ? 'event' : 'publication', 'title' => trim($entity->getTitle()), 'shortText' => $entity->isEvent() ? '' : trim($entity->getShortText()), 'body' => trim($entity->getBody()), 'created' => $createdDate->format('Y-m-d H:i:s')));
         $elasticaType->addDocument($document);
         $elasticaType->getIndex()->refresh();
     } catch (\Exception $e) {
         return false;
     }
     return true;
 }
示例#22
0
 private function autocompleteProduct($product)
 {
     try {
         # check product is visible
         $productTypes = array('DRUG', 'GOME');
         $marketIds = array(1, 2, 7);
         if ($product->getInactive() || !in_array($product->getProductTypeCode(), $productTypes) || !in_array($product->getMarketStatusID()->getMarketStatusID(), $marketIds)) {
             return false;
         }
         # get names
         $RusName = $this->strip($product->getRusName());
         $RusName = mb_strtolower($RusName, 'UTF-8');
         $EngName = $this->strip($product->getEngName());
         $EngName = mb_strtolower($EngName, 'UTF-8');
         $elasticaClient = new \Elastica\Client();
         $elasticaIndex = $elasticaClient->getIndex('website');
         # check if names exists
         $client = new \Elasticsearch\Client();
         $s['index'] = 'website';
         $s['type'] = 'autocomplete';
         $s['body']['size'] = 1;
         $s['body']['query']['filtered']['query']['query_string']['query'] = $RusName;
         $s['body']['query']['filtered']['query']['query_string']['fields'] = array('name', 'type');
         $s['body']['highlight']['fields']['name'] = array("fragment_size" => 100);
         $s['body']['sort']['type']['order'] = 'desc';
         $s['body']['sort']['name']['order'] = 'asc';
         $s['body']['query']['filtered']['filter']['term']['type'] = 'product';
         $results = $client->search($s);
         $totalRus = $results['hits']['total'];
         $s['body']['query']['filtered']['query']['query_string']['query'] = $EngName;
         $results = $client->search($s);
         $totalEng = $results['hits']['total'];
         # autocomplete
         $type = $elasticaIndex->getType('autocomplete');
         if ($totalRus == 0) {
             $document = new \Elastica\Document(null, array('name' => $RusName, 'type' => 'product'));
             $type->addDocument($document);
         }
         if ($totalEng == 0) {
             $document = new \Elastica\Document(null, array('name' => $EngName, 'type' => 'product'));
             $type->addDocument($document);
         }
         $type->getIndex()->refresh();
         # autocomplete_ext
         $type = $elasticaIndex->getType('autocomplete_ext');
         if ($totalRus == 0) {
             $document = new \Elastica\Document(null, array('name' => $RusName, 'type' => 'product'));
             $type->addDocument($document);
         }
         if ($totalEng == 0) {
             $document = new \Elastica\Document(null, array('name' => $EngName, 'type' => 'product'));
             $type->addDocument($document);
         }
         $type->getIndex()->refresh();
         # product
         $type = $elasticaIndex->getType('autocomplete_product');
         if ($totalRus == 0) {
             $name = $RusName . ' ' . $product->getProductID();
             $document = new \Elastica\Document(null, array('name' => $name));
             $type->addDocument($document);
         }
         $type->getIndex()->refresh();
     } catch (\Exception $e) {
     }
     return true;
 }
<?php

require './vendor/autoload.php';
$client = new Elastica\Client(array('url' => 'http://localhost:9200/'));
// Load index
$chat_type = $client->getIndex('cw')->getType('chat');
// delete all data
$query = new Elastica\Query\MatchAll();
$result_set = $chat_type->search($query);
$ids = array();
echo "Collect exists data";
while ($result = $result_set->current()) {
    $ids[] = $result->getId();
    $result_set->next();
    echo ".";
}
echo "\n";
if (!empty($ids)) {
    echo "Found " . count($ids) . " data. Deleting these data. \n";
    $chat_type->deleteIds($ids);
}
示例#24
0
 private function createAutocomplete($indexName, $id, $name)
 {
     $elasticaClient = new \Elastica\Client();
     $elasticaIndex = $elasticaClient->getIndex('website');
     $elasticaType = $elasticaIndex->getType($indexName);
     $document = new \Elastica\Document($id + 100000, array('name' => $name));
     $elasticaType->addDocument($document);
     $elasticaType->getIndex()->refresh();
 }
示例#25
0
 * Time: 16:11.
 */
use Elastica\Type\Mapping;
use ESGateway\DataModel\FieldMapping\MappingFactory;
require __DIR__ . '/../../bootstrap.php';
$createMappingHandler = function (\Elastica\Type $type) {
    $mapping = new Mapping();
    $mapping->setType($type);
    $mappingProperties = (new MappingFactory())->make();
    $mapping->setProperties($mappingProperties);
    $response = $mapping->send();
    dump('create default mapping');
    dump($response);
};
$options = getopt('', ['gv:', 'index:', 'createIndex:']);
$client = new \Elastica\Client(['host' => ELASTIC_SEARCH_HOST, 'port' => ELASTIC_SEARCH_PORT]);
if (isset($options['createIndex'])) {
    $indexName = trim($options['createIndex']);
    assert(is_string($indexName) && strlen($indexName) > 0 && strpos($indexName, 'farm_v') === 0, 'bad index name');
    $index = $client->getIndex($indexName);
    $exists = $index->exists();
    if (!$exists) {
        $response = $index->create();
        dump('create index');
        dump($response);
    }
    $type = $index->getType('_default_');
    call_user_func($createMappingHandler, $type);
    $gameVersionList = ['tw', 'th', 'us', 'de', 'fr', 'it', 'pl', 'nl', 'br'];
    foreach ($gameVersionList as $gameVersion) {
        $type = $index->getType('user:' . $gameVersion);
示例#26
0
 /**
  * @group unit
  */
 public function testPasswordFromClient()
 {
     $password = '******';
     $client = new \Elastica\Client(array('password' => $password));
     $this->assertEquals($password, $client->getConnection()->getPassword('password'));
 }
示例#27
0
<?php

require_once 'bootstrap.php';
define('INDEX_LABEL', 'user');
$client = new \Elastica\Client();
try {
    $index = $client->getIndex(INDEX_LABEL);
    $index->delete(array(), true);
    echo "Index " . INDEX_LABEL . " successfully dropped." . PHP_EOL;
} catch (Exception $e) {
    echo "Index " . INDEX_LABEL . " could not dropped (maybe already gone?)" . PHP_EOL;
}
try {
    $index = $client->getIndex(INDEX_LABEL);
    $index->create(array(), true);
    echo "Index " . INDEX_LABEL . " successfully created." . PHP_EOL;
} catch (Exception $e) {
    echo "Index " . INDEX_LABEL . " could not be created." . PHP_EOL;
}
示例#28
0
<?php

namespace elasticsearch;

add_action('nhp-opts-options-validate-elasticsearch', function ($new, $current) {
    global $NHP_Options;
    if ($new['server_url'] == $current['server_url'] && $new['server_index'] == $current['server_index']) {
        return;
    }
    if ($new['server_url']) {
        $client = new \Elastica\Client(array('url' => $new['server_url']));
        try {
            $index = $client->getIndex($new['server_index']);
            $status = $index->getStats()->getData();
        } catch (\Elastica\Exception\ResponseException $ex) {
            // This kind usually means there was an issue with the index not existing, so we'll associate the message to that field.
            $field = $NHP_Options->sections['server']['fields']['server_index'];
            $field['msg'] = __($ex->getMessage());
            $NHP_Options->errors[] = $field;
            set_transient('nhp-opts-errors-elasticsearch', $NHP_Options->errors, 1000);
            return;
        } catch (\Exception $ex) {
            $field = $NHP_Options->sections['server']['fields']['server_url'];
            $field['msg'] = __($ex->getMessage());
            $NHP_Options->errors[] = $field;
            set_transient('nhp-opts-errors-elasticsearch', $NHP_Options->errors, 1000);
            return;
        }
        if (empty($status) || empty($status['indices']) || empty($status['indices'][$new['server_index']])) {
            $field = $NHP_Options->sections['server']['fields']['server_url'];
            $field['msg'] = 'Unable to connect to the ElasticSearch server.';
示例#29
0
 /**
  * @group unit
  */
 public function testCompressionEnabledWithClient()
 {
     $client = new \Elastica\Client(array('connections' => array(array('compression' => true))));
     $connection = $client->getConnection();
     $this->assertTrue($connection->hasCompression());
 }