/** * Deletes documents with the given ids, index, type from the index * * @throws \Elastica\Exception\InvalidException * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html * * @param array $ids Document ids * @param string|\Elastica\Index $index Index name * @param string|\Elastica\Type $type Type of documents * @param string|false $routing Optional routing key for all ids * @return \Elastica\Bulk\ResponseSet Response object */ public function deleteIds(array $ids, $index, $type, $routing = false) { if (empty($ids)) { throw new InvalidException('Array has to consist of at least one id'); } $bulk = new Bulk($this); $bulk->setIndex($index); $bulk->setType($type); foreach ($ids as $id) { $action = new Action(Action::OP_TYPE_DELETE); $action->setId($id); if (!empty($routing)) { $action->setRouting($routing); } $bulk->addAction($action); } 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; } }
/** * @group unit */ public function testGetPath() { $client = $this->_getClient(); $bulk = new Bulk($client); $this->assertEquals('_bulk', $bulk->getPath()); $indexName = 'testIndex'; $bulk->setIndex($indexName); $this->assertEquals($indexName . '/_bulk', $bulk->getPath()); $typeName = 'testType'; $bulk->setType($typeName); $this->assertEquals($indexName . '/' . $typeName . '/_bulk', $bulk->getPath()); }
/** * 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; }