Ejemplo n.º 1
0
 /**
  * Reindex documents from an old index to a new index.
  *
  * @link https://www.elastic.co/guide/en/elasticsearch/guide/master/reindex.html
  *
  * @param \Elastica\Index $oldIndex
  * @param \Elastica\Index $newIndex
  * @param array           $options  keys: CrossIndex::OPTION_* constants
  *
  * @return \Elastica\Index The new index object
  */
 public static function reindex(Index $oldIndex, Index $newIndex, array $options = array())
 {
     // prepare search
     $search = new Search($oldIndex->getClient());
     $options = array_merge(array(self::OPTION_TYPE => null, self::OPTION_QUERY => new MatchAll(), self::OPTION_EXPIRY_TIME => '1m', self::OPTION_SIZE_PER_SHARD => 1000), $options);
     $search->addIndex($oldIndex);
     if (isset($options[self::OPTION_TYPE])) {
         $type = $options[self::OPTION_TYPE];
         $search->addTypes(is_array($type) ? $type : array($type));
     }
     $search->setQuery($options[self::OPTION_QUERY]);
     // search on old index and bulk insert in new index
     $scanAndScroll = new ScanAndScroll($search, $options[self::OPTION_EXPIRY_TIME], $options[self::OPTION_SIZE_PER_SHARD]);
     foreach ($scanAndScroll as $resultSet) {
         $bulk = new Bulk($newIndex->getClient());
         $bulk->setIndex($newIndex);
         foreach ($resultSet as $result) {
             $action = new Bulk\Action();
             $action->setType($result->getType());
             $action->setId($result->getId());
             $action->setSource($result->getData());
             $bulk->addAction($action);
         }
         $bulk->send();
     }
     $newIndex->refresh();
     return $newIndex;
 }
 /**
  * {@inheritdoc}
  * @see \Silex\ServiceProviderInterface::register()
  */
 public function register(Application $app)
 {
     $app['elastic.client'] = $app->share(function () use($app) {
         $config = $app['config']('elastic.connection', array());
         $client = new Client($config);
         return $client;
     });
     $app['elastic.bulk'] = $app->protect(function ($index) use($app) {
         $bulk = new Bulk($app['elastic.client']);
         $bulk->setIndex($index);
         return $bulk;
     });
     $app['elastic.document'] = $app->protect(function ($type, array $data) {
         $document = new Document();
         $document->setType($type);
         $document->setData($data);
         return $document;
     });
 }
Ejemplo n.º 3
0
 /**
  * Bulk operation
  *
  * Every entry in the params array has to exactly on array
  * of the bulk operation. An example param array would be:
  *
  * array(
  *         array('index' => array('_index' => 'test', '_type' => 'user', '_id' => '1')),
  *         array('user' => array('name' => 'hans')),
  *         array('delete' => array('_index' => 'test', '_type' => 'user', '_id' => '2'))
  * );
  *
  * @throws \Elastica\Exception\ResponseException
  * @throws \Elastica\Exception\InvalidException
  * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html
  *
  * @param  array                      $params Parameter array
  * @return \Elastica\Bulk\ResponseSet Response object
  */
 public function bulk(array $params)
 {
     if (empty($params)) {
         throw new InvalidException('Array has to consist of at least one param');
     }
     $bulk = new Bulk($this);
     $bulk->addRawData($params);
     return $bulk->send();
 }
Ejemplo n.º 4
0
 /**
  * @param array $responseData
  * @param array $actions
  *
  * @return \Elastica\Bulk\ResponseSet
  */
 protected function _createResponseSet(array $responseData, array $actions)
 {
     $client = $this->getMock('Elastica\\Client', array('request'));
     $client->expects($this->once())->method('request')->withAnyParameters()->will($this->returnValue(new Response($responseData)));
     $bulk = new Bulk($client);
     $bulk->addActions($actions);
     return $bulk->send();
 }
Ejemplo n.º 5
0
 /**
  * @group unit
  */
 public function testSetRequestParam()
 {
     $bulk = new Bulk($this->_getClient());
     $this->assertInstanceOf('Elastica\\Bulk', $bulk->setRequestParam('key', 'value'));
 }
