/** * @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(); }
/** * 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; }
/** * 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(); }
/** * @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(); }
/** * 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; }