Elastica\Tool\CrossIndex::reindex PHP Метод

reindex() публичный статический Метод

Reindex documents from an old index to a new index.
public static reindex ( Index $oldIndex, Index $newIndex, array $options = [] ) : Index
$oldIndex Elastica\Index
$newIndex Elastica\Index
$options array keys: CrossIndex::OPTION_* constants
Результат Elastica\Index The new index object
    public static function reindex(Index $oldIndex, Index $newIndex, array $options = [])
    {
        // prepare search
        $search = $oldIndex->createSearch();
        $options = array_merge([self::OPTION_TYPE => null, self::OPTION_QUERY => new MatchAll(), self::OPTION_EXPIRY_TIME => '1m', self::OPTION_SIZE_PER_SHARD => 1000], $options);
        if (isset($options[self::OPTION_TYPE])) {
            $type = $options[self::OPTION_TYPE];
            $search->addTypes(is_array($type) ? $type : [$type]);
        }
        $search->setQuery($options[self::OPTION_QUERY]);
        // search on old index and bulk insert in new index
        $scroll = new Scroll($search, $options[self::OPTION_EXPIRY_TIME]);
        foreach ($scroll 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;
    }

Usage Example

 /**
  * Test reindex type option.
  *
  * @group functional
  */
 public function testReindexTypeOption()
 {
     $oldIndex = $this->_createIndex('', true, 2);
     $type1 = $oldIndex->getType('crossIndexTest_1');
     $type2 = $oldIndex->getType('crossIndexTest_2');
     $docs1 = $this->_addDocs($type1, 10);
     $docs2 = $this->_addDocs($type2, 10);
     $newIndex = $this->_createIndex(null, true, 2);
     // \Elastica\Type
     CrossIndex::reindex($oldIndex, $newIndex, array(CrossIndex::OPTION_TYPE => $type1));
     $this->assertEquals(10, $newIndex->count());
     $newIndex->deleteDocuments($docs1);
     // string
     CrossIndex::reindex($oldIndex, $newIndex, array(CrossIndex::OPTION_TYPE => 'crossIndexTest_2'));
     $this->assertEquals(10, $newIndex->count());
     $newIndex->deleteDocuments($docs2);
     // array
     CrossIndex::reindex($oldIndex, $newIndex, array(CrossIndex::OPTION_TYPE => array('crossIndexTest_1', $type2)));
     $this->assertEquals(20, $newIndex->count());
 }
All Usage Examples Of Elastica\Tool\CrossIndex::reindex
CrossIndex