Ejemplo n.º 6
0
 /**
  * admin_init action. mapping to Elasticsearch
  *
  * @return true or WP_Error object
  * @since 0.1
  */
 public function _data_sync()
 {
     try {
         $options = get_option('wpels_settings');
         $client = $this->_create_client($options);
         if (!$client) {
             throw new Exception('Couldn\'t make Elasticsearch Client. Parameter is not enough.');
         }
         $options = get_option('wpels_settings');
         $index = $client->getIndex($options['index']);
         $index->create(array(), true);
         $type = $index->getType($options['type']);
         $mapping = array('post_title' => array('type' => 'string', 'analyzer' => 'kuromoji'), 'post_content' => array('type' => 'string', 'analyzer' => 'kuromoji'));
         if (!empty($options['custom_fields'])) {
             $custom_fields = explode("\n", $options['custom_fields']);
             $custom_fields = array_map('trim', $custom_fields);
             $custom_fields = array_filter($custom_fields, 'strlen');
             foreach ($custom_fields as $field) {
                 $mapping[$field] = array('type' => 'string', 'analyzer' => 'kuromoji');
             }
         }
         $type->setMapping($mapping);
         $my_posts = get_posts(array('posts_per_page' => -1));
         $docs = array();
         foreach ($my_posts as $p) {
             $d = array('post_title' => (string) $p->post_title, 'post_content' => (string) strip_tags($p->post_content));
             if (!empty($options['custom_fields'])) {
                 foreach ($custom_fields as $field) {
                     $d[$field] = (string) strip_tags(get_post_meta($p->ID, $field, true));
                 }
             }
             $docs[] = $type->createDocument((int) $p->ID, $d);
         }
         $bulk = new Bulk($client);
         $bulk->setType($type);
         $bulk->addDocuments($docs);
         $bulk->send();
         return true;
     } catch (Exception $e) {
         $err = new WP_Error('Elasticsearch Mapping Error', $e->getMessage());
         return $err;
     }
 }
 /**
  * admin_init action. mapping to Elasticsearch
  *
  * @return true or WP_Error object
  * @since 0.1
  */
 public function data_sync()
 {
     $client = $this->_create_client();
     if (!$client) {
         return new \WP_Error('Elasticsearch Mapping Error', 'Elasticsearch failed to create client.');
     }
     $index = $client->getIndex($this->index);
     $index->create(array(), true);
     $type = $index->getType($this->type);
     $mapping = array('product_title' => array('type' => 'string', 'analyzer' => 'kuromoji'), 'product_content' => array('type' => 'string', 'analyzer' => 'kuromoji'), 'product_excerpt' => array('type' => 'string', 'analyzer' => 'kuromoji'), 'product_tags' => array('type' => 'string', 'analyzer' => 'kuromoji'), 'product_category' => array('type' => 'string', 'analyzer' => 'kuromoji'));
     $type->setMapping($mapping);
     $my_posts = get_posts(array('posts_per_page' => -1, 'post_type' => 'product'));
     if (empty($my_posts)) {
         return new \WP_Error('Elasticsearch Mapping Error', 'Products not exist.');
     }
     $docs = array();
     foreach ($my_posts as $p) {
         $d = array('product_title' => (string) $p->post_title, 'product_content' => (string) wp_strip_all_tags($p->post_content, true), 'product_excerpt' => (string) wp_strip_all_tags($p->post_excerpt, true), 'product_tags' => $this->_get_term_name_list(get_the_terms($p->ID, 'product_tag')), 'product_cat' => $this->_get_term_name_list(get_the_terms($p->ID, 'product_cat')));
         $docs[] = $type->createDocument((int) $p->ID, $d);
     }
     $bulk = new Bulk($client);
     $bulk->setType($type);
     $bulk->addDocuments($docs);
     $bulk->send();
     return true;
 }
 public function testRetry()
 {
     $index = $this->_createIndex();
     $type = $index->getType('bulk_test');
     $client = $index->getClient();
     $doc1 = $type->createDocument(1, array('name' => 'Mister Fantastic'));
     $doc1->setOpType(Action::OP_TYPE_UPDATE);
     $doc1->setRetryOnConflict(5);
     $bulk = new Bulk($client);
     $bulk->addDocument($doc1);
     $actions = $bulk->getActions();
     $metadata = $actions[0]->getMetadata();
     $this->assertEquals(5, $metadata['_retry_on_conflict']);
     $script = new \Elastica\Script('');
     $script->setRetryOnConflict(5);
     $bulk = new Bulk($client);
     $bulk->addScript($script);
     $actions = $bulk->getActions();
     $metadata = $actions[0]->getMetadata();
     $this->assertEquals(5, $metadata['_retry_on_conflict']);
 }