addDocuments() 공개 메소드

public addDocuments ( array $documents, string $opType = null )
$documents array
$opType string
예제 #1
1
 /**
  * @group functional
  */
 public function testUpdate()
 {
     $index = $this->_createIndex();
     $type = $index->getType('bulk_test');
     $client = $index->getClient();
     $doc1 = $type->createDocument(1, array('name' => 'John'));
     $doc2 = $type->createDocument(2, array('name' => 'Paul'));
     $doc3 = $type->createDocument(3, array('name' => 'George'));
     $doc4 = $type->createDocument(4, array('name' => 'Ringo'));
     $documents = array($doc1, $doc2, $doc3, $doc4);
     //index some documents
     $bulk = new Bulk($client);
     $bulk->setType($type);
     $bulk->addDocuments($documents);
     $response = $bulk->send();
     $this->assertTrue($response->isOk());
     $this->assertFalse($response->hasError());
     $index->refresh();
     //test updating via document
     $doc2 = $type->createDocument(2, array('name' => 'The Walrus'));
     $bulk = new Bulk($client);
     $bulk->setType($type);
     $updateAction = new \Elastica\Bulk\Action\UpdateDocument($doc2);
     $bulk->addAction($updateAction);
     $response = $bulk->send();
     $this->assertTrue($response->isOk());
     $this->assertFalse($response->hasError());
     $index->refresh();
     $doc = $type->getDocument(2);
     $docData = $doc->getData();
     $this->assertEquals('The Walrus', $docData['name']);
     //test updating via script
     $script = new \Elastica\Script('ctx._source.name += param1;', array('param1' => ' was Paul'), null, 2);
     $doc2 = new Document();
     $script->setUpsert($doc2);
     $updateAction = Action\AbstractDocument::create($script, Action::OP_TYPE_UPDATE);
     $bulk = new Bulk($client);
     $bulk->setType($type);
     $bulk->addAction($updateAction);
     $response = $bulk->send();
     $this->assertTrue($response->isOk());
     $this->assertFalse($response->hasError());
     $index->refresh();
     $doc2 = $type->getDocument(2);
     $this->assertEquals('The Walrus was Paul', $doc2->name);
     //test upsert
     $script = new \Elastica\Script('ctx._scource.counter += count', array('count' => 1), null, 5);
     $doc = new Document('', array('counter' => 1));
     $script->setUpsert($doc);
     $updateAction = Action\AbstractDocument::create($script, Action::OP_TYPE_UPDATE);
     $bulk = new Bulk($client);
     $bulk->setType($type);
     $bulk->addAction($updateAction);
     $response = $bulk->send();
     $this->assertTrue($response->isOk());
     $this->assertFalse($response->hasError());
     $index->refresh();
     $doc = $type->getDocument(5);
     $this->assertEquals(1, $doc->counter);
     //test doc_as_upsert
     $doc = new \Elastica\Document(6, array('test' => 'test'));
     $doc->setDocAsUpsert(true);
     $updateAction = Action\AbstractDocument::create($doc, Action::OP_TYPE_UPDATE);
     $bulk = new Bulk($client);
     $bulk->setType($type);
     $bulk->addAction($updateAction);
     $response = $bulk->send();
     $this->assertTrue($response->isOk());
     $this->assertFalse($response->hasError());
     $index->refresh();
     $doc = $type->getDocument(6);
     $this->assertEquals('test', $doc->test);
     //test doc_as_upsert with set of documents (use of addDocuments)
     $doc1 = new \Elastica\Document(7, array('test' => 'test1'));
     $doc1->setDocAsUpsert(true);
     $doc2 = new \Elastica\Document(8, array('test' => 'test2'));
     $doc2->setDocAsUpsert(true);
     $docs = array($doc1, $doc2);
     $bulk = new Bulk($client);
     $bulk->setType($type);
     $bulk->addDocuments($docs, \Elastica\Bulk\Action::OP_TYPE_UPDATE);
     $response = $bulk->send();
     $this->assertTrue($response->isOk());
     $this->assertFalse($response->hasError());
     $index->refresh();
     $doc = $type->getDocument(7);
     $this->assertEquals('test1', $doc->test);
     $doc = $type->getDocument(8);
     $this->assertEquals('test2', $doc->test);
     //test updating via document with json string as data
     $doc3 = $type->createDocument(2);
     $bulk = new Bulk($client);
     $bulk->setType($type);
     $doc3->setData('{"name" : "Paul it is"}');
     $updateAction = new \Elastica\Bulk\Action\UpdateDocument($doc3);
     $bulk->addAction($updateAction);
     $response = $bulk->send();
     $this->assertTrue($response->isOk());
     $this->assertFalse($response->hasError());
     $index->refresh();
     $doc = $type->getDocument(2);
     $docData = $doc->getData();
     $this->assertEquals('Paul it is', $docData['name']);
     $index->delete();
 }
예제 #2
0
 /**
  * Bulk deletes documents
  *
  * @throws \Elastica\Exception\InvalidException
  *
  * @param  array|\Elastica\Document[] $docs
  * @return \Elastica\Bulk\ResponseSet
  */
 public function deleteDocuments(array $docs)
 {
     if (empty($docs)) {
         throw new InvalidException('Array has to consist of at least one element');
     }
     $bulk = new Bulk($this);
     $bulk->addDocuments($docs, Action::OP_TYPE_DELETE);
     return $bulk->send();
 }
 /**
  * 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;
 }