setSource() public method

public setSource ( array $source )
$source array
Beispiel #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;
 }
Beispiel #2
0
 /**
  * @group unit
  */
 public function testAction()
 {
     $action = new Action();
     $this->assertEquals('index', $action->getOpType());
     $this->assertFalse($action->hasSource());
     $expected = '{"index":{}}' . "\n";
     $this->assertEquals($expected, $action->toString());
     $action->setIndex('index');
     $expected = '{"index":{"_index":"index"}}' . "\n";
     $this->assertEquals($expected, $action->toString());
     $action->setType('type');
     $expected = '{"index":{"_index":"index","_type":"type"}}' . "\n";
     $this->assertEquals($expected, $action->toString());
     $action->setId(1);
     $expected = '{"index":{"_index":"index","_type":"type","_id":1}}' . "\n";
     $this->assertEquals($expected, $action->toString());
     $action->setRouting(1);
     $expected = '{"index":{"_index":"index","_type":"type","_id":1,"_routing":1}}' . "\n";
     $this->assertEquals($expected, $action->toString());
     $client = $this->_getClient();
     $index = new Index($client, 'index2');
     $type = new Type($index, 'type2');
     $action->setIndex($index);
     $expected = '{"index":{"_index":"index2","_type":"type","_id":1,"_routing":1}}' . "\n";
     $this->assertEquals($expected, $action->toString());
     $action->setType($type);
     $expected = '{"index":{"_index":"index2","_type":"type2","_id":1,"_routing":1}}' . "\n";
     $this->assertEquals($expected, $action->toString());
     $action->setSource(array('user' => 'name'));
     $expected = '{"index":{"_index":"index2","_type":"type2","_id":1,"_routing":1}}' . "\n";
     $expected .= '{"user":"******"}' . "\n";
     $this->assertEquals($expected, $action->toString());
     $this->assertTrue($action->hasSource());
     $this->assertFalse(Action::isValidOpType('foo'));
     $this->assertTrue(Action::isValidOpType('delete'));
 }
Beispiel #3
0
 /**
  * @param array $data
  *
  * @throws \Elastica\Exception\InvalidException
  *
  * @return $this
  */
 public function addRawData(array $data)
 {
     foreach ($data as $row) {
         if (is_array($row)) {
             $opType = key($row);
             $metadata = reset($row);
             if (Action::isValidOpType($opType)) {
                 // add previous action
                 if (isset($action)) {
                     $this->addAction($action);
                 }
                 $action = new Action($opType, $metadata);
             } elseif (isset($action)) {
                 $action->setSource($row);
                 $this->addAction($action);
                 $action = null;
             } else {
                 throw new InvalidException('Invalid bulk data, source must follow action metadata');
             }
         } else {
             throw new InvalidException('Invalid bulk data, should be array of array, Document or Bulk/Action');
         }
     }
     // add last action if available
     if (isset($action)) {
         $this->addAction($action);
     }
     return $this;
 }
Beispiel #4
0
 /**
  * @group unit
  */
 public function testAddActions()
 {
     $client = $this->_getClient();
     $bulk = new Bulk($client);
     $action1 = new Action(Action::OP_TYPE_DELETE);
     $action1->setIndex('index');
     $action1->setType('type');
     $action1->setId(1);
     $action2 = new Action(Action::OP_TYPE_INDEX);
     $action2->setIndex('index');
     $action2->setType('type');
     $action2->setId(1);
     $action2->setSource(array('name' => 'Batman'));
     $actions = array($action1, $action2);
     $bulk->addActions($actions);
     $getActions = $bulk->getActions();
     $this->assertSame($action1, $getActions[0]);
     $this->assertSame($action2, $getActions[1]);
 }