Example #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_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!");
 }
Example #3
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!");
 }
Example #4
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_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_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_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!");
 }
 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);
 }
Example #9
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!');
 }
Example #10
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!');
     }
 }
Example #12
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;
 }
 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!");
 }
Example #14
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');
 }
Example #15
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();
 }
Example #16
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;
 }
Example #17
0
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:'******'current mapping:');
        dump($currentMapping);
        if (!$currentMapping) {
<?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();
Example #19
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;
 }
Example #20
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;
}
<?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);
}
Example #22
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.';